Android launches another app's activity via broadcast

Source: Internet
Author: User

Requirement: Now there is application A and application B, I need to start an activity in the B application in a application

Implementation: A application in the activity to send broadcasts, the key code is as follows:

String broadcastintent = "com.example.android.notepad.NotesList";//Customize it Yourself
Intent Intent = new Intent (broadcastintent);
This.sendbroadcast (Intent);

b application needs a broadcastreceiver to receive the broadcast, named Testreceiver inherit Broadcastreceiver override OnReceive method to start an activity, the key code is as follows:

if (Intent.getaction (). Equals ("Com.example.android.notepad.NotesList")) {
Intent notelist = new Intent (context,noteslist.class);
Notelist.addflags (Intent.flag_activity_new_task);
Context.startactivity (notelist);
}

To this code is done, of course, in Androidmanifest.xml to register the Testreceiver, the code is as follows:

<receiver android:name= "Testreceiver" >
<intent-filter>
<action android:name= "Com.example.android.notepad.NotesList"/>
</intent-filter>
</receiver>

This completes the activation of another application via broadcast activity.

Note: There is a startactivity method in context where activity inherits from the context and overloads the StartActivity method. If you use the activity's StartActivity method, there is no limit, and if you use the StartActivity method of the context, you need to open a new task, the solution is to add a flag, This is the notelist.addflags (intent.flag_activity_new_task); If you do not add this sentence, you will be quoted Android.util.AndroidRuntimeException:Calling StartActivity () from outside of a activity,caused by: 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?

Android launches another app's activity by broadcasting

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.