现在的位置: 首页 > 综合 > 正文

关于android创建快捷方式会启动两个应用的问题(一)

2013年03月09日 ⁄ 综合 ⁄ 共 2359字 ⁄ 字号 评论关闭

在做创建应用快捷方式时遇到两个问题:

一、创建快捷方式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" />

抱歉!评论已关闭.