Android編程實現(xiàn)創(chuàng)建,刪除,判斷快捷方式的方法
更新時間:2017年02月24日 12:01:07 作者:Jacob-wj
這篇文章主要介紹了Android編程實現(xiàn)創(chuàng)建,刪除,判斷快捷方式的方法,結合實例形式分析了Android編程針對快捷方式的常用操作技巧,需要的朋友可以參考下
本文實例講述了Android編程實現(xiàn)創(chuàng)建,刪除,判斷快捷方式的方法。分享給大家供大家參考,具體如下:
/**
* 為程序創(chuàng)建桌面快捷方式 ,這樣寫,在程序卸載的時候,快捷方式也會一并刪除
*/
private void addShortcut() {
Intent shortcutIntent = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
// 快捷方式的名稱
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.app_name));
shortcutIntent.putExtra("duplicate", false); // 不允許重復創(chuàng)建
/*
* shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
* getApplicationContext(), SplashActivity.class));
*/
// 注意: ComponentName的第二個參數(shù)必須加上點號(.),否則快捷方式無法啟動相應程序
ComponentName comp = new ComponentName(this.getPackageName(),
this.getPackageName() + "." + this.getLocalClassName());
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
intent.setComponent(comp));
// 快捷方式的圖標
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
this, R.drawable.icon_launcher);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
sendBroadcast(shortcutIntent);
}
//判斷是否已經創(chuàng)建快捷方式
private boolean hasShortcut() {
boolean isInstallShortcut = false;
final ContentResolver resolver = this.getContentResolver();
final String AUTHORITY;
if (android.os.Build.VERSION.SDK_INT < 8) {
AUTHORITY = "com.android.launcher.settings";
} else {
AUTHORITY = "com.android.launcher2.settings";
}
final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/favorites?notify=true");
Cursor c = resolver
.query(CONTENT_URI,
new String[] { "title", "iconResource" },
"title=?",
new String[] { this.getString(R.string.app_name).trim() },
null);
if (c != null && c.getCount() > 0) {
isInstallShortcut = true;
}
return isInstallShortcut;
}
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
相關文章
如何通過Android Logcat插件分析firebase崩潰問題
android crash Crash(應用崩潰)是由于代碼異常而導致App非正常退出,導致應用程序無法繼續(xù)使用,所有工作都停止的現(xiàn)象,本文重點介紹如何通過Android Logcat插件分析firebase崩潰問題,感興趣的朋友一起看看吧2024-01-01
Android Studio 合并module到統(tǒng)一文件夾的方法
這篇文章主要介紹了Android Studio 合并module到統(tǒng)一文件夾的方法,補充介紹了android studio關于同名資源文件的合并技巧,需要的朋友可以參考下2018-04-04
Android提高之SurfaceView的基本用法實例分析
這篇文章主要介紹了Android提高之SurfaceView的基本用法,非常實用的功能,需要的朋友可以參考下2014-08-08

