GVKun编程网logo

Android应用创建手机桌面快捷方式(android应用创建手机桌面快捷方式怎么设置)

8

如果您想了解Android应用创建手机桌面快捷方式的相关知识,那么本文是一篇不可错过的文章,我们将对android应用创建手机桌面快捷方式怎么设置进行全面详尽的解释,并且为您提供关于AndroidLa

如果您想了解Android应用创建手机桌面快捷方式的相关知识,那么本文是一篇不可错过的文章,我们将对android应用创建手机桌面快捷方式怎么设置进行全面详尽的解释,并且为您提供关于Android Launcher 开发之自动添加桌面快捷方式及实际开发中常见问题的解决方案、Android O添加桌面快捷方式、Android O添加桌面快捷方式的示例、android – 为桌面上的任何应用程序创建快捷方式的有价值的信息。

本文目录一览:

Android应用创建手机桌面快捷方式(android应用创建手机桌面快捷方式怎么设置)

Android应用创建手机桌面快捷方式(android应用创建手机桌面快捷方式怎么设置)

下面是小编 jb51.cc 通过网络收集整理的代码片段。

小编小编现在分享给大家,也给大家做个参考。

<LinearLayout 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: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 开发之自动添加桌面快捷方式及实际开发中常见问题的解决方案

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


