如果您想了解Android应用创建手机桌面快捷方式的相关知识,那么本文是一篇不可错过的文章,我们将对android应用创建手机桌面快捷方式怎么设置进行全面详尽的解释,并且为您提供关于AndroidLa
如果您想了解Android应用创建手机桌面快捷方式的相关知识,那么本文是一篇不可错过的文章,我们将对android应用创建手机桌面快捷方式怎么设置进行全面详尽的解释,并且为您提供关于Android Launcher 开发之自动添加桌面快捷方式及实际开发中常见问题的解决方案、Android O添加桌面快捷方式、Android O添加桌面快捷方式的示例、android – 为桌面上的任何应用程序创建快捷方式的有价值的信息。
本文目录一览:- Android应用创建手机桌面快捷方式(android应用创建手机桌面快捷方式怎么设置)
- Android Launcher 开发之自动添加桌面快捷方式及实际开发中常见问题的解决方案
- Android O添加桌面快捷方式
- Android O添加桌面快捷方式的示例
- android – 为桌面上的任何应用程序创建快捷方式
Android应用创建手机桌面快捷方式(android应用创建手机桌面快捷方式怎么设置)
下面是小编 jb51.cc 通过网络收集整理的代码片段。
小编小编现在分享给大家,也给大家做个参考。
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/shortCut_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="快捷方式"
/>
<Button
android:id="@+id/exit_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="退出应用" />
</LinearLayout>
--------------------------------------------------------------------------- public class MainActivity extends Activity { private Button shortCutBtn; private Button exitBtn; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); shortCutBtn = (Button) findViewById(R.id.shortCut_btn); exitBtn = (Button) findViewById(R.id.exit_btn); /**创建 快捷方式*/ shortCutBtn.setonClickListener(new OnClickListener() { public void onClick(View v) { /**创建或删除ShortCut的Intent中设置Action为:com.android.launcher.action.INSTALL_SHORTCUT * 另外删除快捷方式为:com.android.launcher.action.UNINSTALL_SHORTCUT */ Intent addIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); /**Android专门提供了Intent.ShortcutIconResource.fromContext来创建快捷方式的图标,最后通过setResult来返回*/ Parcelable icon = Intent.ShortcutIconResource.fromContext(MainActivity.this,R.drawable.ic_launcher); Intent myIntent = new Intent(MainActivity.this,MainActivity.class); /**应用名称*/ addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"应用名称"); /**应用图片*/ addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon); /**应用启动Intent*/ addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,myIntent); sendbroadcast(addIntent); } }); /**退出应用*/ exitBtn.setonClickListener(new OnClickListener() { public void onClick(View arg0) { System.exit(0); } }); } }
以上是小编(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给程序员好友。
Android Launcher 开发之自动添加桌面快捷方式及实际开发中常见问题的解决方案
最近做到的应用做刚好需要添加快捷方式的功能,在参考了源代码和网上一些其他资料后做了出来.
在做的时候遇到两个问题,
一. 程序卸载后桌面快捷方式仍然存在:
关于此问题,网上的资料和实际中很多应用程序的老版本或者当前版本仍存在。参考源代码后,我找出了解决方案:创建 shortcut 时需要设置 Extre_ShortCut_Intent 的 action. 和 category, 使创建的 shortcut 与自己的应用产生绑定的关系:
二。需要判断快捷方式是否存在,快捷方式的信息,在系统应用 launcher 中以 contentprovider 的形式提供数据.
需要从 com.android.launcher 的 launcher.db 的 favorites 表中读取快捷方式的信息
对于 lancher 的 provider 节点中的 AUTHORITY 属性,在 Android 属性中变成了 "com.android.launcher2.settings". 而 2.2 之前的版本为 "com.android.launcher.settings"
如果应用程序需要兼容 2.2 以下的机器,需要获取当前手机系统的版本号,更改 Uri 中的 AUTHORITY.
解决了如上两个问题,该功能就完善了,需要的朋友可以直接加到要上线的应用中.
效果如图;
全部代码如下,已经加入了详细的注解:
1. AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.android.autoLancher"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk android:minSdkVersion="8" />
- <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
- <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
- <!-- 快捷方式信息需要从 setting 中读取 -->
- <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <activity
- android:name=".LauncherDemo2Activity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- <pre name="code" class="java"> <!- 如果想让应用出现在长按 Menu 键选择添加快捷方式中,则需要加入以下代码 ->
- <intent-filter>
- </pre> <action android:name="android.intent.action.CREATE_SHORTCUT"></action> </intent-filter> </activity> </application></manifest>
- <pre></pre>
- <p></p>
- <p></p>
- <p></p>
- <p></p>
- <pre></pre>
- 2. LauncherDemo2Activity:
- <p></p>
- <p></p>
- <p></p>
- <pre name="code" class="java">package com.android.autoLancher;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.AlertDialog.Builder;
- import android.content.ContentResolver;
- import android.content.DialogInterface;
- import android.content.DialogInterface.OnClickListener;
- import android.content.Intent;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- public class LauncherDemo2Activity extends Activity {
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- boolean flag =isShortcutInstalled();// 如果已经创建,则不需要在创建
- if(flag==false){
- AlertDialog.Builder builder = new Builder(LauncherDemo2Activity.this);
- builder.setTitle("是否为此应用创建桌面快捷方式");
- builder.setPositiveButton("是", new OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- addShortcutToDesktop();
- }
- });
- builder.setNegativeButton("否", new OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- }
- });
- builder.create().show();
- }
- }
- private void addShortcutToDesktop() {
- Intent shortcut = new Intent(
- "com.android.launcher.action.INSTALL_SHORTCUT");
- // 不允许重建
- shortcut.putExtra("duplicate", false);
- // 设置名字
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "加菲快捷");
- // 设置图标
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
- Intent.ShortcutIconResource.fromContext(this,
- R.drawable.cat));
- // 设置意图和快捷方式关联程序
- Intent intent = new Intent(this, this.getClass());
- // 桌面图标和应用绑定,卸载应用后系统会同时自动删除图标
- intent.setAction("android.intent.action.MAIN");
- intent.addCategory("android.intent.category.LAUNCHER");
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
- // 发送广播
- sendBroadcast(shortcut);
- }
- /**
- * 快捷方式信息是保存在 com.android.launcher 的 launcher.db 的 favorites 表中
- *
- * @return
- */
- public boolean isShortcutInstalled() {
- boolean isInstallShortcut = false;
- final ContentResolver cr = LauncherDemo2Activity.this
- .getContentResolver();
- // 2.2 系统是”com.android.launcher2.settings”, 网上见其他的为 "com.android.launcher.settings"
- String AUTHORITY = null;
- /*
- * 根据版本号设置 Uri 的 AUTHORITY
- */
- if(getSystemVersion()>=8){
- AUTHORITY = "com.android.launcher2.settings";
- }else{
- AUTHORITY = "com.android.launcher.settings";
- }
- Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
- + "/favorites?notify=true");
- Cursor c = cr.query(CONTENT_URI,
- new String[] { "title", "iconResource" }, "title=?",
- new String[] { getString(R.string.app_name) }, null);// XXX 表示应用名称。
- if (c != null && c.getCount() > 0) {
- isInstallShortcut = true;
- System.out.println("已创建");
- }
- return isInstallShortcut;
- }
- /**
- * 获取系统的 SDK 版本号
- * @return
- */
- private int getSystemVersion(){
- return Build.VERSION.SDK_INT;
- }
- }
- </pre><br>
- <br>
- <p></p>
- <p><br>
- </p>
- <br>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.autoLancher" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/> <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/> <!-- 快捷方式信息需要从setting中读取 --> <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".LauncherDemo2Activity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>源码打印?
- <!-如果想让应用出现在长按Menu键选择添加快捷方式中,则需要加入以下代码->
- t;intent-filter>
<!-如果想让应用出现在长按Menu键选择添加快捷方式中,则需要加入以下代码-> <intent-filter><action android:name="android.intent.action.CREATE_SHORTCUT"></action> </intent-filter> </activity> </application></manifest> 2. LauncherDemo2Activity:源码打印?
- package com.android.autoLancher;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.AlertDialog.Builder;
- import android.content.ContentResolver;
- import android.content.DialogInterface;
- import android.content.DialogInterface.OnClickListener;
- import android.content.Intent;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- public class LauncherDemo2Activity extends Activity {
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- boolean flag =isShortcutInstalled();//如果已经创建,则不需要在创建
- if(flag==false){
- AlertDialog.Builder builder = new Builder(LauncherDemo2Activity.this);
- builder.setTitle("是否为此应用创建桌面快捷方式");
- builder.setPositiveButton("是", new OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- addShortcutToDesktop();
- }
- });
- builder.setNegativeButton("否", new OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- }
- });
- builder.create().show();
- }
- }
- private void addShortcutToDesktop() {
- Intent shortcut = new Intent(
- "com.android.launcher.action.INSTALL_SHORTCUT");
- // 不允许重建
- shortcut.putExtra("duplicate", false);
- // 设置名字
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "加菲快捷");
- // 设置图标
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
- Intent.ShortcutIconResource.fromContext(this,
- R.drawable.cat));
- // 设置意图和快捷方式关联程序
- Intent intent = new Intent(this, this.getClass());
- // 桌面图标和应用绑定,卸载应用后系统会同时自动删除图标
- intent.setAction("android.intent.action.MAIN");
- intent.addCategory("android.intent.category.LAUNCHER");
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
- // 发送广播
- sendBroadcast(shortcut);
- }
- /**
- * 快捷方式信息是保存在com.android.launcher的launcher.db的favorites表中
- *
- * @return
- */
- public boolean isShortcutInstalled() {
- boolean isInstallShortcut = false;
- final ContentResolver cr = LauncherDemo2Activity.this
- .getContentResolver();
- // 2.2系统是”com.android.launcher2.settings”,网上见其他的为"com.android.launcher.settings"
- String AUTHORITY = null;
- /*
- * 根据版本号设置Uri的AUTHORITY
- */
- if(getSystemVersion()>=8){
- AUTHORITY = "com.android.launcher2.settings";
- }else{
- AUTHORITY = "com.android.launcher.settings";
- }
- Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
- + "/favorites?notify=true");
- Cursor c = cr.query(CONTENT_URI,
- new String[] { "title", "iconResource" }, "title=?",
- new String[] { getString(R.string.app_name) }, null);// XXX表示应用名称。
- if (c != null && c.getCount() > 0) {
- isInstallShortcut = true;
- System.out.println("已创建");
- }
- return isInstallShortcut;
- }
- /**
- * 获取系统的SDK版本号
- * @return
- */
- private int getSystemVersion(){
- return Build.VERSION.SDK_INT;
- }
- }
package com.android.autoLancher; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.ContentResolver; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; public class LauncherDemo2Activity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); boolean flag =isShortcutInstalled();//如果已经创建,则不需要在创建 if(flag==false){ AlertDialog.Builder builder = new Builder(LauncherDemo2Activity.this); builder.setTitle("是否为此应用创建桌面快捷方式"); builder.setPositiveButton("是", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { addShortcutToDesktop(); } }); builder.setNegativeButton("否", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }); builder.create().show(); } } private void addShortcutToDesktop() { Intent shortcut = new Intent( "com.android.launcher.action.INSTALL_SHORTCUT"); // 不允许重建 shortcut.putExtra("duplicate", false); // 设置名字 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "加菲快捷"); // 设置图标 shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.cat)); // 设置意图和快捷方式关联程序 Intent intent = new Intent(this, this.getClass()); // 桌面图标和应用绑定,卸载应用后系统会同时自动删除图标 intent.setAction("android.intent.action.MAIN"); intent.addCategory("android.intent.category.LAUNCHER"); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); // 发送广播 sendBroadcast(shortcut); } /** * 快捷方式信息是保存在com.android.launcher的launcher.db的favorites表中 * * @return */ public boolean isShortcutInstalled() { boolean isInstallShortcut = false; final ContentResolver cr = LauncherDemo2Activity.this .getContentResolver(); // 2.2系统是”com.android.launcher2.settings”,网上见其他的为"com.android.launcher.settings" String AUTHORITY = null; /* * 根据版本号设置Uri的AUTHORITY */ if(getSystemVersion()>=8){ AUTHORITY = "com.android.launcher2.settings"; }else{ AUTHORITY = "com.android.launcher.settings"; } Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true"); Cursor c = cr.query(CONTENT_URI, new String[] { "title", "iconResource" }, "title=?", new String[] { getString(R.string.app_name) }, null);// XXX表示应用名称。 if (c != null && c.getCount() > 0) { isInstallShortcut = true; System.out.println("已创建"); } return isInstallShortcut; } /** * 获取系统的SDK版本号 * @return */ private int getSystemVersion(){ return Build.VERSION.SDK_INT; } }
原文链接: http://blog.csdn.net/t12x3456/article/details/7857835
Android O添加桌面快捷方式
手机升级到安卓O后,突然发现创建快捷方式的功能失效了,查询一番后发现:安卓O要使用ShortcutManager来创建快捷方式。
安卓N及以下版本:
Intent addShortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");//"com.android.launcher.action.INSTALL_SHORTCUT"
// 不允许重复创建
addShortcutIntent.putExtra("duplicate", false);// 经测试不是根据快捷方式的名字判断重复的
// 应该是根据快链的Intent来判断是否重复的,即Intent.EXTRA_SHORTCUT_INTENT字段的value
// 但是名称不同时,虽然有的手机系统会显示Toast提示重复,仍然会建立快链
// 屏幕上没有空间时会提示
// 注意:重复创建的行为MIUI和三星手机上不太一样,小米上似乎不能重复创建快捷方式
// 名字
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "网络设置");
// 图标
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_perm_data_setting_black_24dp));
// 设置关联程序
Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//设置网络页面intent
// 设置关联程序
// Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
// launcherIntent.setClass(MainActivity.this, MainActivity.class);
// launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);
// 发送广播
sendBroadcast(addShortcutIntent);
安卓O:
ShortcutManager scm = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);
Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//设置网络页面intent
ShortcutInfo si = new ShortcutInfo.Builder(this, "dataroam")
.setIcon(Icon.createWithResource(this, R.drawable.ic_perm_data_setting_black_24dp))
.setShortLabel("网络设置")
.setIntent(launcherIntent)
.build();
assert scm != null;
scm.requestPinShortcut(si, null);
那如果要两者兼顾呢,则可以如下这样写:
//添加快捷方式
private void addShortcut() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ShortcutManager scm = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);
Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//设置网络页面intent
ShortcutInfo si = new ShortcutInfo.Builder(this, "dataroam")
.setIcon(Icon.createWithResource(this, R.drawable.ic_perm_data_setting_black_24dp))
.setShortLabel("网络设置")
.setIntent(launcherIntent)
.build();
assert scm != null;
scm.requestPinShortcut(si, null);
} else {
Intent addShortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");//"com.android.launcher.action.INSTALL_SHORTCUT"
// 不允许重复创建
addShortcutIntent.putExtra("duplicate", false);// 经测试不是根据快捷方式的名字判断重复的
// 应该是根据快链的Intent来判断是否重复的,即Intent.EXTRA_SHORTCUT_INTENT字段的value
// 但是名称不同时,虽然有的手机系统会显示Toast提示重复,仍然会建立快链
// 屏幕上没有空间时会提示
// 注意:重复创建的行为MIUI和三星手机上不太一样,小米上似乎不能重复创建快捷方式
// 名字
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "网络设置");
// 图标
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_perm_data_setting_black_24dp));
// 设置关联程序
Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//设置网络页面intent
// 设置关联程序
// Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
// launcherIntent.setClass(MainActivity.this, MainActivity.class);
// launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);
// 发送广播
sendBroadcast(addShortcutIntent);
}
}
Android O添加桌面快捷方式的示例
手机升级到安卓O后,突然发现创建快捷方式的功能失效了,查询一番后发现:安卓O要使用ShortcutManager来创建快捷方式。
安卓N及以下版本:
Intent addShortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");//"com.android.launcher.action.INSTALL_SHORTCUT" // 不允许重复创建 addShortcutIntent.putExtra("duplicate",false);// 经测试不是根据快捷方式的名字判断重复的 // 应该是根据快链的Intent来判断是否重复的,即Intent.EXTRA_SHORTCUT_INTENT字段的value // 但是名称不同时,虽然有的手机系统会显示Toast提示重复,仍然会建立快链 // 屏幕上没有空间时会提示 // 注意:重复创建的行为Miui和三星手机上不太一样,小米上似乎不能重复创建快捷方式 // 名字 addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"网络设置"); // 图标 addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(this,R.drawable.ic_perm_data_setting_black_24dp)); // 设置关联程序 Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//设置网络页面intent // 设置关联程序 // Intent launcherIntent = new Intent(Intent.ACTION_MAIN); // launcherIntent.setClass(MainActivity.this,MainActivity.class); // launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER); addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,launcherIntent); // 发送广播 sendbroadcast(addShortcutIntent);
安卓O:
ShortcutManager scm = (ShortcutManager) getSystemService(SHORTCUT_SERVICE); Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//设置网络页面intent ShortcutInfo si = new ShortcutInfo.Builder(this,"daTaroam") .setIcon(Icon.createWithResource(this,R.drawable.ic_perm_data_setting_black_24dp)) .setShortLabel("网络设置") .setIntent(launcherIntent) .build(); assert scm != null; scm.requestPinShortcut(si,null);
那如果要两者兼顾呢,则可以如下这样写:
//添加快捷方式 private void addShortcut() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { ShortcutManager scm = (ShortcutManager) getSystemService(SHORTCUT_SERVICE); Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//设置网络页面intent ShortcutInfo si = new ShortcutInfo.Builder(this,"daTaroam") .setIcon(Icon.createWithResource(this,R.drawable.ic_perm_data_setting_black_24dp)) .setShortLabel("网络设置") .setIntent(launcherIntent) .build(); assert scm != null; scm.requestPinShortcut(si,null); } else { Intent addShortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");//"com.android.launcher.action.INSTALL_SHORTCUT" // 不允许重复创建 addShortcutIntent.putExtra("duplicate",false);// 经测试不是根据快捷方式的名字判断重复的 // 应该是根据快链的Intent来判断是否重复的,即Intent.EXTRA_SHORTCUT_INTENT字段的value // 但是名称不同时,虽然有的手机系统会显示Toast提示重复,仍然会建立快链 // 屏幕上没有空间时会提示 // 注意:重复创建的行为Miui和三星手机上不太一样,小米上似乎不能重复创建快捷方式 // 名字 addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"网络设置"); // 图标 addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,R.drawable.ic_perm_data_setting_black_24dp)); // 设置关联程序 Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);//设置网络页面intent // 设置关联程序 // Intent launcherIntent = new Intent(Intent.ACTION_MAIN); // launcherIntent.setClass(MainActivity.this,MainActivity.class); // launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER); addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,launcherIntent); // 发送广播 sendbroadcast(addShortcutIntent); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
您可能感兴趣的文章:
- android 为应用程序创建桌面快捷方式技巧分享
- Android 创建/验证/删除桌面快捷方式(已测试可用)
- 解析Android应用启动后自动创建桌面快捷方式的实现方法
- Android添加(创建)、删除及判断是否存在桌面快捷方式的方法
- Android程序开发之手机APP创建桌面快捷方式
- Android如何创建桌面快捷方式
- Android应用创建桌面快捷方式代码
- Android编程创建桌面快捷方式的常用方法小结【2种方法】
android – 为桌面上的任何应用程序创建快捷方式
private void createShortcutOnDesktop(Application app) { Intent shortcutIntent = new Intent(); shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,app.getIntentShortcut()); shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,app.getName()); shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(context,R.drawable.home_button)); shortcutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); this.sendbroadcast(shortcutIntent); finish(); }
app.getIntentShortcut()是这样的:
public Intent getIntentShortcut() { Intent i = new Intent(); i.setClassName(packageName,name); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); return i; }
在清单中:
<permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
我失踪了什么谢谢.
解决方法
这个:
<permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
到这个:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
只是一个’使用’¬¬
我们今天的关于Android应用创建手机桌面快捷方式和android应用创建手机桌面快捷方式怎么设置的分享已经告一段落,感谢您的关注,如果您想了解更多关于Android Launcher 开发之自动添加桌面快捷方式及实际开发中常见问题的解决方案、Android O添加桌面快捷方式、Android O添加桌面快捷方式的示例、android – 为桌面上的任何应用程序创建快捷方式的相关信息,请在本站查询。
本文标签: