2012-10-04 45 views
2

我在尝试以编程方式在Android应用程序主屏幕中创建快捷方式图标时出现问题。Android应用程序在主屏幕上创建快捷方式图标后关闭

我能够创建快捷方式图标,但之后会弹出一个警告,提示“创建应用程序快捷方式”,然后应用程序关闭。我不希望应用程序关闭,如果可能的话,我想摆脱创建快捷方式后弹出的警报。

我该如何做到这一点?

这里是我当前的代码:

Intent targetIntent = new Intent (Intent.ACTION_MAIN); 
targetIntent.setClassName (getApplicationContext(), "com.mainListActivity"); 
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 
shortcutintent.addFlags(Intent.FLAG_FROM_BACKGROUND); 

//repeat to create is forbidden 
shortcutintent.putExtra("duplicate", false); 

//set the name of shortCut 
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"SecureLauncher"); 

//set icon 
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher_cloud); 
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); 

//set the application to lunch when you click the icon 
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,targetIntent); 
sendBroadcast(shortcutintent); 
+0

你看看这个? http://stackoverflow.com/questions/6337431/android-create-shortcuts-on-the-home-screen – Antrromet

+0

是的,我做了,它创建应用程序后仍然关闭应用程序 –

+0

所以当你说关闭时,所有的活动生命周期方法被调用? – nandeesh

回答

0

我想通了为什么在应用程序关闭它在我的manifest.xml

我有这个在我面前表现的原因:我取下接收器部分:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/> 
<receiver 
    android:name=".CreateShortcut" 
    android:permission="com.android.launcher.permission.INSTALL_SHORTCUT"> 
    <intent-filter> 
     <action android:name="com.android.launcher.action.INSTALL_SHORTCUT"/> 
    </intent-filter> 
</receiver> 

,所以我只有这在我的清单:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/> 
0

使用此代码时创建的快捷

Intent shortcutIntent = new Intent(getApplicationContext(), LoginScreen.class);  
shortcutIntent.setAction(Intent.ACTION_MAIN); 

Intent addIntent = new Intent(); 
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "AIMS DOCTOR"); 
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.aims)); 
addIntent.putExtra("duplicate", false); 

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

而且你应该在你的清单使用:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> 
相关问题