我目前正在开发一个应用程序,可以创建设备的所有安装应用程序的快捷方式。为了创建快捷方式,用户需要以管理员身份登录,并从该活动登录,其中包含一个ListView,其中包含所有已安装的应用程序。用户需要选择app/s,以便创建快捷方式。如何在Android的自定义启动器中创建应用快捷方式?
private boolean Generate_Shortcut(Context context, String appName, boolean isTrue) {
boolean flag =false ;
int app_id=-1;
PackageManager pm = context.getPackageManager();
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> res = pm.queryIntentActivities(i,0);
for(int k=0; k<res.size(); k++) {
if(res.get(k).activityInfo.loadLabel(pm).toString().equals(appName)){
flag = true;
app_id = k;
break;
}
}
if(flag) {
ActivityInfo ai = res.get(app_id).activityInfo;
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(ai.packageName, ai.name);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);
intent.putExtra("duplicate", false);
if(isTrue) {
// CREATE SHORTCUT
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher));
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
} else {
// REMOVE SHORTCUT
intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
}
context.sendBroadcast(intent);
} else
System.out.println("applicaton not found");
return true;
}
选择应用程序后,它将在主活动中创建一个具有GridView的快捷方式。这里的问题是创建的快捷方式是在主屏幕中创建的。现在我的问题是,如何在我的应用程序(MainActivity.java)中创建一个快捷方式,而不是在主屏幕中?
请您详细说明“在当前正在开发的应用程序中创建一个快捷方式”吗?你想创造一种发射器吗? –
@AndrewT。,是的,这就是我想要做的。我想在我的应用程序内创建一个启动器。 – androidBoomer
由于快捷方式位于您自己的应用程序内,因此您可以根据自己的需要进行操作。只需存储您已经显示给用户的信息,然后使用它。 –