[英]How to create app shortcut inside a Custom launcher in Android?
我目前正在开发一个应用程序,可以创建设备的所有已安装应用程序的快捷方式。 为了创建快捷方式,用户需要以管理员身份登录,并且从该活动中,有一个包含所有已安装应用程序的ListView。 用户需要选择app / s以便创建快捷方式。
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;
}
选择应用程序后,它将在Main Activity中创建一个具有GridView的快捷方式。 这里的问题是创建的快捷方式是在主屏幕中创建的。 现在我的问题是,如何在主屏幕中创建我的应用程序(MainActivity.java)中的快捷方式?
你试过了吗?
<activity android:name=".YourActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
然后在接收意图的活动中,为快捷方式创建意图并将其作为活动结果返回。
// create shortcut if requested
ShortcutIconResource icon =
Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
Intent intent = new Intent();
Intent launchIntent = new Intent(this,ActivityToLaunch.class);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, someNickname());
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
setResult(RESULT_OK, intent);
可以在应用程序中显示将启动另一个应用程序的图标。 您可以使用PackageManager
来发现已安装的应用程序并获取应显示的启动器图标。
从应用程序的上下文中获取PackageManager
。
PackageManager pm = context.getPackageManager();
创建一个intent,用于查询已存在并将在启动器中显示的已安装应用程序。
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
查询其IntentFilter与mainIntent
匹配的任何应用程序。
List<ResolveInfo> apps = pm.queryIntentActivities(mainIntent, 0);
迭代返回的ResolveInfo
列表,以获取创建用于启动应用程序的Intent和要显示为启动器图标的资源所需的详细信息。
for (ResolveInfo app : apps) { // Create an Intent that can be use to launch the application Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setComponent(new ComponentName(app.activityInfo.applicationInfo.packageName, app.activityInfo.name)); // Get the icon to display in your UI Drawable icon = app.activityInfo.loadIcon(pm); }
更新:你可以在这里找到一个创建自定义启动器的步骤: http : //arnab.ch/blog/2013/08/how-to-write-custom-launcher-app-in-android/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.