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()likeを使用していることを確認してください

context.getApplicationContext().startActivity(意図);

于 2013-03-05T08:01:30.377 に答える
0

コードを からemailIntent.setType("plain/text");に変更しますemailIntent.setType("text/plain");

于 2013-03-05T08:09:49.920 に答える