4

Edit: Managed to solve it. Created a new project, cleared app data before uninstalling it, re-installed it, and it magically worked. Thanks for the help.

I'm trying to detect when an app is installed, but I'm not quite sure where I am going wrong. I've checked several questions on here, but I just can't seem to figure it out. I'm pretty new to android, so I may have missed something obvious. Here is the test case of what I have.

I installed it to my phone, and the process and service is shown as running in the settings. Then, I download an app from the play store, check the logcat, and there is nothing from the app.

Any help on where I'm going wrong? Thanks.

Edit:

After playing with it some more, I have some new example test code. When a new package is installed, the onReceive method doesn't fire. However, as I was testing things out, I added a constructor to the BroadcastReceiver class, and the constructor fires everytime a package is installed. So, it seems to be receiving the intent, but the log inside the onReceive never seems to print out. Any ideas?

public class InstallReceiver extends BroadcastReceiver {

    public InstallReceiver()
    {
        //This log will display in the logcat
        Log.d("InstallReceiver", "InstallReceiver constructor called.");
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        //This log never displays if the constructor is in or commented out
        Log.d("InstallReceiver", "Install detected.");
    }

}

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

4 回答 4

1

也包括这个

<receiver android:name=".InstallReceiver">
    <intent-filter >
        <action android:name="android.intent.action.PACKAGE_ADDED"/>
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

您所做的其他一切似乎都是正确的。

唯一的另一种可能性是正如 prijupaul 所说,出于安全原因,此广播可能不再有效。

于 2013-09-23T06:10:39.650 回答
0

来自 developer.android.com 参考。- “注意新安装的包收不到这个广播。” - 所以你无法检测到你自己的包安装。它应该检测其他软件包安装。

于 2015-07-09T12:41:20.607 回答
0

文档说:“广播动作:设备上已经安装了一个新的应用包。数据中包含包的名称。注意新安装的包没有收到这个广播。

于 2015-08-28T08:49:50.020 回答
-2

AFAIK,出于隐私原因,这在 Android 中是不可能的。没有广播说,已安装应用程序。

于 2013-09-23T02:58:43.023 回答