我创建了一个应用程序,用于侦听手机上最后安装的应用程序,并在安装新应用程序时通知我。现在它正确通知我最后安装的应用程序,但它也会在任何应用程序更新时通知我。 是否有人知道如何听取上次安装的应用,而不是在应用进行更新时收听?
这是我的代码:
public class NewInstallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("tag_name", "NewInstallReceiver" + intent.getAction());
final PackageManager pm = context.getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo(intent.getData().getSchemeSpecificPart(), 0);
Log.d("tag_name", "Application Info" + ai);
} catch (final PackageManager.NameNotFoundException e) {
ai = null;
}
List<PackageInfo> packages = pm.getInstalledPackages(PackageManager.GET_META_DATA);
Collections.sort(packages, new Comparator<PackageInfo>() {
@Override
public int compare(PackageInfo p1, PackageInfo p2) {
return Long.toString(p2.firstInstallTime).compareTo(Long.toString(p1.firstInstallTime));
}
});
}
}
我的广播接收器意图过滤器
<receiver android:name="com.example.natalie.listener.NewInstallReceiver">
<intent-filter android:priority="100">
<action
android:name="android.intent.action.PACKAGE_INSTALL"/>
<action
android:name="android.intent.action.PACKAGE_ADDED"/>
<action
android:name="android.intent.action.PACKAGE_DATA_CLEARED"/>
<action
android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
答案 0 :(得分:0)
使用此代码,您可以看到手机上安装了应用程序:
PackageManager pm = context.getPackageManager();
ApplicationInfo appInfo = pm.getApplicationInfo("application package name", 0);
String appFile = appInfo.sourceDir;
long installed = new File(appFile).lastModified();