Android編程實(shí)現(xiàn)創(chuàng)建,刪除,判斷快捷方式的方法
本文實(shí)例講述了Android編程實(shí)現(xiàn)創(chuàng)建,刪除,判斷快捷方式的方法。分享給大家供大家參考,具體如下:
/**
* 為程序創(chuàng)建桌面快捷方式 ,這樣寫,在程序卸載的時(shí)候,快捷方式也會(huì)一并刪除
*/
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); // 不允許重復(fù)創(chuàng)建
/*
* shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
* getApplicationContext(), SplashActivity.class));
*/
// 注意: ComponentName的第二個(gè)參數(shù)必須加上點(diǎn)號(hào)(.),否則快捷方式無法啟動(dòng)相應(yīng)程序
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));
// 快捷方式的圖標(biāo)
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
this, R.drawable.icon_launcher);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
sendBroadcast(shortcutIntent);
}
//判斷是否已經(jīng)創(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;
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
如何通過Android Logcat插件分析firebase崩潰問題
android crash Crash(應(yīng)用崩潰)是由于代碼異常而導(dǎo)致App非正常退出,導(dǎo)致應(yīng)用程序無法繼續(xù)使用,所有工作都停止的現(xiàn)象,本文重點(diǎn)介紹如何通過Android Logcat插件分析firebase崩潰問題,感興趣的朋友一起看看吧2024-01-01
Android視頻加水印之FFmpeg的簡單應(yīng)用實(shí)例
最近有個(gè)需求,需要錄制視頻,能添加水印,所以下面這篇文章主要給大家介紹了關(guān)于Android視頻加水印之FFmpeg的簡單應(yīng)用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
Android Studio 合并module到統(tǒng)一文件夾的方法
這篇文章主要介紹了Android Studio 合并module到統(tǒng)一文件夾的方法,補(bǔ)充介紹了android studio關(guān)于同名資源文件的合并技巧,需要的朋友可以參考下2018-04-04
Kotlin學(xué)習(xí)筆記之const val與val
這篇文章主要給大家介紹了關(guān)于Kotlin學(xué)習(xí)筆記之const val與val的相關(guān)資料,并給大家介紹了const val和val區(qū)別以及Kotlin中var和val的區(qū)別,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
Android提高之SurfaceView的基本用法實(shí)例分析
這篇文章主要介紹了Android提高之SurfaceView的基本用法,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08
Android WebView基礎(chǔ)應(yīng)用詳解
這篇文章主要為大家介紹了Android中WebView這一控件的基礎(chǔ)應(yīng)用,例如:播放音樂,播放視頻等,文中的示例代碼講解詳細(xì),對(duì)于我們了解WebView很有幫助,需要的同學(xué)可以學(xué)習(xí)一下2021-12-12

