Android custom Notification layout Notification, click Notification navigation to switch back to original Activity

Source: Internet
Author: User

Android custom Notification layout Notification, click Notification navigation to switch back to original Activity

A Simple Application Scenario: if the user opens the Activity and presses the Home key, the Activity enters-> onPause ()-> onStop () invisible. The code sends a Notification to the Notification bar at this time. After you click Notification in the Notification bar, you can switch onRestart ()-> onStart ()-> onResume () back to the original Activity.

Package zhangphil. pendingintent; import android. OS. bundle; import android. support. v4.app. notificationCompat; import android. util. log; import android. widget. remoteViews; import android. widget. textView; import android. app. activity; import android. app. notification; import android. app. icationicationmanager; import android. app. pendingIntent; import android. content. context; import android. content. intent; public class MainActivity extends Activity {private final int NOTIFICATION_ID = 0xa01; private final int REQUEST_CODE = 0xb01; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); Log. d (this. getClass (). getName (), "onCreate ()"); setContentView (R. layout. activity_main); TextView TV = (TextView) findViewById (R. id. textView); TV. setText (System. currentTimeMillis () + "") ;}@ Override Protected void onRestart () {super. onRestart (); Log. d (this. getClass (). getName (), "onRestart ()") ;}@ Overrideprotected void onStart () {super. onStart (); Log. d (this. getClass (). getName (), "onStart ()") ;}@ Overrideprotected void onResume () {super. onResume (); Log. d (this. getClass (). getName (), "onResume ()");} // The lock screen of the Android device also enters onStop () @ Overrideprotected void onStop () {super. onStop (); Log. d (this. getClass (). getName (), "on Stop () "); sendNotification (this);} private void sendNotification (Context context) {icationicationmanager = (icationicationmanager) getSystemService (icationication_service); NotificationCompat. builder mBuilder = new NotificationCompat. builder (context); // The icon set here is only used to display the mBuilder in the notification bar of the device when a new reminder is displayed. setSmallIcon (R. drawable. ic_launcher); // mBuilder. setContentTitle ("Notification title"); // mBuilder. setContentText ("Notification content"); notification Notification = mBuilder. build (); // when the user goes down the Notification bar, the custom Notification layout RemoteViews contentView = new RemoteViews (context. getPackageName (), R. layout. notification); contentView. setImageViewResource (R. id. image, R. drawable. ic_launcher); contentView. setTextViewText (R. id. title, "the title must be long ...... "); contentView. setTextViewText (R. id. text, "the content should be short ...... "); notification. contentView = cont EntView; // when a notification is sent to the notification bar, the system displays a sound prompt, a mobile phone vibrate, and an Android phone breathing light. // Note !! (Sound prompt + mobile phone shake) These two items are basically supported by Android phones. // However, whether Android breathing lights can be lit depends on the settings of various mobile phone hardware manufacturers. Notification. defaults = Notification. DEFAULT_SOUND | Notification. DEFAULT_VIBRATE | Notification. DEFAULT_LIGHTS; // click notification to automatically disappear notification. flags = Notification. FLAG_AUTO_CANCEL; // notification time. when = System. currentTimeMillis (); // as an option, you can set the MainActivity startup mode to singleTop to avoid repeated onCreate (). Intent intent = new Intent (context, MainActivity. class); // when you click Notification in the Notification bar, switch back to MainActivity. PendingIntent pi = PendingIntent. getActivity (context, REQUEST_CODE, intent, PendingIntent. FLAG_UPDATE_CURRENT); notification. contentIntent = pi; // The notification bar sent to the mobile phone icationicationmanager. Y (ication_id _id, notification );}}



It should be noted that the Android Activity is the standard mode by default, that is, every time a new Activity is generated, it is not the original Activity. In this example, we can see that if onCreate () in MainActivity does not modify the start mode, the time displayed for each TextView call is different (incrementing ), to use the original Activity and avoid repeating the new one, you must:

In AndroidManifest. xml, modify the MainActivity startup mode to singleTop.

            
                                 
              
         

Source code of the notification. xml file:

 
     
      
      
  
 


Activity_main.xml:

     
  
 

Zookeeper

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.