编辑:管理解决它。创建一个新项目,清除应用程序数据,然后卸载它,重新安装它,它神奇地工作。谢谢您的帮助。检测应用安装
我试图检测应用何时安装,但我不太确定我要出错的位置。我在这里查了几个问题,但我似乎无法弄清楚。我对android非常陌生,所以我可能错过了一些明显的东西。这是我所拥有的测试案例。
我将它安装到了我的手机,并且过程和服务显示为在设置中运行。然后,我从Play商店下载应用程序,检查logcat,并且应用程序中没有任何内容。
任何帮助我去哪里错了?谢谢。
编辑:
用它打的多了一些之后,我有一些新的例子测试代码。安装新包时,onReceive方法不会触发。然而,当我测试了一些东西的时候,我在BroadcastReceiver类中添加了一个构造函数,并且每次安装包时构造函数都会触发。所以,它似乎正在接受意图,但onReceive内部的日志似乎从未打印出来。有任何想法吗?
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>
除非我误解了文档,这是它:http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_ADDED – Lucas