阻止 BroadCastReceiver 在 Android 中触发应用程序更新
Stop BroadCastReceiver from triggering on Application update in Android
我必须在设备中安装或卸载新应用程序时显示提示,到目前为止它工作正常。唯一的问题是即使更新应用程序也会提示。
如何阻止 BroadCastReceiver
触发应用程序更新。
<receiver android:name=".WeepingReceiver">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
广播
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_PACKAGE_INSTALL)
|| intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
context.startActivity(new Intent(context, NewAppActivity.class).
setFlags(Intent.FLAG_ACTIVITY_NEW_TASK).putExtra(Utility.NEW_PACKAGE_NAME, packageName));
}
只需尝试将您的 if 条件更改为
if (!intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED) &&
(intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED))) {
context.startActivity(new Intent(context, NewAppActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(Utility.NEW_PACKAGE_NAME, packageName));
}
并显示
<receiver android:name=".WeepingReceiver">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
Application Update 正在重新安装一个新的应用程序,因此您的接收者收到事件 PACKAGE_ADDED
是正确的。因此,您无法阻止广播接收事件。
但是,您可以通过检查包名称之前是否存在来验证意图是否正在更新。您可以拥有已安装应用程序包名称和商店的列表。然后检查你在做什么:
if (intent.getAction().equals(Intent.ACTION_PACKAGE_INSTALL)
|| intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)
|| !packageList.contains(packageName))
You could get your package list 来自:
final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
packageList.add(packageInfo.packageName);
}
试试这个
Bundle extras = intent.getExtras();
if (extras.containsKey(Intent.EXTRA_REPLACING) && extras.getBoolean(Intent.EXTRA_REPLACING))
{
//do nothing here it is condition of updating your existing app
}else
{
//do your code here
}
我必须在设备中安装或卸载新应用程序时显示提示,到目前为止它工作正常。唯一的问题是即使更新应用程序也会提示。
如何阻止 BroadCastReceiver
触发应用程序更新。
<receiver android:name=".WeepingReceiver">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
广播
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_PACKAGE_INSTALL)
|| intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
context.startActivity(new Intent(context, NewAppActivity.class).
setFlags(Intent.FLAG_ACTIVITY_NEW_TASK).putExtra(Utility.NEW_PACKAGE_NAME, packageName));
}
只需尝试将您的 if 条件更改为
if (!intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED) &&
(intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED))) {
context.startActivity(new Intent(context, NewAppActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(Utility.NEW_PACKAGE_NAME, packageName));
}
并显示
<receiver android:name=".WeepingReceiver">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
Application Update 正在重新安装一个新的应用程序,因此您的接收者收到事件 PACKAGE_ADDED
是正确的。因此,您无法阻止广播接收事件。
但是,您可以通过检查包名称之前是否存在来验证意图是否正在更新。您可以拥有已安装应用程序包名称和商店的列表。然后检查你在做什么:
if (intent.getAction().equals(Intent.ACTION_PACKAGE_INSTALL)
|| intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)
|| !packageList.contains(packageName))
You could get your package list 来自:
final PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
packageList.add(packageInfo.packageName);
}
试试这个
Bundle extras = intent.getExtras();
if (extras.containsKey(Intent.EXTRA_REPLACING) && extras.getBoolean(Intent.EXTRA_REPLACING))
{
//do nothing here it is condition of updating your existing app
}else
{
//do your code here
}