首先,我感觉在实现中PendingIntent感觉就是Intent的包装。
它的三个实例化方法:
getActivity(Context, int, Intent, int)
getService(Context, int, Intent, int)
getBroadcast(Context, int, Intent, int)
感觉是保存当前的Activity的Context,然后在外部启动Intent动作。类似于代码Context.startActivity(*, *);
常和Notification和Alarm一起使用。
代码例子:
public class BannerActivity extends Activity { private Button b; private NotificationManager mNotificationManager; private Intent intent; private PendingIntent mPendingIntent; private Notification mNotification; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); b = (Button)this.findViewById(R.id.b); intent = new Intent(BannerActivity.this, Activity01.class); mPendingIntent = PendingIntent.getActivity(BannerActivity.this, 0, intent, Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); mNotification = new Notification(); b.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub mNotification.icon = R.drawable.ic_launcher; mNotification.tickerText = "通知!"; //通知在通知栏出现的时候的标题 mNotification.defaults = Notification.DEFAULT_SOUND; // 第二个参数是打开通知栏后的标题, 第三个参数是通知内容 mNotification.setLatestEventInfo(BannerActivity.this, "通知?", "通知内容!", mPendingIntent); mNotificationManager.notify(0, mNotification); } }); } }