2012-10-04 41 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" /> 
相關問題