在做創建應用快捷方式時遇到兩個問題:
一、創建快捷方式OK,但測試時MOTO部分機型會報錯,原因也在Log裏面給提示,如下:
java.lang.SecurityException: Permission Denial: opening provider com.motorola.blur.home.WorkspaceProvider from ProcessRecord{40a940f0 9595:com.android.xxx/10089} (pid=9595, uid=10089) requires com.android.launcher.permission.READ_SETTINGS or com.android.launcher.permission.WRITE_SETTINGS
相信做過android應用的這個問題都能解決,原因是沒有申請權限,在Manifest文件中加上相應的權限即可。
二、如果在應用列表中啓動時,按HOME鍵返回時,再點擊快捷方式會重啓應用,想要徹底退出時,會發現要退出兩次,不管以何種順序啓動,只要以兩種方式啓動時都會需要退兩次才能退出應用。在做應用過程中遇到的問題的解決方法是在manifest文件中在第一個啓動應用的Activity也就是歡迎界面的Activity中加上android:launchMode="singleInstance"屬性。最後看到的效果是以兩種方式啓動應用每次都會經過歡迎界面,之後進入的是上一次退出的界面,按返回鍵退出時,只需一次,問題基本解決,但要進兩次歡迎界面,還有待解決。
以下是創建快捷方式代碼(摘自http://www.cnblogs.com/-OYK/archive/2011/05/31/2064797.html)
1,判斷是否已經創建了快捷方式(在某些moto的機型中需要判斷)
/**
* 是否已創建快捷方式
* @return
*/
private boolean hasShortcut()
{
boolean isInstallShortcut = false;
final String AUTHORITY ="com.android.launcher.settings";
final Uri CONTENT_URI = Uri.parse("content://" +AUTHORITY + "/favorites?notify=true");
Cursor c = getContentResolver().query(CONTENT_URI, new String[] {"title", "iconResource" }, "title=?", new String[] {getString(R.string.app_name).trim()}, null);
if(null != c && c.getCount() > 0)
{
isInstallShortcut = true ;
}
return isInstallShortcut;
}
2, 創建
/**
* 創建快捷方式
*/
private void addShortcut()
{
if (hasShortcut())
{
return;
}
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(this.getPackageName(), this.getPackageName() + "." +this.getLocalClassName());
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);
}
3, 聲明權限
在AndroidManifest.xml 文件中聲明 創建和刪除快捷方式時聲明權限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
注:MOTO的部分機型還要加上以下權限
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" />