Context. startactivity error: startactivity () from outside of an activity context require the flag_activity_new_task flag

Source: Internet
Author: User

The source code is as follows:

 

1 public class receivehandler extends broadcastreceiver {2 3 private final string action_rece_linkreply = "com. hutao. linkrequestreceive "; 4 private context; 5 private int receivenumber; 6 private byte businesstype; 7 @ override 8 Public void onreceive (context, intent) {9 // todo auto-generated method stub10 this. context = context; 11 if (intent. getaction (). equals (action_rece_linkr Eply) {12 receivenumber = intent. getintextra ("Number", 0); 13 businesstype = intent. getbyteextra ("businesstype", (byte) 0); 14 switch (businesstype) {15 case datareceive. audio_connect: 16 audiohandle (); 17 break; 18 19 default: 20 break; 21} 22 23} 24 25} 26 private void audiohandle () {27 // todo auto-generated method stub28 alertdialog builder = new alertdialog. builder (context ). settitle ("Note:") 29. seticon (R. Drawable. dailog_icon) 30. setmessage ("ms0" + receivenumber + "request to establish a voice connection, do you agree? ") 31. setpositivebutton ("agree", new dialoginterface. onclicklistener () {32 33 @ override34 public void onclick (dialoginterface arg0, int arg1) {35 // todo auto-generated method stub36/* sends a voice connection request, accept */37 system. out. println ("entering the voice connection request ........................ "); 38 new thread (New datasend (createsignal. linkreply (businesstype ))). start ();/* Send the connection request to agree to the signaling, businesstype business type */39 new thread (New datasend (createsignal. audioreceive ())). start ();/* command for sending audio receiving */40 intent audiointent = new intent (context, phoneinterface. class); 41 audiointent. addflags (intent. flag_activity_new_task); 42 context. startactivity (audiointent); 43} 44}) 45. setnegativebutton ("cancel", new dialoginterface. onclicklistener () {46 47 @ override48 public void onclick (dialoginterface arg0, int arg1) {49 // todo auto-generated method stub50 51} 52 }). create (); 53 builder. getwindow (). settype (windowmanager. layoutparams. type_system_alert); 54 builder. show (); 55} 56 57}

 

The program reports an error when audiointent. addflags (intent. flag_activity_new_task) is not found. The error message is as follows:

 1 06-28 11:24:40.359: E/AndroidRuntime(7397): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 2 06-28 11:24:40.359: E/AndroidRuntime(7397):     at android.app.ContextImpl.startActivity(ContextImpl.java:617) 3 06-28 11:24:40.359: E/AndroidRuntime(7397):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:258) 4 06-28 11:24:40.359: E/AndroidRuntime(7397):     at com.hotalk.ui.homepage.favorites.FavoritesListAdapter$7.onClick(FavoritesListAdapter.java:1509) 5 06-28 11:24:40.359: E/AndroidRuntime(7397):     at android.view.View.performClick(View.java:2408) 6 06-28 11:24:40.359: E/AndroidRuntime(7397):     at android.view.View$PerformClick.run(View.java:8816) 7 06-28 11:24:40.359: E/AndroidRuntime(7397):     at android.os.Handler.handleCallback(Handler.java:587) 8 06-28 11:24:40.359: E/AndroidRuntime(7397):     at android.os.Handler.dispatchMessage(Handler.java:92) 9 06-28 11:24:40.359: E/AndroidRuntime(7397):     at android.os.Looper.loop(Looper.java:123)10 06-28 11:24:40.359: E/AndroidRuntime(7397):     at android.app.ActivityThread.main(ActivityThread.java:4627)11 06-28 11:24:40.359: E/AndroidRuntime(7397):     at java.lang.reflect.Method.invokeNative(Native Method)12 06-28 11:24:40.359: E/AndroidRuntime(7397):     at java.lang.reflect.Method.invoke(Method.java:521)13 06-28 11:24:40.359: E/AndroidRuntime(7397):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)14 06-28 11:24:40.359: E/AndroidRuntime(7397):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)15 06-28 11:24:40.359: E/AndroidRuntime(7397):     at dalvik.system.NativeStart.main(Native Method)

Cause of error:

Caused by: Android. util. androidruntimeexception: Calling startactivity () from outside of an activity context requires the flag_activity_new_task flag. Is this really what you want?

There is a startactivity method in context. Activity inherits the context and the startactivity method is overloaded. If the startactivity method is used, there is no restriction. If the startactivity method of context is used, you need to start a new task and encounter the exception above, it is because the startactivity method of context is used. Solution: Add a flag, audiointent. addflags (intent. flag_activity_new_task );

 

 

 

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.