Android桌面快捷方式

这篇博客,我们主要讨论一下Android中的桌面快捷方式的开发。。。

废话不多说了。。。

直接实战

1.判断快捷方式是否创建。

/**
 * 验证是否创建快捷方式
* 
* @return
 */
public boolean isAddShortcut() {
final ContentResolver mContentResolver=mContext.getContentResolver();
int versionLevel=Build.VERSION.SDK_INT;
String AUTHORITY = "com.android.launcher2.settings";
if(versionLevel>=8){
AUTHORITY="com.android.launcher2.settings";
}else{
AUTHORITY="com.android.launcher.settings";
}
final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");
        Cursor c = mContentResolver.query(CONTENT_URI,
                new String[] { "title", "iconResource" }, "title=?",
                new String[] { mContext.getString(R.string.app_name) }, null);


        if (c != null && c.getCount() > 0) {
            return true;
        }
return false;
}


2.添加快捷方式。

/**
* 创建快捷方式
*/
public void addShortcut() {
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        // 设置属性
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, mContext.getString(R.string.app_name));
        ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(mContext.getApplicationContext(), R.drawable.ic_launcher);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,iconRes);
 
        // 是否允许重复创建
        shortcut.putExtra("duplicate", false);
        
        //设置桌面快捷方式的图标
        Parcelable icon = Intent.ShortcutIconResource.fromContext(mContext,R.drawable.ic_launcher);        
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);
        
        //点击快捷方式的操作
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setClass(mContext, MainActivity.class);
        
        // 设置启动程序
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
        
        //广播通知桌面去创建
        mContext.sendBroadcast(shortcut);
}

3.删除快捷方式。

/**
* 删除快捷图标
*/
public void deleteShortcut(){
    Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");    
    //快捷方式的名称    
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,mContext.getString(R.string.app_name));    
    /**删除和创建需要对应才能找到快捷方式并成功删除**/  
    Intent intent = new Intent();   
    intent.setClass(mContext, MainActivity.class);    
    intent.setAction("android.intent.action.MAIN");    
    intent.addCategory("android.intent.category.LAUNCHER");    
         
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);    
    mContext.sendBroadcast(shortcut);  
}


4.权限的加入。

<!-- 创建快捷方式权限 -->  
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

5.快捷方式启动的Activity加入intent-filter,如下:

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

上一份源码:

/**
 * 
 */
package com.hanfeng.shortcut;


import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Parcelable;


/**
 *
 * 快捷方式处理
 *
 * @author hanfeng
 * @time 2014-12-3 下午1:48:01
 * 
 */
public class ShorcutHandler {

private Context mContext;



/**
* @param mContext
*/
public ShorcutHandler(Context mContext) {
super();
this.mContext = mContext;
}


/**
* 创建快捷方式
*/
public void addShortcut() {
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        // 设置属性
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, mContext.getString(R.string.app_name));
        ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(mContext.getApplicationContext(), R.drawable.ic_launcher);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,iconRes);
 
        // 是否允许重复创建
        shortcut.putExtra("duplicate", false);
        
        //设置桌面快捷方式的图标
        Parcelable icon = Intent.ShortcutIconResource.fromContext(mContext,R.drawable.ic_launcher);        
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);
        
        //点击快捷方式的操作
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setClass(mContext, MainActivity.class);
        
        // 设置启动程序
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
        
        //广播通知桌面去创建
        mContext.sendBroadcast(shortcut);
}

/**
* 删除快捷图标
*/
public void deleteShortcut(){
Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");    
    //快捷方式的名称    
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,mContext.getString(R.string.app_name));    
    /**删除和创建需要对应才能找到快捷方式并成功删除**/  
    Intent intent = new Intent();   
    intent.setClass(mContext, MainActivity.class);    
    intent.setAction("android.intent.action.MAIN");    
    intent.addCategory("android.intent.category.LAUNCHER");    
         
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);    
    mContext.sendBroadcast(shortcut);  
}


/**
* 验证是否创建快捷方式
* 
* @return
*/
public boolean isAddShortcut() {
final ContentResolver mContentResolver=mContext.getContentResolver();
int versionLevel=Build.VERSION.SDK_INT;
String AUTHORITY = "com.android.launcher2.settings";
if(versionLevel>=8){
AUTHORITY="com.android.launcher2.settings";
}else{
AUTHORITY="com.android.launcher.settings";
}
final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");
        Cursor c = mContentResolver.query(CONTENT_URI,
                new String[] { "title", "iconResource" }, "title=?",
                new String[] { mContext.getString(R.string.app_name) }, null);


        if (c != null && c.getCount() > 0) {
            return true;
        }
return false;
}
}


到此,Android的快捷方式的开发完毕。。。


源码下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值