2013-09-23 63 views
3

编辑:管理解决它。创建一个新项目,清除应用程序数据,然后卸载它,重新安装它,它神奇地工作。谢谢您的帮助。检测应用安装

我试图检测应用何时安装,但我不太确定我要出错的位置。我在这里查了几个问题,但我似乎无法弄清楚。我对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> 

回答

-1

据我所知,这是不可能的的Android隐私的原因。没有广播说,应用程序已安装。

+2

除非我误解了文档,这是它:http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_ADDED – Lucas

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说,出于安全原因,此广播可能不再工作。

+0

我也试过这个,我仍然无法得到它的工作。我认为它仍然在系统中工作。我的手机上有防病毒程序,会在下载并安装新应用程序时触发。 – Lucas

0

from the developer.android.com ref。 - “请注意,新安装的软件包未收到此广播。” - 所以你无法检测到你自己的软件包安装。它应该检测其他软件包安装。

0

Documentation说:“广播事件:一个新的应用程序包已经安装在设备上的数据包含了包的名称注意,新安装包不会收到此广播。”

+1

我认为这意味着新安装的软件包在安装时不会收到安装广播。我试图在我的软件包安装后检测其他软件包的安装。他们的措辞在我看来似乎有点不清楚,但我确实得到了它的工作,所以这是我对这个意义的最好猜测。 – Lucas