2012-04-27 10 views

Odpowiedz

22

Wysyła transmisję INSTALL_SHORTCUT z wynikowym intentem jako dodatkową (w tym przypadku wynik Intent bezpośrednio otwiera aktywność).

//where this is a context (e.g. your current activity) 
    final Intent shortcutIntent = new Intent(this, SomeActivity.class); 

    final Intent intent = new Intent(); 
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 
    // Sets the custom shortcut's title 
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); 
    // Set the custom shortcut icon 
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon)); 
    // add the shortcut 
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 
    sendBroadcast(intent); 

Trzeba również to uprawnienie w swoim manifeście:

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

Dziękuję za rozwiązanie. Działa to dobrze dla mnie. Ale mam inny problem. Moja aplikacja obsługuje wiele języków. Tak więc, gdy zmieniam język telefonu w ustawieniach, nazwa mojej aplikacji zostanie zaktualizowana poprawnie w programie uruchamiającym. Ale nazwa aplikacji nie aktualizuje się do nowego języka na ekranie głównym. Czy muszę coś do tego dodać? Dziękujemy za przeczytanie .. – Sushil

+0

Ponieważ nie można zmodyfikować skrótu, nie będzie to możliwe. – volkersfreunde

+0

Doskonały. Wymagane jest pozwolenie w manifeście. –

5

pierwszym kroku należy upewnić się luncher może otrzymać nadanie:

<!-- Intent received used to install shortcuts from other applications --> 
    <receiver 
     android:name="com.android.launcher2.InstallShortcutReceiver" 
     android:permission="com.android.launcher.permission.INSTALL_SHORTCUT"> 
     <intent-filter> 
      <action android:name="com.android.launcher.action.INSTALL_SHORTCUT"/> 
     </intent-filter> 
    </receiver> 

Następnie dodaj uprawnienia w manifeście .xml

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

Wreszcie utworzyć funkcję i nazywają to po kliknięciu przycisku:

public void createShortCut(){ 
    // a Intent to create a shortCut 
    Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 
    //repeat to create is forbidden 
    shortcutintent.putExtra("duplicate", false); 
    //set the name of shortCut 
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname)); 
    //set icon 
    Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon); 
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); 
    //set the application to lunch when you click the icon 
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext() , EnterActivity.class)); 
    //sendBroadcast,done 
    sendBroadcast(shortcutintent); 
} 

zrobić to tak:

button.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      createShortCut(); 
     } 
    }); 
+0

, więc onclick powinien mieć w rezultacie trzecią sekcję? – CreeperHunter

+0

Tak, po prostu musisz zrobić tak: public void onClick (View v) { \t \t \t \t createShortCut(); \t \t} – Laolizi

2

Ok ... Wiem, że to stary wątek, ale chciałem się upewnić, inżynierowie odwiedzający ten wątek mają najnowsze informacje.

Począwszy od Androida O - w ramach limitów sprawdzania w tle (w tym przypadku niejawnych odbiorców) transmisja com.android.launcher.action.INSTALL_SHORTCUT nie ma już żadnego wpływu na aplikację, ponieważ jest teraz prywatna, niejawna transmisja.

Per Android O ActivityManagerService.java:

case "com.android.launcher.action.INSTALL_SHORTCUT": 
       // As of O, we no longer support this broadcasts, even for pre-O apps. 
       // Apps should now be using ShortcutManager.pinRequestShortcut(). 
       Log.w(TAG, "Broadcast " + action 
         + " no longer supported. It will not be delivered."); 

Mam nadzieję, że to pomoże!

+0

potwierdzenie, stare nadawanie 'Intent' sposób nie działa na Oreo i powyżej. za pomocą [ShortcutManagerCompat] (https://developer.android.com/reference/android/support/v4/content/pm/ShortcutManagerCompat.html) podczas kierowania na interfejs API26 i nowszy (metoda 'requestPinShortcut' zawiera funkcję powrotu do opcji' Intent') sposób na <= API25, powyżej używa 'ShortcutManager.pinRequestShortcut()') – snachmsm

Powiązane problemy