获取最近安装的包的图标
Get icon of the package that is recently installed
我收到了最近通过实施 broadcastreceiver
的 onReceive()
添加的包的名称和其他详细信息,但我不知道如何获取该包的图标。
我无法在 brodcast Receiver
中获得 packageManager()
是否可以从 intent 获取图标??
public class PackageActionsReceiver extends BroadcastReceiver {
private static final String TAG = PackageActionsReceiver.class
.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
if (UtilSharedPreference.isRegistered(context)) {
Log.d(TAG, "onReceiver");
Uri data = intent.getData();
Package packageBean = new Package();
Drawable tempDrawableImage;
packageBean.setPackageName(data.getEncodedSchemeSpecificPart());
if (intent.getAction().equals(
Intent.ACTION_PACKAGE_ADDED.toString())) {
packageBean.setInfo("I");
} else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
packageBean.setInfo("U");
} else if (intent.getAction()
.equals(Intent.ACTION_PACKAGE_REPLACED)) {
packageBean.setInfo("R");
}
packageBean.setName(Utils.getApplicationName(context, intent
.getData().getEncodedSchemeSpecificPart(),
PackageManager.GET_UNINSTALLED_PACKAGES));
packageBean.setDate(System.currentTimeMillis());
PackageDataSource packageSource = new PackageDataSource(context);
packageSource.open();
packageSource.addNewPackage(packageBean);
packageSource.close();
}
}
}
您可以从 onReceive 方法的上下文中获取 packageManager
例如
public void onReceive(Context context, Intent intent) {
context.getPackageManager()
}
你可以使用这个答案
Drawable icon = context.getPackageManager().getApplicationIcon("com.example.testnotification");
imageView.setImageDrawable(icon);
来自 this answer
我收到了最近通过实施 broadcastreceiver
的 onReceive()
添加的包的名称和其他详细信息,但我不知道如何获取该包的图标。
我无法在 brodcast Receiver
packageManager()
是否可以从 intent 获取图标??
public class PackageActionsReceiver extends BroadcastReceiver {
private static final String TAG = PackageActionsReceiver.class
.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
if (UtilSharedPreference.isRegistered(context)) {
Log.d(TAG, "onReceiver");
Uri data = intent.getData();
Package packageBean = new Package();
Drawable tempDrawableImage;
packageBean.setPackageName(data.getEncodedSchemeSpecificPart());
if (intent.getAction().equals(
Intent.ACTION_PACKAGE_ADDED.toString())) {
packageBean.setInfo("I");
} else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
packageBean.setInfo("U");
} else if (intent.getAction()
.equals(Intent.ACTION_PACKAGE_REPLACED)) {
packageBean.setInfo("R");
}
packageBean.setName(Utils.getApplicationName(context, intent
.getData().getEncodedSchemeSpecificPart(),
PackageManager.GET_UNINSTALLED_PACKAGES));
packageBean.setDate(System.currentTimeMillis());
PackageDataSource packageSource = new PackageDataSource(context);
packageSource.open();
packageSource.addNewPackage(packageBean);
packageSource.close();
}
}
}
您可以从 onReceive 方法的上下文中获取 packageManager
例如
public void onReceive(Context context, Intent intent) {
context.getPackageManager()
}
你可以使用这个答案
Drawable icon = context.getPackageManager().getApplicationIcon("com.example.testnotification");
imageView.setImageDrawable(icon);
来自 this answer