2011-06-21 13 views
7

Możliwe duplikaty:
Android create shortcuts on the home screentworzenia skrótów w Androidzie przez intencyjny

Mam jedną TextView i Button w mojej działalności (HomeActivity.class).

bramki: Kiedy klikam na Button należy utworzyć skrót z domyślną android obrazu jako ikony i tekst wpisanej do TextView.

Jak dotąd okazało się, że możemy tworzyć skróty użyciu ACTION_CREATE_SHORTCUT

Jest to kod mam tak daleko:

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); 

Mój plik manifestu:

<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> 

Ale ten kod nie tworzy żadnego skrótu.

Jak mogę utworzyć skróty?

+0

Myślę, że należy spojrzeć na tej stronie [] (http://codinggeek.org/2011/ 01/02/android-how-to-add-home-screen-skróty-do-twojej-aplikacji /). Jest to prawdopodobnie również duplikat tego pytania (http://stackoverflow.com/questions/5676090/android-is-there-- programming-way-to-create-a-web-shortcut-on-home-screen) lub [to pytanie] (http://stackoverflow.com/questions/6337431/android-create-shortcuts-on-the-home-screen). –

Odpowiedz

17

Spróbuj tego:

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); 

i dodać to uprawnienie w manifeście:

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

Nie działa dla mnie. Czy to nadal działa? –

+0

@CapDroid: Tworzy skrót, nawet jeśli taki istnieje. Czy możemy utworzyć skrót w taki sposób, aby zastąpił istniejący zamiast duplikowania ..? Proszę przewodnika. – Mudassir

+0

@CapDroid: Czy to nie tylko dla programu uruchamiającego giełdę? Lub dowolny program uruchamiający ustawiony jako domyślny przez użytkownika? –