首页 > 代码库 > context.startActivity时报错startActivity() from outside of an Activity context require the FLAG_ACTIVITY_NEW_TASK flag
context.startActivity时报错startActivity() from outside of an Activity context require the FLAG_ACTIVITY_NEW_TASK flag
源代码如下:
1 public class ReceiveHandler extends BroadcastReceiver{ 2 3 private final String ACTION_RECE_LinkReply="com.hutao.linkRequestReceive"; 4 private Context context; 5 private int ReceiveNumber; 6 private byte businessType; 7 @Override 8 public void onReceive(Context context, Intent intent) { 9 // TODO Auto-generated method stub10 this.context=context;11 if (intent.getAction().equals(ACTION_RECE_LinkReply)) {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("注意:")29 .setIcon(R.drawable.dailog_icon)30 .setMessage("MS0"+ReceiveNumber+"请求建立语音连接,是否同意?")31 .setPositiveButton("同意", new DialogInterface.OnClickListener() {32 33 @Override34 public void onClick(DialogInterface arg0, int arg1) {35 // TODO Auto-generated method stub36 /*发送语音连接请求,同意接收*/37 System.out.println("进入语音连接请求中……………………");38 new Thread(new DataSend(CreateSignal.LinkReply(businessType))).start();/*发送连接请求同意信令,businessType业务类型*/39 new Thread(new DataSend(CreateSignal.AudioReceive())).start();/*发送音频接收操作命令*/40 Intent audioIntent=new Intent(context,PhoneInterface.class);41 audioIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);42 context.startActivity(audioIntent);43 }44 })45 .setNegativeButton("取消", 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 }
在没有audioIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);这句时候程序会报错,报错内容如下:
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)
报错的原因:
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?
Context中有一个startActivity方法,Activity继承Context,重载了startActivity方法。如果使用的startActivity方法,不会有任何限制,而如果使用Context的startActivity方法的话,就需要开启一个新的task,遇到上面那个异常的,都是因为使用了Context的startActivity方法。解决办法是:加一个flag,audioIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。