2011-06-21 15 views
7

可能性の重複:私は私の活動(HomeActivity.class)で1 TextViewButtonを持っています
Android create shortcuts on the home screen作成ショートカット

目標:私はTextViewに入力されたとして、それはアイコンとテキストとしてデフォルトアンドロイドの画像とのショートカットを作成する必要がありButtonをクリックしてください。

はこれまでのところ、私たちはこれは私がこれまで持っているコードがある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-how-to-add-home-screen-あなたのアプリへのショートカット/)それはおそらく[この質問]の複製です(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:これは株式ランチャー専用ですか?または、ユーザーがデフォルトで設定したランチャーはありますか? –

関連する問題