2015-02-06 116 views
1

我想知道用户何时安装新应用程序。我用过广播接收器。Android,当用户安装应用程序?

<receiver android:name=".NewInstallReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.package_added" /> 
     <data android:scheme="package" /> 
    </intent-filter> 
</receiver> 

代码:

public class NewInstallReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String act = intent.getAction(); 
     Log.e("ee", "new install" + intent); 
     if (Intent.ACTION_PACKAGE_ADDED.equals(act) || Intent.ACTION_PACKAGE_REMOVED.equals(act)) { 
      /* ContentResolver contentResolver = context.getContentResolver(); 
      contentResolver.query()*/ 
     } 
    } 
} 

我看不出有任何日志时,我从Play商店中安装应用。

我应该使用服务吗?如果是的话,我该如何让它无限次地运行?

+0

是啊,你太? – 2015-02-06 09:25:42

回答

2

我想有一个错字在你的意图过滤器,请尝试以下意图过滤

<receiver android:name=".NewInstallReceiver"> 
     <intent-filter>    
     <action android:name="android.intent.action.PACKAGE_ADDED"/> 
     <action android:name="android.intent.action.PACKAGE_INSTALL"/> 
     <data android:scheme="package" /> 
    </intent-filter> 
</receiver> 
+0

谢谢,它的工作 – 2015-02-06 09:59:49

+0

当然,作为一个附注,我相信Eclipse/Android工作室会自动使用XML和Java完成这些代码。 Ctrl + Space应该给你选择什么选择,而编辑是最好的,然后手动输入!谢谢! – AADProgramming 2015-02-06 10:05:49

+0

我相信我也是这样做的。 – 2015-02-06 10:06:31