public class MultiComponentActivity extends Activity { NotificationManager notificationManager; private final int NOTIFICATION_ID=0x123; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_multi_component); notificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE); } public void senNotification(View view){ Intent intent=new Intent(MultiComponentActivity.this,MainActivity.class); PendingIntent pendingIntent=PendingIntent.getActivity( MultiComponentActivity.this,0,intent,0); Notification notification=new Notification.Builder(MultiComponentActivity.this) .setAutoCancel(true) .setTicker("new message") .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("this is a notification") .setContentText("thank you for testing this app !") .setDefaults(Notification.DEFAULT_LIGHTS) .setWhen(System.currentTimeMillis()) .setContentIntent(pendingIntent) .build(); notificationManager.notify(NOTIFICATION_ID, notification); } public void cancelNotifiacation(View view){ notificationManager.cancel(NOTIFICATION_ID); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。