1

私が達成しようとしていること:

インストール/アンインストール/変更/置換されたすべてのアプリケーション/パッケージを追跡しようとしています。これらすべてのインテントをレシーバーにブロードキャストレシーバーを登録し、サービスを開始して自分の仕事をしたいと思います。アプリが現在アクティブかどうかに関係なく、ブロードキャスト レシーバーが呼び出されることを願っています。

私がやった事:

最初に、マニフェスト ファイルに android.intent.action.PACKAGE_ADDED,REMOVED,CHANGED,REPLACED インテントの MyBroadcastReceiver を登録しました。私のレシーバーは呼び出されませんでした。フォーラムの質問を調べて、これに出くわし、 MyApplication.onCreate() オーバーライドで行ったこれらのインテントに対してレシーバーを動的に登録することを提案しましたが、アプリケーションがアクティブな場合でも (おそらく停止した場合でも)、まだ成功していません。

私が避けようとしていること:

システムと同期するためだけに、アプリケーションが開始されるたびに次のコードを呼び出す、最も醜く非効率的な方法は使用したくありません。

public List<ResolveInfo> getLaunchable() {
     Intent intent = new Intent(Intent.ACTION_MAIN);
     intent.addCategory(Intent.CATEGORY_LAUNCHER);              
     return myContext.getPackageManager().queryIntentActivities(intent, 0);
}

私の質問:

私のソリューションに欠けているものを誰か教えてください。この問題に対する別の解決策はありますか?

私のマニフェストファイル:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.my_name_space" android:versionCode="1" android:versionName="1.0" >

<uses-sdk android:minSdkVersion="15" />

<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">

    <receiver 
        android:name=".MyReceiver" android:enabled="true" android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <action android:name="android.intent.action.PACKAGE_REMOVED" />
            <action android:name="android.intent.action.PACKAGE_CHANGED" />
            <action android:name="android.intent.action.PACKAGE_REPLACED" />
            <action android:name="com.gg.test_action"/>
        </intent-filter>
    </receiver>

</application>

</manifest>

MyBroadcastReceiver:

package com.my_name_space;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
    // Never make it to here…
    Toast.makeText(context, "Got " + intent.getAction(), Toast.LENGTH_LONG);
    }
}

動的登録のバリエーションでは、PackageBrApp を追加しました。

package com.my_name_space;
import android.app.Application;
import android.content.Intent;
import android.content.IntentFilter;

public class PackageBrApp extends Application { 
    public MyReceiver myReceiver = null;

    @Override
    public void onCreate(){
        super.onCreate();
        myReceiver = new MyReceiver();
        registerReceiver(myReceiver, new IntentFilter(Intent.ACTION_PACKAGE_ADDED));
        registerReceiver(myReceiver, new IntentFilter(Intent.ACTION_PACKAGE_REMOVED));
        registerReceiver(myReceiver, new IntentFilter(Intent.ACTION_PACKAGE_CHANGED));
        registerReceiver(myReceiver, new IntentFilter(Intent.ACTION_PACKAGE_REPLACED));
    }
}

それに応じてマニフェストファイルを変更しました(レシーバー宣言を削除しました):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=http://schemas.android.com/apk/res/android 
    package="com.gg" android:versionCode="1" android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
    </application>

</manifest>

@@@@ アクティビティを追加する提案に従って変更 @@@@

残念ながら、アクティビティを追加しても (インストール後、最初のブロードキャスト レシーバーを使用する前だけでなく、複数回起動しても) 役に立ちませんでした。

受信者はまだ目覚めることを拒否しています…</p>

更新されたレシーバー コードは次のとおりです (デバッガーがそこで停止しない可能性を排除するために、いくつかの永続的な設定を更新しています)。

package com.my_name_space;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        SharedPreferences prefs = context.getSharedPreferences("ActivityBr", Context.MODE_PRIVATE);        
        Editor prefsEditor = prefs.edit();
        prefsEditor.putBoolean("receiverInvoked", true);
        prefsEditor.apply();        
    }
}

追加されたアクティビティ:

package com.my_name_space;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.widget.TextView;

public class ActivityBr extends Activity  {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState);               
        setContentView(R.layout.main);
        TextView v = (TextView) findViewById(R.id.text_view);

        SharedPreferences prefs = getSharedPreferences("ActivityBr", MODE_PRIVATE);
        boolean receiverInvoked = prefs.getBoolean("receiverInvoked", false);
        v.setText((receiverInvoked ? "BR invoked" : "BR not invoked"));

        Editor prefsEditor = prefs.edit();
        prefsEditor.putBoolean("receiverInvoked", false);
        prefsEditor.apply();
    }
}

マニフェスト ファイルは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.my_name_space"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ActivityBr"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver 
            android:name=".MyReceiver" 
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <action android:name="android.intent.action.PACKAGE_CHANGED" />
                <action android:name="android.intent.action.PACKAGE_REPLACED" />
                <action android:name="com.gg.test_action"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

違いがある場合、main.xml は次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_name" />

</LinearLayout>

変更の定期的なポーリングという醜い解決策に行き詰まっていると思います...

4

1 に答える 1

1

最初に、マニフェスト ファイルに android.intent.action.PACKAGE_ADDED,REMOVED,CHANGED,REPLACED インテントの MyBroadcastReceiver を登録しました。私のレシーバーは呼び出されませんでした。

Android 3.1 以降の環境でこれをテストしていると思います。その場合、アプリにアクティビティを用意する必要があり、マニフェストに登録されたレシーバーが応答する前に、ユーザーはそのアクティビティを手動で 1 回実行する必要があります。

于 2012-05-06T16:50:12.800 に答える