"Android development that dissected thing" message push Broadcastreceiver, click Notifications to open two activity questions

Source: Internet
Author: User

In Android development, Broadcastreceiver is typically used to accept push push messages. When the app receives a push notification, we need to add our own logic to the notification's Click event. For example, jump to mainactivity.
For example, the following code (note the red part):
public void OnReceive (context context, Intent Intent) {Bundle bundle = Intent.getextras (); if (JPushInterface.ACTION_REGISTRATION_ID.equals (Intent.getaction ())) {String Regid = bundle.getstring (Jpushin Terface.            EXTRA_REGISTRATION_ID);            LOG.D (TAG, "[Jpushreceiver] Receive registration Id:" + regid); Send the registration Id to your server ...} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals (Intent.getaction ())) {LOG.D (TAG, "[Jpushreceiver] receives the push-down self-                Definition message: "+ bundle.getstring (jpushinterface.extra_message)); } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals (Intent.getaction ())) {LOG.D (TAG, "[Jpushreceiver            Received a push-down notification ");            int Notifactionid = Bundle.getint (jpushinterface.extra_notification_id);                LOG.D (TAG, "[Jpushreceiver] receives a push-down notification ID:" + notifactionid); } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals (intent.getactIon ()) {LOG.D (TAG, "[Jpushreceiver] user clicked to open the notification");                     jpushinterface.reportnotificationopened (Context, bundle.getstring (jpushinterface.extra_msg_id));//Open the custom activity Intent i = new Intent (context, maintabactivity.class);        I.putextras (bundle);        I.setflags (Intent.flag_activity_new_task); Context.startactivity (i);} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals (Intent.getaction ())) {LOG.D (TAG, "[Myreceiver] user receives            RICH PUSH CALLBACK: "+ bundle.getstring (Jpushinterface.extra_extra));                In this case, according to the content of Jpushinterface.extra_extra processing code, such as open a new activity, open a Web page and so on.        } else {log.d (TAG, "[Myreceiver] Unhandled Intent-" + intent.getaction ()); }}

Use content other than ACTIVITY to startactivity and must be specified as Intent.flag_activity_new_task.
I.setflags (Intent.flag_activity_new_task);


We can try to use Intent.flag_activity_brought_to_front and throw the following exception:

Error/androidruntime (7557): java.lang.RuntimeException:Unable to start receiver Com.wcc.Wakeup: Android.util.AndroidRuntimeException:Calling startactivity () from outside of an Activity context requires the Flag_activ Ity_new_task flag. Is this really what do you want?

For more information about Intent flag, refer to: http://developer.android.com/reference/android/content/Intent.html



So you can only use Flag_activity_new_task. Another problem with this is that it will open a new mainactivity. How to solve this problem?
In fact, it is very simple to define mainactivity in Androidmanifest.xml as:
Android:launchmode= "Singletask" can:
<activity android:name= "com.withiter.quhao.activity.MainTabActivity" android:launchmode= "Singletask" Android: Label= "@string/app_name" android:screenorientation= "Portrait" >

This way, every time you open a push, there will be no 2 activity cases.

For more information on activity Launchmode, refer to: Http://developer.android.com/guide/topics/manifest/activity-element.html#lmode


Well, today this is a bad thing to be here, in fact, Android development is so dissected. For other broken things, see column:

More about Android development, see the column: "Android development that dissected thing"

Related Article

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.