我在嘗試以編程方式在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);
你看看這個? http://stackoverflow.com/questions/6337431/android-create-shortcuts-on-the-home-screen – Antrromet
是的,我做了,它創建應用程序後仍然關閉應用程序 –
所以當你說關閉時,所有的活動生命週期方法被調用? – nandeesh