欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 創(chuàng)建/驗(yàn)證/刪除桌面快捷方式(已測(cè)試可用)

 更新時(shí)間:2013年02月06日 17:42:09   作者:  
桌面快捷方式的出現(xiàn)方便了用戶操作,在某些程度上提高了用戶體驗(yàn),接下來(lái)將介紹下Android創(chuàng)建/驗(yàn)證/刪除桌面快捷方式的實(shí)現(xiàn)思路及代碼,感興趣的朋友可以了解下,或許本文可以幫助到你
測(cè)試環(huán)境為Adnroid 2.1以上。
第一步:AndroidManifest.xml 權(quán)限配置:
添加快捷方式權(quán)限:
復(fù)制代碼 代碼如下:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

驗(yàn)證快捷方式是否存在權(quán)限:
復(fù)制代碼 代碼如下:

<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />

刪除快捷方式權(quán)限: 
復(fù)制代碼 代碼如下:

<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

代碼:
復(fù)制代碼 代碼如下:

public class ShortCutSample {
/**
* 添加快捷方式
* */
public void creatShortCut(Activity activity,String shortcutName,int resourceId)
{
Intent intent = new Intent();
intent.setClass(activity, activity.getClass());
/*以下兩句是為了在卸載應(yīng)用的時(shí)候同時(shí)刪除桌面快捷方式*/
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//不允許重復(fù)創(chuàng)建
shortcutintent.putExtra("duplicate", false);
//需要現(xiàn)實(shí)的名稱
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
//快捷圖片
Parcelable icon = Intent.ShortcutIconResource.fromContext(activity.getApplicationContext(), resourceId);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
//點(diǎn)擊快捷圖片,運(yùn)行的程序主入口
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
//發(fā)送廣播。OK
activity.sendBroadcast(shortcutintent);
}
/**
* 刪除快捷方式
* */
public void deleteShortCut(Activity activity,String shortcutName)
{
Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
//快捷方式的名稱
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,shortcutName);
//在網(wǎng)上看到到的基本都是一下幾句,測(cè)試的時(shí)候發(fā)現(xiàn)并不能刪除快捷方式。
//String appClass = activity.getPackageName()+"."+ activity.getLocalClassName();
//ComponentName comp = new ComponentName( activity.getPackageName(), appClass);
//shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
/**改成以下方式能夠成功刪除,估計(jì)是刪除和創(chuàng)建需要對(duì)應(yīng)才能找到快捷方式并成功刪除**/
Intent intent = new Intent();
intent.setClass(activity, activity.getClass());
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);
activity.sendBroadcast(shortcut);
}
/**
* 判斷是否存在快捷方式
* */
public boolean hasShortcut(Activity activity,String shortcutName)
{
String url = "";
int systemversion = Integer.parseInt(android.os.Build.VERSION.SDK);
/*大于8的時(shí)候在com.android.launcher2.settings 里查詢(未測(cè)試)*/
if(systemversion < 8){
url = "content://com.android.launcher.settings/favorites?notify=true";
}else{
url = "content://com.android.launcher2.settings/favorites?notify=true";
}
ContentResolver resolver = activity.getContentResolver();
Cursor cursor = resolver.query(Uri.parse(url), null, "title=?",new String[] {shortcutName}, null);
if (cursor != null && cursor.moveToFirst()) {
cursor.close();
return true;
}
return false;
}
}

調(diào)用測(cè)試代碼:
復(fù)制代碼 代碼如下:

 public class mainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ShortCutSample sample =new ShortCutSample();
String shortcutName=getString(R.string.app_name);
if(sample.hasShortcut(this, shortcutName))
sample.deleteShortCut(this,shortcutName);
else
sample.creatShortCut(this,shortcutName,R.drawable.icon);
}
}

在網(wǎng)上找了很久都是一樣的代碼,刪除那塊搞了一個(gè)下午才弄好,其實(shí)很簡(jiǎn)單的東東。
第一次發(fā)文章,Adnroid新人。多多交流和指導(dǎo)呀。呵呵。

相關(guān)文章

最新評(píng)論