Đã tạo BroadcastReceiver hiển thị tên ứng dụng và số phiên bản khi cài đặt/gỡ cài đặt bất kỳ ứng dụng nào. Nhưng tôi nhận được tên gói thông qua intent.getData()
. Nhưng khi tôi đang cố gắng tìm tên của ứng dụng đó bằng cách sử dụng packagemanager nó ném một ngoại lệ trong tất cả các trường hợp Install/Uninstall/Replaced. Điều gì có thể là vấn đề có thể xảy ra và làm thế nào điều này có thể được sửa?Đã tạo BroadcastReceiver hiển thị tên ứng dụng và số phiên bản khi cài đặt/gỡ cài đặt bất kỳ ứng dụng nào?
Mã sản phẩm:
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;
}
}
Có một điều bạn có thể muốn thay đổi, thay vì nghe Intent.ACTION_PACKAGE_INSTALL, bạn có thể đang tìm Intent.ACTION_PACKAGE_ADDED. Dựa trên tài liệu đầu tiên nên không bao giờ được sử dụng và nó khá ngạc nhiên nếu bạn đã nhận được hành động như vậy. – harism
Có nhưng vẫn pckManager.getApplicationInfo (packageName, flag) đang ném ngoại lệ NameNotFoundException. –
Bạn có thể thêm vào phương thức onReceive Log.d ("MyApp", "Intent intent:" + intent); và thêm thông điệp logcat? – Yury