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



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

一、创建快捷方式OK,但测试时MOTO部分机型会报错,原因也在Log里面给提示,如下:

  1. 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  
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的机型中需要判断)

  1. /** 
  2.      * 是否已创建快捷方式 
  3.      * @return 
  4.      */  
  5.     private boolean hasShortcut()  
  6.     {  
  7.         boolean isInstallShortcut = false;  
  8.         final String AUTHORITY ="com.android.launcher.settings";  
  9.         final Uri CONTENT_URI = Uri.parse("content://" +AUTHORITY + "/favorites?notify=true");  
  10.         Cursor c = getContentResolver().query(CONTENT_URI, new String[] {"title""iconResource" }, "title=?"new String[] {getString(R.string.app_name).trim()}, null);  
  11.         if(null != c && c.getCount() > 0)  
  12.         {  
  13.             isInstallShortcut = true ;  
  14.         }  
  15.           
  16.         return isInstallShortcut;  
  17.     }  
/**
	 * 是否已创建快捷方式
	 * @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, 创建

  1. /** 
  2.      * 创建快捷方式 
  3.      */  
  4.     private void addShortcut()  
  5.     {  
  6.         if (hasShortcut())  
  7.         {  
  8.             return;  
  9.         }  
  10.           
  11.         Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");   
  12.         //快捷方式的名称   
  13.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));   
  14.         shortcut.putExtra("duplicate"false); //不允许重复创建   
  15.   
  16.         //指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer   
  17.         //注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序   
  18.         ComponentName comp = new ComponentName(this.getPackageName(), this.getPackageName() + "." +this.getLocalClassName());   
  19.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));   
  20.   
  21.         //快捷方式的图标   
  22.         ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);   
  23.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);   
  24.   
  25.         sendBroadcast(shortcut);  
  26.     }  
/**
	 * 创建快捷方式
	 */
	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 文件中声明 创建和删除快捷方式时声明权限

  1. <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />   
  2. <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />  
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> 
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

注:MOTO的部分机型还要加上以下权限

  1. <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />   
  2. <uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" />  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值