2013-07-02 71 views
1

创建一个独特的快捷键安装应用时行为有所我用下面的代码来创建一个快捷方式安装的应用程序时:不同的Android版本

在AndroidManifest.xml

<!-- for creating a shortcut in the home screen --> 
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> 

中的onCreate( )主要活动:

// an Intent to create a shortCut 
    Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 
    //repeat to create is forbidden 
    shortcutIntent.putExtra("duplicate", false); 
    //set the name of shortCut 
    shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, this.getString(R.string.app_name)); 
    //set icon 
    Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher); 
    shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); 
    //set the application to lunch when you click the icon 
    shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT 
      , new Intent(getApplicationContext() , MainActivity.class)); 
    //sendBroadcast,done 
    sendBroadcast(shortcutIntent); 

这些代码在安卓4.0.4,其产生在第一时间快捷方式,并发送举杯说的快捷方式已经EXI做工精细sts在第一次安装后。但在Android 4.2.2中,我可以通过单击后退键创建许多重复的快捷方式,然后再次输入应用程序。

有没有什么办法可以在两个版本的Android上工作?

感谢提前:)

回答

-1

shortcutIntent.putExtra( “重复”,FALSE)未在4.2版本的工作。我认为你必须用其他方法来创造独特的捷径。

如果failour则只需卸载快捷方式,安装again.something,如:

addIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT"); 
getApplicationContext().sendBroadcast(addIntent);    
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
getApplicationContext().sendBroadcast(addIntent); 

和舱单同时使用UNINSTALL_SHORTCUT和INSTALL_SHORTCUT权限。

而这个粗略的想法很好地适用于每个版本。

相关问题