0

カスタムアプリクラスがあります

public class MyApp extends Application {

    public static Context application_context;

 @Override
    public void onCreate() {
        super.onCreate();
application_context=getApplicationContext();
    }

    public static void startShareIntent() {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, "some text");

        shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        shareIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
        application_context.startActivity(Intent.createChooser(shareIntent, 
                                                                  "Share with"));
    }
}

このメソッドを呼び出すと、このエラーメッセージが表示されます

「Activity コンテキストの外部から startActivity() を呼び出すには、FLAG_ACTIVITY_NEW_TASK フラグが必要です。これでよろしいですか?」

はい、それは本当に欲しいのですが、なぜ私はそれができないのですか?

4

1 に答える 1

0

Intent.createChooser() を呼び出すと、FLAG_ACTIVITY_NEW_TASK が設定されていない新しい ACTION_CHOOSER インテントが返されます。そのため、このエラーが発生しています。多分あなたはこのようなものが欲しい:

public static void startShareIntent() {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, "some text");

    shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
    Intent chooserIntent = Intent.createChooser(shareIntent, "Share with");
    chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    application_context.startActivity(ChooserIntent);
}
于 2012-04-27T10:40:30.367 に答える