0

メイン アクティビティと別のクラスに情報を送信するサービスに、暗黙のインテントがあります。また、そのインテントでメインのアクティビティを開始したいと考えています。私はこれに関連する無数の投稿を見て、さまざまなことを試しましaddCategoryた.setAction(MAIN; the activity's name; you name it, I've tried it...)category.DEFAULT

インテントが設定される場所とマニフェストの関連部分は次のとおりです。インテントの受信者は、メイン アクティビティに登録されます。

final String NEW_DOSES = "changed to protect the innocent";
Intent bluetoothBroadcast = new Intent();
several putExtra lines here
bluetoothBroadcast.setAction(NEW_DOSES);
sendBroadcast(bluetoothBroadcast);

<activity
   android:name=".AsthmaAppActivity"
   android:label="@string/app_name" >
   <intent-filter>
       <action android:name="android.intent.action.MAIN" />
       <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
</activity>

比較的マイナーな変更でメイン アクティビティを開始するというこのインテントを取得することは可能ですか? 前もって感謝します。

4

1 に答える 1

1

はい、可能ですが、sendBroadcast(bluetoothBroadcast); ではできません。sendBroadcast はアクティビティを起動しません。これを実現するには、startActivity を使用する必要があります。たとえば、アプリケーションを起動するためにランチャー アプリケーションが行うことは次のとおりです。

public static void LaunchApplication(Context cx, String packagename) {
    PackageManager pm = cx.getPackageManager();
    Intent i = pm.getLaunchIntentForPackage(ai.packageName);
    if (i != null) cx.startActivity(i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}

アクティビティを開始するために必要なエクストラとデータを簡単に調整できます。たとえば、アクティビティの名前が myActivity の場合、次のようになります。

Intent i = new Intent(cx, myActivity.class);
//Put the extras and the data you want here...
//If you are launching the activity from a receiver component you must use
//i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
cx.startActivity(i);

お役に立てれば...

于 2012-09-28T21:56:16.830 に答える