2016-02-29 1 views
1

Je dois définir un rappel dans mon application. Ainsi, une fois l'heure de rappel est atteinte, l'application doit afficher une fenêtre pop-up (Même si l'application ne fonctionne pas), tout comme la façon dont WhatsApp affiche des messages dans une fenêtre contextuelle quand il ne fonctionne pasComment afficher WhatsApp comme notification contextuelle lorsque l'application est en arrière-plan?

enter image description here

sur les écoutes le bouton que je dois lancer mon application aussi. Comment puis-je afficher une fenêtre contextuelle en arrière-plan? Y a-t-il des échantillons disponibles? Merci d'avance

+0

Cela pourrait vous aider http://stackoverflow.com/quest ions/25921960/gcm-intentservice-comment-afficher-une-pop-up-on-notification-receive – Arshad

Répondre

6

Vous pouvez utiliser SYSTEM_ALERT_WINDOW à partir de votre BroadcastReceiver pour afficher une boîte de dialogue qui s'affichera au-dessus de toutes les autres applications.

Ajoutez d'abord la permission

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> 

dans Manifest, puis dans votre onReceiver, Créer un AlertDialog comme suit

@Override 
public void onReceive(final Context context, Intent intent) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(context.getApplicationContext()); 
     LayoutInflater inflater = LayoutInflater.from(context); 
     View dialogView = inflater.inflate(R.layout.your_dialog_layout, null); 
     builder.setView(dialogView); 
     final AlertDialog alert = builder.create(); 
     alert.getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
     alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 
     alert.show(); 
     WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
     Window window = alert.getWindow(); 
     lp.copyFrom(window.getAttributes()); 
     //This makes the dialog take up the full width 
     lp.width = WindowManager.LayoutParams.MATCH_PARENT; 
     lp.height = WindowManager.LayoutParams.WRAP_CONTENT; 
     window.setAttributes(lp); 
} 
+0

@Droiod est-ce du code complet? – kadik

-1
private void sendNotification(String messageBody) { 

     preference = new SharePreference(this); 


     new Thread() { 
      public void run() { 

       Activity.runOnUiThread(new Runnable() { 

        @Override 
        public void run() { 
         try { 
          AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); 
          LayoutInflater inflater = LayoutInflater.from(getApplicationContext()); 
          View dialogView = inflater.inflate(R.layout.your_dialog_layout, null); 
          builder.setView(dialogView); 
          final AlertDialog alert = builder.create(); 
          alert.getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
          alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 
          alert.show(); 
          WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
          Window window = alert.getWindow(); 
          lp.copyFrom(window.getAttributes()); 
          lp.width = WindowManager.LayoutParams.MATCH_PARENT; 
          lp.height = WindowManager.LayoutParams.WRAP_CONTENT; 
          window.setAttributes(lp); 

         } catch (Exception e) { 

         } 
        } 
       }); 

      } 
     }.start(); 

J'ai mis en œuvre ce code mais je reçois la ligne rouge sous le runOnUIThread