源码打印?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.android.autoLancher"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="8" />  
  8.     <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>  
  9.     <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>    
  10.     <!-- 快捷方式信息需要从 setting 中读取 -->  
  11.     <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>   
  12.     <application  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name" >  
  15.         <activity  
  16.             android:name=".LauncherDemo2Activity"  
  17.             android:label="@string/app_name" >  
  18.             <intent-filter>  
  19.                 <action android:name="android.intent.action.MAIN" />  
  20.   
  21.                 <category android:name="android.intent.category.LAUNCHER" />  
  22.             </intent-filter>  
  23.             <pre name="code" class="java">              <!- 如果想让应用出现在长按 Menu 键选择添加快捷方式中,则需要加入以下代码 ->   
  24.             <intent-filter>  
  25. </pre> <action android:name="android.intent.action.CREATE_SHORTCUT"></action> </intent-filter> </activity> </application></manifest>  
  26. <pre></pre>  
  27. <p></p>  
  28. <p></p>  
  29. <p></p>  
  30. <p></p>  
  31. <pre></pre>  
  32. 2. LauncherDemo2Activity:  
  33. <p></p>  
  34. <p></p>  
  35. <p></p>  
  36. <pre name="code" class="java">package com.android.autoLancher;  
  37.   
  38. import android.app.Activity;  
  39. import android.app.AlertDialog;  
  40. import android.app.AlertDialog.Builder;  
  41. import android.content.ContentResolver;  
  42. import android.content.DialogInterface;  
  43. import android.content.DialogInterface.OnClickListener;  
  44. import android.content.Intent;  
  45. import android.database.Cursor;  
  46. import android.net.Uri;  
  47. import android.os.Bundle;  
  48.   
  49. public class LauncherDemo2Activity extends Activity {  
  50.     public void onCreate(Bundle savedInstanceState) {  
  51.         super.onCreate(savedInstanceState);  
  52.         setContentView(R.layout.main);  
  53.           
  54.           
  55.         boolean  flag =isShortcutInstalled();// 如果已经创建,则不需要在创建    
  56.         if(flag==false){    
  57.               
  58.             AlertDialog.Builder builder = new Builder(LauncherDemo2Activity.this);  
  59.             builder.setTitle("是否为此应用创建桌面快捷方式");  
  60.             builder.setPositiveButton("是"new OnClickListener() {  
  61.                   
  62.                 public void onClick(DialogInterface dialog, int which) {  
  63.                     addShortcutToDesktop();  
  64.                 }  
  65.             });  
  66.               
  67.             builder.setNegativeButton("否"new OnClickListener() {  
  68.                   
  69.                 public void onClick(DialogInterface dialog, int which) {  
  70.                       
  71.                 }  
  72.             });  
  73.               
  74.             builder.create().show();  
  75.               
  76.               
  77.         }    
  78.           
  79.     }  
  80.   
  81.     private void addShortcutToDesktop() {  
  82.   
  83.         Intent shortcut = new Intent(  
  84.                 "com.android.launcher.action.INSTALL_SHORTCUT");  
  85.         // 不允许重建  
  86.         shortcut.putExtra("duplicate"false);  
  87.         // 设置名字  
  88.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "加菲快捷");  
  89.         // 设置图标  
  90.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,  
  91.                 Intent.ShortcutIconResource.fromContext(this,  
  92.   
  93.                 R.drawable.cat));  
  94.         // 设置意图和快捷方式关联程序  
  95.         Intent intent = new Intent(thisthis.getClass());  
  96.         // 桌面图标和应用绑定,卸载应用后系统会同时自动删除图标  
  97.         intent.setAction("android.intent.action.MAIN");  
  98.         intent.addCategory("android.intent.category.LAUNCHER");  
  99.           
  100.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);  
  101.   
  102.         // 发送广播  
  103.         sendBroadcast(shortcut);  
  104.   
  105.           
  106.     }  
  107.   
  108.     /** 
  109.      * 快捷方式信息是保存在 com.android.launcher 的 launcher.db 的 favorites 表中 
  110.      *  
  111.      * @return 
  112.      */  
  113.     public boolean isShortcutInstalled() {  
  114.         boolean isInstallShortcut = false;  
  115.         final ContentResolver cr = LauncherDemo2Activity.this  
  116.                 .getContentResolver();  
  117.        // 2.2 系统是”com.android.launcher2.settings”, 网上见其他的为 "com.android.launcher.settings"  
  118.         String AUTHORITY = null;  
  119.         /* 
  120.          * 根据版本号设置 Uri 的 AUTHORITY 
  121.          */  
  122.         if(getSystemVersion()>=8){  
  123.             AUTHORITY = "com.android.launcher2.settings";  
  124.         }else{  
  125.             AUTHORITY = "com.android.launcher.settings";  
  126.         }  
  127.           
  128.         Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY  
  129.                 + "/favorites?notify=true");  
  130.         Cursor c = cr.query(CONTENT_URI,  
  131.                 new String[] { "title""iconResource" }, "title=?",  
  132.                 new String[] { getString(R.string.app_name) }, null);// XXX 表示应用名称。  
  133.         if (c != null && c.getCount() > 0) {  
  134.             isInstallShortcut = true;  
  135.             System.out.println("已创建");  
  136.         }  
  137.         return isInstallShortcut;  
  138.     }  
  139.       
  140.      /** 
  141.      * 获取系统的 SDK 版本号 
  142.      * @return 
  143.      */  
  144.     private int getSystemVersion(){  
  145.         return Build.VERSION.SDK_INT;  
  146.     }  
  147.   
  148.  }  
  149. </pre><br>  
  150. <br>  
  151. <p></p>  
  152. <p><br>  
  153. </p>  
  154. <br>  
  155. <pre></pre>  
  156. <pre></pre>  
  157. <pre></pre>  
  158. <pre></pre>  
  159. <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>
            
源码打印?
  1. <!-如果想让应用出现在长按Menu键选择添加快捷方式中,则需要加入以下代码->   
  2. t;intent-filter>  
              <!-如果想让应用出现在长按Menu键选择添加快捷方式中,则需要加入以下代码-> 
            <intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT"></action> </intent-filter> </activity> </application></manifest>


2. LauncherDemo2Activity:

