package com.wuxiaolong.androidsamples.notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.support.v4.app.TaskStackBuilder; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import com.wuxiaolong.androidsamples.R; public class NotificationActivity extends AppCompatActivity { private int id = 100; NotificationCompat.Builder mBuilder; NotificationManager mNotificationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.icon) .setContentTitle("My notification") .setContentText("Hello World!"); mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); } /** * 创建简单通知,即常规 Activity,返回回退到主屏幕 */ public void notification1(View v) { Log.d("wxl", "notification1"); // Creates an explicit intent for an Activity in your app Intent resultIntent = new Intent(this, ResultActivity.class); // The stack builder object will contain an artificial back stack for the // started Activity. // This ensures that navigating backward from the Activity leads out of // your application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addParentStack(ResultActivity.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT ); mBuilder.setContentIntent(resultPendingIntent); mBuilder.setAutoCancel(true); // mId allows you to update the notification later on. mNotificationManager.notify(id, mBuilder.build()); } /** * 更新通知 */ public void notification2(View v) { int numMessages = 0; mBuilder.setContentText("Hello,World!") .setNumber(++numMessages); // Because the ID remains unchanged, the existing notification is // updated. mNotificationManager.notify(id, mBuilder.build()); } /** * 删除通知 */ public void notification3(View v) { // cancelAll()将删除之前发出的所有通知 mNotificationManager.cancel(id); } /** * 设置特殊Activity */ public void notification4(View v) { Intent notifyIntent = new Intent(this, Result2Activity.class); // Sets the Activity to start in a new, empty task notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // Creates the PendingIntent PendingIntent notifyPendingIntent = PendingIntent.getActivity( this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT ); mBuilder.setContentIntent(notifyPendingIntent); mNotificationManager.notify(id + 1, mBuilder.build()); } public void notification5(View v) { new Thread(new Runnable() { @Override public void run() { int incr; // Do the "lengthy" operation 20 times for (incr = 0; incr <= 100; incr += 5) { // Sets the progress indicator to a max value, the // current completion percentage, and "determinate" // state mBuilder.setProgress(100, incr, false); // Displays the progress bar for the first time. mNotificationManager.notify(id, mBuilder.build()); // Sleeps the thread, simulating an operation // that takes time try { // Sleep for 5 seconds Thread.sleep(5 * 1000); } catch (InterruptedException e) { } } // When the loop is finished, updates the notification mBuilder.setContentText("Download complete") // Removes the progress bar .setProgress(0, 0, false); mNotificationManager.notify(id, mBuilder.build()); } } ).start(); } }