创建自定义启动器在Android的应用程序内的快捷方式 [英] Create app shortcut inside the Custom Launcher in Android
问题描述
我试图建立一个自定义启动,它将包含我安装的应用程序(甚至是APK文件)的快捷方式。我可以创建应用程序之外的快捷方式但什么,我需要做的是在我的发射活动创建它。我该怎么办呢?
AdminActivity.java (此活动包含具有创建从设备中安装的应用程序的快捷方式的能力的方法)
私人布尔installUninstall_ShortCut(上下文的背景下,字符串的appName,布尔IsTrue运算){
布尔标志= FALSE;
INT APP_ID = -1;
PackageManager下午= context.getPackageManager();
意图I =新的意图(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
名单< ResolveInfo>解析度= pm.queryIntentActivities(ⅰ,0);
对于(INT K = 0; K< res.size(); k ++){
如果(res.get(K).activityInfo.loadLabel(PM)的ToString()。等于(的appName)){
标志=真正的;
APP_ID = K;
打破;
}
}
如果(旗){
ActivityInfo AI = res.get(APP_ID).activityInfo;
意图shortcutIntent =新的意图(Intent.ACTION_MAIN);
shortcutIntent.setClassName(ai.packageName,ai.name);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
意向意图=新的意图();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,的appName);
intent.putExtra(重复,假);
如果(IsTrue运算){
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(上下文,R.drawable.ic_launcher));
intent.setAction(com.android.launcher.action.INSTALL_SHORTCUT);
} 其他 {
intent.setAction(com.android.launcher.action.UNINSTALL_SHORTCUT);
}
context.sendBroadcast(意向);
} 其他
的System.out.println(的一个应用没有找到);
返回true;
}
activity_launcher.xml (这是发射活动的布局,其中的快捷方式会去的。我真的不知道如何把创建的快捷方式本次活动)
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=@可绘制/ plain_without_logo
工具:上下文=S_2nd_MainActivity。>
< GridView控件
机器人:ID =@ + ID / grid_app
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:columnWidth中=80dp
机器人:重力=中心
机器人:horizontalSpacing =15dp
机器人:为numColumns =auto_fit
机器人:填充=10dp
机器人:stretchMode =columnWidth中
机器人:verticalSpacing =15dp/>
你所创建并不像主屏幕,它只是一个应用程序启动器。该发射器类是指活动被列在所有的应用程序抽屉。我不认为应用程序可以添加自己的快捷方式,否则垃圾邮件洁具也只是不断增加它的图标,这样您的主屏幕,并惹恼每个用户。
此外installUninstall_ShortCut是不是我们如何命名的东西在Java中!其实这不符合我曾经遇到
任何命名约定如果你想模仿加入-快捷键一样的功能,那么你可以得到网格视图,并呼吁addView就可以了。
I'm trying to develop a custom launcher that will contain the shortcut of my installed application (or even apk files). I can create a shortcut BUT outside the application and what I need to do is to create it inside my launcher activity. How can I do that?
AdminActivity.java (This activity contains a method that has the ability to create shortcut of the installed app from the device)
private boolean installUninstall_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) {
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher));
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
} else {
intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
}
context.sendBroadcast(intent);
} else
System.out.println("applicaton not found");
return true;
}
activity_launcher.xml (This is the Layout of Launcher Activity where the shortcut will go. I really don't know how to bring the created shortcut in this activity)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/plain_without_logo"
tools:context=".S_2nd_MainActivity" >
<GridView
android:id="@+id/grid_app"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="80dp"
android:gravity="center"
android:horizontalSpacing="15dp"
android:numColumns="auto_fit"
android:padding="10dp"
android:stretchMode="columnWidth"
android:verticalSpacing="15dp" />
What you are creating is not a launcher like the home screen, its just an application. The launcher category refers to the activity being listed in the all apps drawer. I don't think applications can add their own shortcuts otherwise spam ware could just keep adding its icon to your home screen like this and annoy every user.
Also installUninstall_ShortCut is not how we name things in Java! Actually it doesn't match any naming convention I've ever come across
If you want to mimic adding-shortcut-like functionality then you can get the grid view and call addView on it
这篇关于创建自定义启动器在Android的应用程序内的快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!