创建的 BroadcastReceiver 在安装/卸载任何应用程序时显示应用程序名称和版本号。但我正在通过 intent.getData()
获取包名。但是当我尝试使用 packagemanager 查找该应用程序的名称时,它在所有安装/卸载/替换的情况下都会抛出异常。可能的问题是什么?如何解决?
代码:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.widget.Toast;
public class ApplicationStatusNotification extends BroadcastReceiver {
/**
* This method receives message for any application status(Install/ Uninstall) and display details.
*/
@Override
public void onReceive(Context context, Intent intent) {
// Get application status(Install/ Uninstall)
boolean applicationStatus = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
String toastMessage = null;
// Check if the application is install or uninstall and display the message accordingly
if(intent.getAction().equals("android.intent.action.PACKAGE_INSTALL")){
// Application Install
toastMessage = "PACKAGE_INSTALL: "+ intent.getData().toString() + getApplicationName(context, intent.getData().toString(), PackageManager.GET_UNINSTALLED_PACKAGES);
}else if(intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")){
// Application Uninstall
toastMessage = "PACKAGE_REMOVED: "+ intent.getData().toString() + getApplicationName(context, intent.getData().toString(), PackageManager.GET_UNINSTALLED_PACKAGES);
}else if(intent.getAction().equals("android.intent.action.PACKAGE_REPLACED")){
// Application Replaced
toastMessage = "PACKAGE_REPLACED: "+ intent.getData().toString() + getApplicationName(context, intent.getData().toString(), PackageManager.GET_UNINSTALLED_PACKAGES);
}
//Display Toast Message
if(toastMessage != null){
Toast.makeText(context, toastMessage, Toast.LENGTH_LONG).show();
}
}
/**
* This method get application name name from application package name
*/
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;
}
}
最佳答案
我关注了this引入BroadcastReceiver的例子如下;
<receiver android:name="PackageChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
现在一旦 PackageChangeReceiver.onReceive(..) 被调用,Intent.getData() Uri 包含一些东西; package:my.test.package
由 Uri.toString() 返回。要使用 PackageManager 搜索此 ApplicationInfo,您应该只提取可以通过 Uri.getSchemeSpecificPart()
检索的包名称,它应该只给您 my.test.package
。
此外,根据快速测试,似乎很可能在删除包后不再有可用的 ApplicationInfo。
关于android - 创建的 BroadcastReceiver 在安装/卸载任何应用程序时显示应用程序名称和版本号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8680874/