android shortcut 创建 判断

一、直接在桌面直接生成,是通过广播(Broadcast)的形式向Luncher发送请求生成快捷方式的

需要权限

  和要点击快捷方式对应的那个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表中,所以可以查询此表得到,

  需要权限

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

  

以下是代码片段:
  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.           //快捷图片
        //Parcelable icon 
    = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);
        //shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
  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. }  

以下是代码片段:
  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>  


  二、长按桌面弹出的桌面快捷方式创建

  第一页谈过直接在桌面生成快捷方式,现在说说如何在添加到一个SHORTCUTS列表中,就是你长按桌面弹出来的那个东东。

  首先在注册activity时,需要添加一个action为android.intent.action.CREATE_SHOERTCUT的intentFilter.如下所示:

<activity android:name= " ShortCutTest ">
            <intent- filter>
                <action android:name= " android.intent.action.CREATE_SHORTCUT "/>
            </intent- filter>
        </activity>

  接下来就是就是设置快捷方式的图标、名称、事件等属性。这里图表的生成,android里提供了专门的方法来生成。

public class ShortCutTest extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }
    
     public void createShortCut(){
        Intent addShortCut;
        //判断是否需要添加快捷方式
         if(getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT)){
            addShortCut =  new Intent();
            //快捷方式的名称
            addShortCut.putExtra(Intent.EXTRA_SHORTCUT_NAME ,  " 我的快捷方式 ");
            //显示的图片
            Parcelable icon = ShortcutIconResource.fromContext(this, R.drawable.icon);
            addShortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
            //快捷方式激活的activity,需要执行的intent,自己定义
            addShortCut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,  new Intent());
            //OK,生成
            setResult(RESULT_OK, addShortCut);
        } else{
            //取消
            setResult(RESULT_CANCELED);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值