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>

我的广播接收器:

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+ 环境中测试这个。如果是这样,您需要在您的应用程序中有一个活动,并且用户必须手动运行该活动一次,然后您的清单注册接收器才会响应。

于 2012-05-06T16:50:12.800 回答