android--创建快捷方式和判断是否已经创建

简介:

一般android应用程序安装完成后是不会自动创建快捷方式的,所以可以自己在程序启动时实现。

 需要权限  <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

和要点击快捷方式对应的那个activity的属性。

        <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
              <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT"></action>
            </intent-filter>

如何判断快捷方式是否已经创建的方法,因为快捷方式信息是保存在com.android.launcher的launcher.db的favorites表中,所以可以查询此表得到,

需要权限 <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/> 

下面给个例子分享给各位:

[java]  view plain copy
  1. import android.app.Activity;  
  2. import android.content.ContentResolver;  
  3. import android.content.Intent;  
  4. import android.content.Intent.ShortcutIconResource;  
  5. import android.database.Cursor;  
  6. import android.graphics.BitmapFactory;  
  7. import android.net.Uri;  
  8. import android.os.Bundle;  
  9. public class AddShortCutActivity extends Activity {  
  10.     /** Called when the activity is first created. */  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.main);  
  15.             boolean  flag =IfaddShortCut();//如果已经创建,则不需要在创建  
  16.             if(flag==false){  
  17.                 addShortCut();  
  18.             }  
  19.     }  
  20.     public void addShortCut(){  
  21.         Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");  
  22.         // 设置属性  
  23.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.app_name));  
  24.         ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(AddShortCutActivity.this, R.drawable.icon);  
  25.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,iconRes);  
  26.           
  27.         // 是否允许重复创建  
  28.         shortcut.putExtra("duplicate"false);   
  29.         Intent intent = new Intent(Intent.ACTION_MAIN);  
  30.         intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);  
  31.         intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);  
  32.         intent.addCategory(Intent.CATEGORY_LAUNCHER);  
  33.         intent.setClass(AddShortCutActivity.this, AddShortCutActivity.class);  
  34.         // 设置启动程序  
  35.         System.out.println("createIcon");  
  36.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);  
  37.         AddShortCutActivity.this.sendBroadcast(shortcut);  
  38.     }  
  39.    public  boolean  IfaddShortCut(){  
  40.       boolean isInstallShortcut = false ;    
  41.         final ContentResolver cr = AddShortCutActivity.this.getContentResolver();    
  42.         //本人的2.2系统是”com.android.launcher2.settings”,网上见其他的为"com.android.launcher.settings"  
  43.         final String AUTHORITY = "com.android.launcher2.settings";    
  44.         final Uri CONTENT_URI = Uri.parse("content://" +    
  45.                          AUTHORITY + "/favorites?notify=true");  
  46.         Cursor c = cr.query(CONTENT_URI,    
  47.         new String[] {"title","iconResource" },    
  48.         "title=?",    
  49.         new String[] {getString(R.string.app_name ) }, null);//XXX表示应用名称。    
  50.                 if(c!=null && c.getCount()>0){    
  51.             isInstallShortcut = true ;    
  52.             System.out.println("已创建");  
  53.         }    
  54.         return isInstallShortcut ;    
  55.     }  
  56. }  


[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.shao.add"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="8" />  
  7.   
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  9.         <activity android:name=".AddShortCutActivity"  
  10.                   android:label="@string/app_name">  
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.               <intent-filter>  
  16.                 <action android:name="android.intent.action.CREATE_SHORTCUT"></action>  
  17.             </intent-filter>  
  18.         </activity>  
  19.   
  20.     </application>  
  21.      <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>   
  22.       <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>  
  23. </manifest>  


相关文章
|
XML 定位技术 Android开发
Android shortcuts快捷方式实现(支付宝长按图标弹出快捷方式入口)
Android shortcuts快捷方式实现(支付宝长按图标弹出快捷方式入口)
1599 4
Android shortcuts快捷方式实现(支付宝长按图标弹出快捷方式入口)
|
6天前
|
Java Android开发
Android桌面快捷方式图标生成与删除 使用Intent与launcher交互
Android桌面快捷方式图标生成与删除 使用Intent与launcher交互
18 1
|
10月前
|
XML API Android开发
Android长按图标展示快捷方式
这个特性,可以追溯到Android 7.1,也就是在7.1之后的系统,如果app支持,可以通过长按app图标展示一些快捷操作
127 0
|
安全 Java Android开发
【Android 逆向】APK 加壳脱壳现状 | 判断 APK 是否加壳 | APK 逆向流程
【Android 逆向】APK 加壳脱壳现状 | 判断 APK 是否加壳 | APK 逆向流程
1148 0
|
Java Android开发
Android 10.0 StatusBar—下拉菜单快捷方式
Android 10.0 StatusBar—下拉菜单快捷方式
|
存储 编解码 前端开发
Android自定义控件(八)——详解创建bitmap的方式
Android自定义控件(八)——详解创建bitmap的方式
239 0
Android自定义控件(八)——详解创建bitmap的方式
|
存储 Java Android开发
Android OpenGL ES(六)----进入三维在代码中创建投影矩阵和旋转矩阵
Android OpenGL ES(六)----进入三维在代码中创建投影矩阵和旋转矩阵
153 0
Android OpenGL ES(六)----进入三维在代码中创建投影矩阵和旋转矩阵
|
存储 XML 设计模式
一个简单的Android网络访问全局码判断及通用数据解析方案
我们在开发中,网络请求经常会遇到各种错误码的判断。比如下面这样:
124 0
|
Dart Android开发
使用Android Studio 创建flutter工程
使用Android Studio 创建flutter工程
220 0
|
传感器 安全 开发工具
Android识别模拟器,判断是模拟器还是真机
Android识别模拟器,判断是模拟器还是真机
1844 0