2011-06-21 91 views
7

可能重複:
Android create shortcuts on the home screen創建快捷

我有在我的活動(HomeActivity.class)一個 TextViewButton

目標:當我點擊Button作爲TextView進入它應該創建一個默認的Android形象圖標和文本的快捷方式。

到目前爲止,我發現,我們可以使用ACTION_CREATE_SHORTCUT

這是我的代碼到目前爲止創建快捷方式:

Intent result = new Intent(android.content.Intent.ACTION_CREATE_SHORTCUT); 
result.putExtra(Intent.EXTRA_SHORTCUT_INTENT, 
       new Intent(getApplicationContext(), Editor.class)); 
result.putExtra(TodoDbAdapter.KEY_ROWID,rowid); 
result.putExtra(Intent.EXTRA_SHORTCUT_NAME,textView.getText()); 
result.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 
       Intent.ShortcutIconResource.fromContext(HomeActivity.this, 
                 R.drawable.icon)); 
setResult(RESULT_OK, result); 

我的清單文件:

<activity android:name=".HomeActivity" android:label="@string/app_name"> 

     <intent-filter> 
       <action android:name="android.intent.action.CREATE_SHORTCUT" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 

但此代碼不會創建任何快捷方式。

如何創建快捷方式?

+0

我想你應該看看[此頁](http://codinggeek.org/2011/ 01/02/Android系統如何對附加主屏幕 - 快捷方式到您的應用程序內/)。這也可能是[這個問題]的副本(http://stackoverflow.com/questions/5676090/android-is-there-a-programming-way-to-create-a-web-shortcut-on-home-screen )或[這個問題](http://stackoverflow.com/questions/6337431/android-create-shortcuts-on-the-home-screen)。 –

回答

17

試試這個:

Intent shortcutIntent; 
shortcutIntent = new Intent(); 
shortcutIntent.setComponent(new ComponentName(activity.getPackageName(), ".classname")); 

shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

final Intent putShortCutIntent = new Intent(); 
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 

// Sets the custom shortcut's title 
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"Title"); 
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(PersonProfile.this, R.drawable.icon)); 
putShortCutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
sendBroadcast(putShortCutIntent); 

並在清單中添加此權限:

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

不適合我。這是否仍然有效? –

+0

@CapDroid:這將創建一個快捷方式,即使存在一個。我們可以創建快捷方式,以取代現有的快捷方式而不是重複..?請指導。 – Mudassir

+0

@CapDroid:這不僅適用於股票發射器嗎?或者任何啓動器設置爲默認的用戶? –