简体   繁体   English

Android获取有关安装或删除的应用程序的广播

[英]Android get broadcast about the application installed or removed

I want to develop an app that can receive broadcast about the other apps being installed or removed. 我想开发一个应用程序,可以接收有关正在安装或删除的其他应用程序的广播。 So far, I tried the code below but, it only provides a broadcast event when an installation on deletion occurs, it does not provide info about the other apps beibg installed or removed. 到目前为止,我尝试了下面的代码,但是,它仅在删除安装时提供广播事件,它不提供有关安装或删除的其他应用程序beibg的信息。 So, is there a way to get the package name of the newly installed application. 那么,有没有办法获取新安装的应用程序的包名称。

in manifset: 在表现:

receiver android:name=".apps.AppListener">
    <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_REMOVED"/>
         <data android:scheme="package"/> 
    </intent-filter>
</receiver>

in AppListener: 在AppListener中:

public class AppListener extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
      // there is a broadcast event here
      // but how to get the package name of the newly installed application 
      Log.v(TAG, "there is a broadcast");
    }
}

Addition: This is deprecated for Api 14 or upper. 增加:对于Api 14或更高版本,不推荐使用此功能。

     <action android:name="android.intent.action.PACKAGE_INSTALL"/> 

Package name is embedded into the Intent you are receiving in onReceive() method. 包名称嵌入到您在onReceive()方法中接收的Intent中。 You can read it using below code snippet : 您可以使用下面的代码段阅读它:

Uri data = broadcastIntent.getData();
String installedPackageName = data.getEncodedSchemeSpecificPart();

For PACKAGE_ADDED, PACKAGE_REMOVED and PACKAGE_REPLACED, you can get package name using above code. 对于PACKAGE_ADDED,PACKAGE_REMOVED和PACKAGE_REPLACED,您可以使用上面的代码获取包名称。

In case of application update, you will get 2 broadcasts back to back as below : 1. PACKAGE_REMOVED 2. PACKAGE_REPLACED 如果应用程序更新,您将获得2个背靠背广播,如下所示:1。PACKAGE_REMOVED 2. PACKAGE_REPLACED

In case of application update, PACKAGE_REMOVED intent will contain extra boolean to differentiate between app removal and app update. 在应用程序更新的情况下,PACKAGE_REMOVED意图将包含额外的布尔值,以区分应用程序删除和应用程序更新。 You can read this boolean as below : 您可以读取此布尔值,如下所示:

boolean isReplacing = broadcastIntent.getBooleanExtra(Intent.EXTRA_REPLACING, false);

Just to get package name you are calling PackageManagerService api is overhead. 只是为了得到包名称,你正在调用PackageManagerService api是开销。 Must avoid it. 必须避免它。

Hope, this will help you. 希望这个能对您有所帮助。

There are 2 options: 有两种选择:

1) If you are asking if you can receive a broadcast when your same app is being uninstalled, then the answer is: 1)如果您在询问是否可以在卸载相同的应用程序时收到广播,则答案是:

This cannot be done, unless you are a System app. 除非您是System应用程序,否则无法执行此操作。

Android does not notify the app when it is going to be installed. Android将在安装时通知应用程序。 This would be a security risk, as it would enable apps to prevent uninstallation. 这将是一个安全风险,因为它将使应用程序能够防止卸载。

2) If you are asking about when other apps are being uninstalled, then this might be a duplicate of: 2)如果您询问何时卸载其他应用程序,则可能与以下内容重复:

Intent that you get in onReceive function contains the information related to the package being added or removed. 您在onReceive函数中获得的意图包含与要添加或删除的包相关的信息。

intent.getData().toString()

You can get the application name by this function: 您可以通过此函数获取应用程序名称:

  private String getApplicationName(Context context, String data, int flag) {

        final PackageManager pckManager = context.getPackageManager();
        ApplicationInfo applicationInformation;
        try {
            applicationInformation = pckManager.getApplicationInfo(data, flag);
        } catch (PackageManager.NameNotFoundException e) {
            applicationInformation = null;
        }
        final String applicationName = (String) (applicationInformation != null ? pckManager.getApplicationLabel(applicationInformation) : "(unknown)");

        return applicationName;
    }

For more info check: Created BroadcastReceiver which displays application name and version number on install/ uninstall of any application? 有关详细信息,请查看: 创建的BroadcastReceiver,在安装/卸载任何应用程序时显示应用程序名称和版本号?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM