Android custom notification layout notification, click Notification navigation to switch back to the original activity

Source: Internet
Author: User

A simple application scenario: If the user opens the activity, press the home key, and the activity entry, OnPause (), OnStop () is not visible. The code sends a notification to the notification bar at this time. When the user clicks on the notification of the notification bar, Onrestart (), OnStart (), Onresume (), switches 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.notificationmanager;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 () + "");} @Overrideprotected 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 Android Device lock screen will also enter onStop () @Overrideprotected void OnStop () {super.onstop (); LOG.D (This.getclass (). GetName (), "OnStop ()"); SendNotification (this);} private void SendNotification (context context) {Notificationmanager Notificationmanager = (Notificationmanager) Getsystemservice (Notification_service); Notificationcompat.builder Mbuilder = new Notificationcompat.builder (context);// The icon set here is only used to display the new reminder when it appears in the notification bar of the device Mbuilder.setsmallicon (r.drawable.ic_launcher);//Mbuilder.setcontenttitle ("title of the Notice");// Mbuilder.setcontenttext ("Content of the notice"); Notification Notification = Mbuilder.build ();//When the user comes down to the notification bar, they see a custom Notification layout in Remoteviews remoteviews Contentview = new Remoteviews (Context.getpackagename (), r.layout.notification); Contentview.setimageviewresource ( R.id.image, R.drawable.ic_launcher); Contentview.settextviewtext (R.id.title, "title Long ..."); Contentview.settextviewtext (R.id.text, "short content ..."); Notification.contentview = contentview;//send notification to the notification bar: prompt sound + mobile phone vibration + Light up the Android phone breathing light. Attention!! (cue sound + phone shake) These two basically Android phones are supported. But whether or not the Android breathing light is lit depends on the individual phone hardware manufacturer's own settings. Notification.defaults = notification.default_sound| Notification.default_vibrate | notification.default_lights;//Click Notification Auto Disappear notification.flags = notification.flag_auto_cancel;// Notification Time Notification.when = System.currenttimemillis ();//Note that, as an option, here you can set the Mainactivity startup mode to Singletop, Avoid repeating new OnCreate (). Intent Intent = new Intent (context, mainactivity.class);//When the user clicks Notification in the notification bar, switch back to mainactivity. pendingintent pi = pendingintent.getactivity (context, request_code,intent, pendingintent.flag_update_current); Notification.contentintent = pi;//sent to the phone's notification bar notificationmanager.notify (notification_id, Notification);}}



It is important to note that the default Android activity is the standard mode, that is, each time new activity comes out, not the original activity, in this case, you can observe mainactivity in the OnCreate () if you do not modify the startup mode , each time the call to TextView is displayed differently (incremented), all in order to use the original activity and avoid repeating the new one, you need:

Modify the Mainactivity startup mode in Androidmanifest.xml to: singletop

<activity            android:name= "zhangphil.pendingintent.MainActivity"            android:launchmode= "Singletop            " Android:label= "@string/app_name" >            <intent-filter>                <action android:name= " Android.intent.action.MAIN "/>                <category android:name=" Android.intent.category.LAUNCHER "/>            < /intent-filter>        </activity>

Notification.xml File Source code:

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent ">    < ImageView        android:id= "@+id/image"        android:layout_width= "wrap_content" android:layout_height= "Wrap_        Content "        android:layout_alignparentleft=" true "        android:layout_marginright=" 10DP "/>    <textview        android:id= "@+id/title"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_ Content "        android:layout_torightof=" @id/image "/>    <textview        android:id=" @+id/text        " Android:layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:layout_below= "@id/ Title "        android:layout_torightof=" @id/image "/></relativelayout>


Activity_main.xml:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Match_ Parent "    android:layout_height=" match_parent ">    <textview        android:id=" @+id/textview "        Android:layout_width= "Wrap_content"        android:layout_height= "Wrap_content"/></relativelayout>



Android custom notification layout notification, click Notification navigation to switch back to the original activity

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.