读书人

Notification运用以及PendingIntent.g

发布时间: 2012-09-20 09:36:50 作者: rapoo

Notification使用以及PendingIntent.getActivity()

public void sendNotification(Context ctx, String message) {
// get the notification manager
String str = Context.NOTIFICATION_SERVICE;
NotificationManager nm = (NotificationManager) ctx.getSystemService(str);

// create notification object includes icon ,text ,the time to send
int icon = R.drawable.ic_launcher;
CharSequence tickerText = "hello";
long when = System.currentTimeMillis();// return the system current time
// in milliseconds since 1970

Notification notification = new Notification(icon, tickerText, when);

// set contextView by using setLastestEventInfo
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.google.com"));
PendingIntent pi = PendingIntent.getActivity(ctx, 0, intent, 0);// explain
// it
// below
notification.setLatestEventInfo(ctx, "title", "text", pi);

// send notification
nm.notify(1, notification);// 通知加入状态栏,标记为id=1
}// above all is how to use Notification

个人水平有限,接触到的暂时是这么多,因此当我系统了解完了之后会对此做一个补充

Notification 的使用需要以上四个步骤。

简单的说PendingIntent.getActivity()就是即将发生的intent.

PendingIntent.getActivity(ctx,o,intent,o)官方文档是这样解释的:

public staticPendingIntentgetActivity(Context context, int requestCode,Intent intent, int flags)Since: API Level 1

Retrieve a PendingIntent that will start a new activity, like callingContext.startActivity(Intent). Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent.

ParameterscontextThe Context in which this PendingIntent should start the activity.requestCodePrivate request code for the sender (currently not used).  现在不使用了intentIntent of the activity to be launched.flagsMay be FLAG_ONE_SHOT,FLAG_NO_CREATE,FLAG_CANCEL_CURRENT,FLAG_UPDATE_CURRENT, or any of the flags as supported byIntent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens.
Returns
读书人网 >移动开发

热点推荐