在Android中的自定义启动器中创建应用程序快捷方式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在Android中的自定义启动器中创建应用程序快捷方式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2620字,纯文字阅读大概需要4分钟。
内容图文
我正在尝试开发一个自定义启动器,它将包含我安装的应用程序的快捷方式(甚至是apk文件).我可以在应用程序外部创建一个快捷方式,我需要做的是在我的启动器活动中创建它.我怎样才能做到这一点?
AdminActivity.java(此活动包含一个能够从设备创建已安装应用程序快捷方式的方法)
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(这是Launcher 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" />
解决方法:
你正在创建的不是像主屏幕那样的启动器,它只是一个应用程序.启动器类别是指所有应用程序抽屉中列出的活动.我不认为应用程序可以添加自己的快捷方式,否则垃圾邮件可能会像这样继续将其图标添加到主屏幕并使每个用户烦恼.
另外installUninstall_ShortCut不是我们用Java命名的东西!实际上它与我遇到过的任何命名约定都不符
如果您想模仿类似添加快捷方式的功能,那么您可以获取网格视图并在其上调用addView
内容总结
以上是互联网集市为您收集整理的在Android中的自定义启动器中创建应用程序快捷方式全部内容,希望文章能够帮你解决在Android中的自定义启动器中创建应用程序快捷方式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。