天天看点

常用代码段Android Shortcut操作(快捷方式)

1、创建应用的快捷方式

所以在我们的manifest.xml文件中,添加权限 uses-permission android:name=com.android.launcher.permission.INSTALL_SHORTCUT

private void addShortcut(){   
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");   
 
// 快捷方式的名称   
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));   
// 不允许重复创建   
shortcut.putExtra("duplicate", false);   
 
// 指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer   
// 这里必须为Intent设置一个action,可以任意(但安装和卸载时该参数必须一致)   
String action = "com.android.action.test";   
Intent respondIntent = new Intent(this, this.getClass());   
respondIntent.setAction(action);   
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, respondIntent);   
// 下面的方法与上面的效果是一样的,另一种构建形式而已   
// 注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序   
// String appClass = this.getPackageName() + "." + this.getLocalClassName();   
// ComponentName comp = new ComponentName(this.getPackageName(), appClass);   
// shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(action).setComponent(comp));   
 
// 快捷方式的图标   
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);   
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);   
sendBroadcast(shortcut);   
}
           

2、右上角有数字的快捷方式         

public void createShortCut(){
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");   
 
// 快捷方式的名称   
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));   
// 允许重复创建   
shortcut.putExtra("duplicate", true);   
 
// 指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer   
// 这里必须为Intent设置一个action,可以任意(但安装和卸载时该参数必须一致)   
String action = "com.android.action.test";   
Intent respondIntent = new Intent(this, this.getClass());   
respondIntent.setAction(action);   
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, respondIntent);   

shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,
generatorContactCountIcon(((BitmapDrawable)(getResources().getDrawable(R.drawable.ic_launcher))).getBitmap())); 
sendBroadcast(shortcut);  
}
     private Bitmap generatorContactCountIcon(Bitmap icon){ 
        //初始化画布 
        int iconSize=(int)getResources().getDimension(android.R.dimen.app_icon_size); 
        Bitmap contactIcon=Bitmap.createBitmap(iconSize, iconSize, Config.ARGB_8888); 
        Canvas canvas=new Canvas(contactIcon); 
         
        //拷贝图片 
        Paint iconPaint=new Paint(); 
        iconPaint.setDither(true);//防抖动 
        iconPaint.setFilterBitmap(true);//用来对Bitmap进行滤波处理,这样,当你选择Drawable时,会有抗锯齿的效果 
        Rect src=new Rect(0, 0, icon.getWidth(), icon.getHeight()); 
        Rect dst=new Rect(0, 0, iconSize, iconSize); 
        canvas.drawBitmap(icon, src, dst, iconPaint); 
         
        //在图片上创建一个覆盖的联系人个数 
//        int contacyCount=11; 
        String ssString=editText.getText().toString();
       int sum= Integer.parseInt(ssString);
       if (sum>=100) {
ssString="99+";
  }
    
        //启用抗锯齿和使用设备的文本字距 
        Paint countPaint=new Paint(Paint.ANTI_ALIAS_FLAG|Paint.DEV_KERN_TEXT_FLAG); 
        countPaint.setColor(Color.RED); 
        countPaint.setTextSize(20f); 
        countPaint.setTypeface(Typeface.DEFAULT_BOLD); 
        canvas.drawText(ssString, iconSize-35, 30, countPaint); 
        return contactIcon; 
    } 
           

 3、删除程序的快捷方式

/** 
* 删除程序的快捷方式 
* 
* 同时需要在manifest中设置以下权限: 
* <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> 
*/ 
private void delShortcut() { 
Intent shortcut = newIntent("com.android.launcher.action.UNINSTALL_SHORTCUT"); 
// 快捷方式的名称 
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); 
// 指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer 
// 这里必须为Intent设置一个action,可以任意(但安装和卸载时该参数必须一致) 
String action = "com.android.action.test"; 
Intent respondIntent = new Intent(this, this.getClass()); 
respondIntent.setAction(action); 
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, respondIntent); 
// 下面的方法与上面的效果是一样的,另一种构建形式而已 
// 注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序 
// String appClass = this.getPackageName() + "." + this.getLocalClassName(); 
// ComponentName comp = new ComponentName(this.getPackageName(), appClass); 
// shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(action).setComponent(comp)); 
sendBroadcast(shortcut); 
} 
           

4、判断是否存在快捷方式

/**
* 判断桌面是否已添加快捷方式
* 
* @param cx
* @param titleName
*            快捷方式名称
* @return
*/
public static boolean hasShortcut(Context cx) {
   boolean result = false;
   // 获取当前应用名称
   String title = null;
   try {
       final PackageManager pm = cx.getPackageManager();
       title = pm.getApplicationLabel(
               pm.getApplicationInfo(cx.getPackageName(),
                       PackageManager.GET_META_DATA)).toString();
   } catch (Exception e) {
   }

   final String uriStr;
   if (android.os.Build.VERSION.SDK_INT < 8) {
       uriStr = "content://com.android.launcher.settings/favorites?notify=true";
   } else {
       uriStr = "content://com.android.launcher2.settings/favorites?notify=true";
   }
   final Uri CONTENT_URI = Uri.parse(uriStr);
   final Cursor c = cx.getContentResolver().query(CONTENT_URI, null,
           "title=?", new String[] { title }, null);
   if (c != null && c.getCount() > 0) {
       result = true;
   }
   return result;
}
           

5、添加到Shortcut选项中

/** 
* 添加到Shortcut选项中(默认桌面上长按调出) 
* 
* 同时需要在manifest中为activity提供一个包含 
* action="android.intent.action.CREATE_SHORTCUT"的intent-filter 
*/ 
private void addShortcutToOptions(){ 
// 创建一个默认的Intent 
Intent shortcut = new Intent(); 


//快捷方式的名称 
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); 
//不允许重复创建 
shortcut.putExtra("duplicate", false); 


//指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer 
//注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序 
String appClass = this.getPackageName() + "." +this.getLocalClassName(); 
ComponentName comp = new ComponentName(this.getPackageName(), appClass); 
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, newIntent(Intent.ACTION_MAIN).setComponent(comp)); 

//快捷方式的图标 
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon); 
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); 


// 发送到消息队列 
setResult(RESULT_OK, shortcut); 
}