3

我有以下代码从扩展的类发送电子邮件BroadcastReceiver

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
S2Mconfig s2m = new S2Mconfig();
Log.d(TAG, "Create Intent for mail to " + address);
emailIntent.setType("plain/text");
emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, s2m.read(thisContext));
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, address);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
Log.d(TAG, String.format("Sending mail %s", emailIntent.toString()));
thisContext.startActivity(Intent.createChooser(emailIntent, "Send mail..."));

是在BroadcastReciever中注册的manifest,我设置了INTERNET权限:

<uses-permission android:name="android.permission.INTERNET" />...

<receiver android:name=".SmsReceiver" android:exported="true" > 
        <intent-filter android:priority="1000"> 
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            <action android:name="android.provider.Telephony.MMS_RECEIVED" />
        </intent-filter>
    </receiver>

日志确认FLAG_ACTIVITY_NEW_TASK在调用 之前设置了startActivity()。尽管如此,我仍然感到害怕"Calling startActivity()... requires the FLAG_ACTIVITY_NEW_TASK flag... 任何线索将不胜感激。

4

3 回答 3

4
@Override
public void onReceive(Context context, Intent intent){
    Context appContext = context.getApplicationContext();

appContext你一起可以开始一个“正常”的活动。这里描述的是一个例子

public void sendNotificationEmail(String emailBody) {
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setType("text/html");
        emailIntent.putExtra(Intent.EXTRA_EMAIL, notificationRecipients);
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "MyAppName Error");
        emailIntent.putExtra(Intent.EXTRA_TEXT, emailBody);
        Intent emailChooser = Intent.createChooser(emailIntent, "An error has occurred! Send an error report?");
        emailChooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            startActivity(emailChooser);
        } catch (ActivityNotFoundException e) {
            // If there is nothing that can send a text/html MIME type
            e.printStackTrace();
        }
    }

所以添加FLAG_ACTIVITY_NEW_TASK到选择器意图而不是发送者!

于 2013-03-05T08:02:25.233 回答
0

尝试添加以下标志

intent.addFlags(
          Intent.FLAG_ACTIVITY_NEW_TASK
        | Intent.FLAG_ACTIVITY_CLEAR_TOP
        | Intent.FLAG_ACTIVITY_SINGLE_TOP);

当你开始你的活动时,确保你正在使用context.getApplicationContext()喜欢

context.getApplicationContext().startActivity(intent);

于 2013-03-05T08:01:30.377 回答
0

将您的代码从更改emailIntent.setType("plain/text");emailIntent.setType("text/plain");

于 2013-03-05T08:09:49.920 回答