我有一个应用程序,它保存设备上安装的内部开发应用程序的日志.安装后,将调用Intent.PACKAGE_ADDED的广播接收器,并使用以下代码记录包名称:
public class NewInstallReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Bundle b = intent.getExtras();
int uid = b.getInt(Intent.EXTRA_UID);
String[] packages = context.getPackageManager().getPackagesForUid(uid);
ApplicationService appService = new ApplicationService(context);
appService.ApplicationInstalled(packages);
}
}
Run Code Online (Sandbox Code Playgroud)
我面临的问题是当使用Intent.PACKAGE_REMOVED的广播接收器时,通过唯一ID(UID)对包的所有引用都会返回空信息(如您已经卸载的那样,正如您所期望的那样).我有一个临时解决方案,但它不是很优雅,对于下一个版本,我希望有更清晰的代码.代码应如何工作的示例:
public class RemoveApplicationReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Bundle b = intent.getExtras();
int uid = b.getInt(Intent.EXTRA_UID);
String[] packages = context.getPackageManager().getPackagesForUid(uid);
ApplicationService appService = new ApplicationService(context);
appService.ApplicationRemoved(packages);
}
}
Run Code Online (Sandbox Code Playgroud)
所以回顾一下,问题是:
删除程序后,如何在广播接收器中为Intent.PACKAGE_REMOVED引用包名称.
谢谢
创建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: …Run Code Online (Sandbox Code Playgroud)