2015-06-03 8 views
21

Desidero utilizzare la nuova versione del metodo Intent.createChooser che utilizza IntentSender.Ottieni oggetto IntentSender per il metodo createChooser in Android

La documentazione indica solo che è possibile prenderlo dall'istanza PendingIntent. Nel mio caso sembra che PendingIntent non abbia alcun altro uso.

C'è un altro modo per ottenere IntentSender o è necessario creare PendingIntent?

+0

è necessario crearlo tramite 'PendingIntent'. I costruttori sono pubblici ma annotano con '@ hide' – Blackbelt

+0

@Blackbelt ma dovrei passare' PendingIntent' come obiettivo 'Intento'? – pixel

risposta

34

l'intenzione del destinatario della scelta non è PendingIntent. Ad esempio, nel seguente frammento, sto dichiarando l'intento per ACTION_SEND, con tipo text/plain e questo è l'intento del mio obiettivo per lo Intent.createChooser. Quindi sto creando un altro Intent, ricevitore e un gestore, il PendingIntet, che invocherà onReceive del mio BroadcastTest dopo aver selezionato qualcosa dal selettore.

Intent intent = new Intent(Intent.ACTION_SEND); 
intent.putExtra(Intent.EXTRA_TEXT, "This is my text to send."); 
intent.setType("text/plain"); 
Intent receiver = new Intent(this, BroadcastTest.class); 
receiver.putExtra("test", "test"); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT); 
Intent chooser = Intent.createChooser(intent, "test", pendingIntent.getIntentSender()); 
startActivity(chooser); 

Edit:

Le informazioni, nel caso del BroadcastReceiver è incorporato nel l'intento che si ottiene come parametro. Dopo aver selezionato una delle opzioni, recuperare gli extra del pacchetto e usare il tasto android.intent.extra.CHOSEN_COMPONENT, dovresti riuscire a trovare ciò che l'utente ha scelto.

Prova ad aggiungere più semplice Log.d al onReceive

for (String key : intent.getExtras().keySet()) { 
    Log.d(getClass().getSimpleName(), " " + intent.getExtras().get(key)); 
} 

Nel mio esempio ho avuto

ComponentInfo{org.telegram.messenger/org.telegram.ui.LaunchActivity}

per Telegram e

ComponentInfo{com.google.android.apps.inbox/com.google.android.apps.bigtop.activities.ComposeMessageActivity} 

per InBox

+1

Ok e come ottenere informazioni su quale scelta è stata effettuata dall'utente? Stati della documentazione 'Il chiamante può facoltativamente fornire un IntentSender per ricevere una richiamata quando l'utente effettua una scelta. Questo può essere utile se l'applicazione chiamante desidera ricordare l'ultimo target scelto e visualizzarlo come una quota più prominente o one-touch altrove nell'interfaccia utente per la prossima volta. – pixel

+4

non dimenticare di aggiungere la tua classe di registro broadcast al tuo manifest –

+3

C'è qualche esempio di codice che posso dare un'occhiata a come farlo? Il mio ricevitore broadcast non riceve nulla. – Migore

0

Un altro modo per farlo.

/** 
    * Receiver to record the chosen component when sharing an Intent. 
    */ 
    static class TargetChosenReceiver extends BroadcastReceiver { 
     private static final String EXTRA_RECEIVER_TOKEN = "receiver_token"; 
     private static final Object LOCK = new Object(); 

     private static String sTargetChosenReceiveAction; 
     private static TargetChosenReceiver sLastRegisteredReceiver; 

     static boolean isSupported() { 
      return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1; 
     } 

     @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1) 
     static void sendChooserIntent(Activity activity, Intent sharingIntent) { 
      synchronized (LOCK) { 
       if (sTargetChosenReceiveAction == null) { 
        sTargetChosenReceiveAction = activity.getPackageName() + "/" 
          + TargetChosenReceiver.class.getName() + "_ACTION"; 
       } 
       Context context = activity.getApplicationContext(); 
       if (sLastRegisteredReceiver != null) { 
        context.unregisterReceiver(sLastRegisteredReceiver); 
       } 
       sLastRegisteredReceiver = new TargetChosenReceiver(); 
       context.registerReceiver(
         sLastRegisteredReceiver, new IntentFilter(sTargetChosenReceiveAction)); 
      } 

      Intent intent = new Intent(sTargetChosenReceiveAction); 
      intent.setPackage(activity.getPackageName()); 
      intent.putExtra(EXTRA_RECEIVER_TOKEN, sLastRegisteredReceiver.hashCode()); 
      final PendingIntent callback = PendingIntent.getBroadcast(activity, 0, intent, 
        PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT); 
      Intent chooserIntent = Intent.createChooser(sharingIntent, 
        activity.getString(R.string.share_link_chooser_title), 
        callback.getIntentSender()); 
      activity.startActivity(chooserIntent); 
     } 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      synchronized (LOCK) { 
       if (sLastRegisteredReceiver != this) return; 
       context.getApplicationContext().unregisterReceiver(sLastRegisteredReceiver); 
       sLastRegisteredReceiver = null; 
      } 
      if (!intent.hasExtra(EXTRA_RECEIVER_TOKEN) 
        || intent.getIntExtra(EXTRA_RECEIVER_TOKEN, 0) != this.hashCode()) { 
       return; 
      } 

      ComponentName target = intent.getParcelableExtra(Intent.EXTRA_CHOSEN_COMPONENT); 
      if (target != null) { 
       setLastShareComponentName(context, target); 
      } 
     } 
    } 
Problemi correlati