源码打印?
  1. package com.android.autoLancher;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.app.AlertDialog.Builder;  
  6. import android.content.ContentResolver;  
  7. import android.content.DialogInterface;  
  8. import android.content.DialogInterface.OnClickListener;  
  9. import android.content.Intent;  
  10. import android.database.Cursor;  
  11. import android.net.Uri;  
  12. import android.os.Bundle;  
  13.   
  14. public class LauncherDemo2Activity extends Activity {  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.           
  19.           
  20.         boolean  flag =isShortcutInstalled();//如果已经创建,则不需要在创建    
  21.         if(flag==false){    
  22.               
  23.             AlertDialog.Builder builder = new Builder(LauncherDemo2Activity.this);  
  24.             builder.setTitle("是否为此应用创建桌面快捷方式");  
  25.             builder.setPositiveButton("是"new OnClickListener() {  
  26.                   
  27.                 public void onClick(DialogInterface dialog, int which) {  
  28.                     addShortcutToDesktop();  
  29.                 }  
  30.             });  
  31.               
  32.             builder.setNegativeButton("否"new OnClickListener() {  
  33.                   
  34.                 public void onClick(DialogInterface dialog, int which) {  
  35.                       
  36.                 }  
  37.             });  
  38.               
  39.             builder.create().show();  
  40.               
  41.               
  42.         }    
  43.           
  44.     }  
  45.   
  46.     private void addShortcutToDesktop() {  
  47.   
  48.         Intent shortcut = new Intent(  
  49.                 "com.android.launcher.action.INSTALL_SHORTCUT");  
  50.         // 不允许重建  
  51.         shortcut.putExtra("duplicate"false);  
  52.         // 设置名字  
  53.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "加菲快捷");  
  54.         // 设置图标  
  55.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,  
  56.                 Intent.ShortcutIconResource.fromContext(this,  
  57.   
  58.                 R.drawable.cat));  
  59.         // 设置意图和快捷方式关联程序  
  60.         Intent intent = new Intent(thisthis.getClass());  
  61.         // 桌面图标和应用绑定,卸载应用后系统会同时自动删除图标  
  62.         intent.setAction("android.intent.action.MAIN");  
  63.         intent.addCategory("android.intent.category.LAUNCHER");  
  64.           
  65.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);  
  66.   
  67.         // 发送广播  
  68.         sendBroadcast(shortcut);  
  69.   
  70.           
  71.     }  
  72.   
  73.     /** 
  74.      * 快捷方式信息是保存在com.android.launcher的launcher.db的favorites表中 
  75.      *  
  76.      * @return 
  77.      */  
  78.     public boolean isShortcutInstalled() {  
  79.         boolean isInstallShortcut = false;  
  80.         final ContentResolver cr = LauncherDemo2Activity.this  
  81.                 .getContentResolver();  
  82.        // 2.2系统是”com.android.launcher2.settings”,网上见其他的为"com.android.launcher.settings"  
  83.         String AUTHORITY = null;  
  84.         /* 
  85.          * 根据版本号设置Uri的AUTHORITY 
  86.          */  
  87.         if(getSystemVersion()>=8){  
  88.             AUTHORITY = "com.android.launcher2.settings";  
  89.         }else{  
  90.             AUTHORITY = "com.android.launcher.settings";  
  91.         }  
  92.           
  93.         Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY  
  94.                 + "/favorites?notify=true");  
  95.         Cursor c = cr.query(CONTENT_URI,  
  96.                 new String[] { "title""iconResource" }, "title=?",  
  97.                 new String[] { getString(R.string.app_name) }, null);// XXX表示应用名称。  
  98.         if (c != null && c.getCount() > 0) {  
  99.             isInstallShortcut = true;  
  100.             System.out.println("已创建");  
  101.         }  
  102.         return isInstallShortcut;  
  103.     }  
  104.       
  105.      /** 
  106.      * 获取系统的SDK版本号 
  107.      * @return 
  108.      */  
  109.     private int getSystemVersion(){  
  110.         return Build.VERSION.SDK_INT;  
  111.     }  
  112.   
  113.  }  
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添加桌面快捷方式

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添加桌面快捷方式的示例

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 – 为桌面上的任何应用程序创建快捷方式

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 – 为桌面上的任何应用程序创建快捷方式的相关信息,请在本站查询。

本文标签: