android 桌面快捷方式
阿新 • • 發佈:2019-01-31
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
//注意: ComponentName的第二個引數必須加上點號(.),否則快捷方式無法啟動相應程式
ComponentName comp = new ComponentName(getPackageName(), ".activity.Welcome");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
//快捷方式的圖示
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
sendBroadcast(shortcut);
}
private boolean isCreateShortcut(){
boolean isInstallShortcut = false ;
final String AUTHORITY = "com.android.launcher2.settings";
final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");
Cursor c = managedQuery(CONTENT_URI, new String[] {"title","iconResource" }, "title=?",
new String[] {getString(R.string.app_name) }, null);
if(c != null && c.getCount() > 0){
isInstallShortcut = true ;
}
return isInstallShortcut ;
}