Android 用java代码在应用列表添加一个带有图片的应用
时间: 2024-05-08 19:19:53 浏览: 10
可以使用以下代码实现:
1. 首先,在AndroidManifest.xml中添加以下权限:
<uses-permission android:name="android.permission.INSTALL_SHORTCUT"/>
2. 在java代码中使用以下方法创建快捷方式:
```
private void createShortcut(){
Intent shortcutIntent = new Intent(getApplicationContext(),
MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.app_name));
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(
getApplicationContext(), R.mipmap.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
```
3. 在MainActivity.java的onCreate()方法中添加以下代码:
createShortcut();
这样就可以在应用列表中创建一个带有应用图标的快捷方式了。