停止BroadCastReceiver在Android中触发应用程序更新

时间:2017-08-04 10:08:52

标签: android broadcastreceiver android-broadcast

我必须在设备中安装或卸载新应用程序时显示提示,到目前为止它的工作正常。唯一的问题是,即使应用程序更新,提示即将到来。 如何阻止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));
}

3 个答案:

答案 0 :(得分:1)

试试这个

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
}

答案 1 :(得分:0)

尝试将if条件更改为

public function __construct()
{
   //.. others
   AttributeRepositoryInterface $attributeRepository,
   EntityManager $em
}

并显示

if (!intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED) &&
       (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));
}

答案 2 :(得分:0)

应用程序更新正在重新安装新的应用程序,因此您的接收方收到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);
}