7

ユーザーが自分のデバイスでアプリケーションを開いたりインストールしたりしたいという解決策を探しています。BroadcastReceiver クラスからユーザーが開いたアプリを取得する必要があります。次のようにコードを実装しました。

public class AppReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {

    ActivityManager am = (ActivityManager)context.getSystemService(context.ACTIVITY_SERVICE);
    List l = am.getRunningAppProcesses();
    Iterator i = l.iterator();
    PackageManager pm = context.getPackageManager();
    while(i.hasNext()) {
      ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
      try {

        CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
        Log.v(" App Name : ", c.toString());


      }catch(Exception e) {
      }
    }


}

また、マニフェスト ファイルにこのレシーバーについて次のように追加しました。

  <receiver android:name="AppReciever">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.PACKAGE_ADDED"></action>
            <action android:name="android.intent.action.PACKAGE_CHANGED"></action>
            <action android:name="android.intent.action.PACKAGE_INSTALL"></action>
            <action android:name="android.intent.action.PACKAGE_REPLACED"></action>
            <action android:name="android.intent.action.PACKAGE_RESTARTED"></action>
  <data android:scheme="package" />

  </intent-filter>
    </receiver>

上記のコードから、デバイスに既に存在する (インストールされている) 新しいアプリを開いたときに、Log.v でアプリ名を取得するために AppReciver が実行されませんでした。

BroadcastReceiverから現在開いているアプリケーションを取得するのを手伝ってください

4

1 に答える 1

2

サービス registerReceiver() に追加します。受信者を登録解除することを忘れないでください。

    AppReceiver appReceiver = new AppReceiver();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.setPriority(900);
    intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
    intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
    intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL); //@deprecated This constant has never been used.
    intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
    intentFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
    registerReceiver(appReceiver, intentFilter);

登録解除の場合:unregisterReceiver(appReceiver);

于 2013-01-18T11:20:25.653 に答える