2013-06-07 3 views
1

Je souhaite lancer une boîte de dialogue personnalisée lorsque l'utilisateur clique sur la notification dans la barre de notification.Lancer une boîte de dialogue personnalisée lorsque vous cliquez sur la notification

J'ai déjà créé la notification et la classe de boîte de dialogue personnalisée. Mais je ne sais pas comment lancer quand l'utilisateur clique dessus.

Tous les didacticiels que j'ai recherchés lancent une activité et non un dialogue. Alors, quelqu'un peut-il m'aider à cet égard.

Merci.

C'est mon code de dialogue personnalisé

public class custom_dialog extends Dialog { 
    Context m_context; 
    LayoutInflater mInflater = null; 

    public custom_dialog (Context context, int theme) { 
     super(context,R.style.Theme_Dialog); 
     // TODO Auto-generated constructor stub 
     this.m_context = context; 
     Toast.makeText(getBaseContext(),"onCreate", Toast.LENGTH_SHORT).show(); 
     mInflater=LayoutInflater.from(m_context); 
       } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 
} 
+0

ce serait bien de voir à quoi ressemble votre classe de dialogue, si vous utilisez fragment et instance (ce que vous devriez) vous l'appelez en utilisant le gestionnaire de fragment –

+0

Définissez le thème de votre activité sur dialog, par ex. 'android: theme =" @ android: style/Theme.Dialog "' – ozbek

+0

J'ai mis à jour ma question avec le code. – Vinuthan

Répondre

2

vous pouvez essayer avec ce code

Context context = getApplicationContext(); 
CharSequence contentTitle = "Title"; 
CharSequence contentText = "content"; 
Intent notifyIntent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.android.com")); 

PendingIntent intent = 
       PendingIntent.getActivity(SimpleNotification.this, 0, 
       notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK); 

notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent); 
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails); 

juste faire la pendingintent ouvrir une de vos activités et votre activité soit complète transparente et juste ouvert un dialogue.

+0

J'utilise en fait mon propre thème pour le dialogue personnalisé. Aussi setLatestEventInfo est obsolète. Nous devrions donc utiliser Notification.Builder pour cela. – Vinuthan

+1

@Vinuthan Nous pouvons utiliser Notification.Builder pour cela. Lorsque vous appelez l'activité, définissez le thème comme transparent ainsi que votre thème de dialogue personnalisé sur l'acitivité afin qu'il affiche l'apparence du dialogue. – chaitanya

+0

Je vais essayer et revenir vers vous. Je vous remercie. – Vinuthan

2

Eh bien, tout clic est comme un clic sur un élément. Je lance ma boîte de dialogue personnalisée en cliquant sur un bouton. Voilà comment je le fais:

main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/buttonShowCustomDialog" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Show Custom Dialog" /> 

</LinearLayout> 

custom.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="5dp" /> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#FFF" 
     android:layout_toRightOf="@+id/image"/>/> 

    <Button 
     android:id="@+id/dialogButtonOK" 
     android:layout_width="100px" 
     android:layout_height="wrap_content" 
     android:text=" Ok " 
     android:layout_marginTop="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_below="@+id/image" 
     /> 

</RelativeLayout> 

Maintenant, pour le java pour les lier:

MainActivity.java:

public class MainActivity extends Activity { 

    final Context context = this; 
    private Button button; 

    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     button = (Button) findViewById(R.id.buttonShowCustomDialog); 

     // add button listener 
     button.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

      // custom dialog 
      final Dialog dialog = new Dialog(context); 
      dialog.setContentView(R.layout.custom); 
      dialog.setTitle("Custom Title"); 

      // set the custom dialog components - text, image and button 
      TextView text = (TextView) dialog.findViewById(R.id.text); 
      text.setText("Android custom dialog example!"); 
      ImageView image = (ImageView) dialog.findViewById(R.id.image); 
      image.setImageResource(R.drawable.ic_launcher); 

      Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK); 
      // if button is clicked, close the custom dialog 
      dialogButton.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        dialog.dismiss(); 
       } 
      }); 

      dialog.show(); 
      } 
     }); 
    } 
} 

Vous pouvez probablement le comparez ceci pour votre but ...

+0

Je suis désolé mais je pense que vous vous êtes trompé, Ma question est de savoir comment lancer une boîte de dialogue lorsque l'utilisateur clique sur la notification dans la barre de notification. Votre solution se lance en cliquant sur un bouton. – Vinuthan

+0

@Vinuthan: Tout clic si c'est sur un bouton ou une barre de notification est un événement de clic seulement. Tout ce que vous devez faire ici est d'attraper la notification de R.java, au lieu du bouton, dire comme ceci: 'nfBar = (nfBar) findViewById (R.id.buttonShowCustomDialog);'. Maintenant vous avez juste besoin de mettre 'nfBar.setOnClickListener() 'au lieu de' button.setOnClickListener'. –

+0

Oui, c'est ce que je pose la question. Comment lancer une boîte de dialogue à partir d'un clic de notification. Je lance déjà la boîte de dialogue en cliquant sur un bouton. – Vinuthan

Questions connexes