2017-09-20 1 views
-2

enter code here BackgroundService-Lorsque vous utilisez la bulle flottante en tant que service d'arrière-plan pour prendre une capture d'écran de l'écran en direct. Obtenir deux erreurs

private void addNewBubble()//ERROR , Expression expected and Missing ';' token{ 


     windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); 
     //here is all the science of params 
     final WindowManager.LayoutParams myParams = new WindowManager.LayoutParams(
       WindowManager.LayoutParams.WRAP_CONTENT, 
       WindowManager.LayoutParams.WRAP_CONTENT, 
       WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, 
       WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 
         | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON 
         | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, 
       PixelFormat.TRANSLUCENT 
     ); 
     BubbleLayout bubbleView = (BubbleLayout) LayoutInflater.from(BackgroundService.this).inflate(R.layout.bubble_layout, null); 
     bubbleView.setLayoutParams(myParams); 

     bubbleView.setOnBubbleRemoveListener(new BubbleLayout.OnBubbleRemoveListener() { 
      @Override 
      public void onBubbleRemoved(BubbleLayout bubble) { 
      } 
     }); 
     bubbleView.setOnBubbleClickListener(new BubbleLayout.OnBubbleClickListener() { 

      @Override 
      public void onBubbleClick(BubbleLayout bubble) { 


       Bitmap b = Screenshot.takescreenshotOfRootView(imageView); 
       imageView.setImageBitmap(b); 
       main.setBackgroundColor(Color.parseColor("#999999")); 

       //Toast.makeText(getApplicationContext(), "Clicked !", 
       // Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     bubbleView.setShouldStickToWall(true); 
     bubblesManager.addBubble(bubbleView, 60, 20); 
    } 
} 

private void initializeBubblesManager() { 
    bubblesManager = new BubblesManager.Builder(this) 
      .setTrashLayout(R.layout.bubble_trash_layout) 
      .setInitializationCallback(new OnInitializedCallback() { 
       @Override 
       public void onInitialized() { 
        addNewBubble();// ERROR 
       } 
      }) 
      .build(); 
    bubblesManager.initialize(); 
} 

}

Ceci est la méthode OnStart qui comprend toutes les méthodes pour créer la bulle flottante et de le rendre cliquable de prendre une capture d'écran. Seul addNewBubble affiche des erreurs, alors que lorsque le code Floating Bubble est exécuté sur MainActivity sans la création de BackgroundService, il fonctionne correctement sans aucune erreur. Des suggestions sur ce qu'il faut faire?

+0

Je suis nouveau à stackoverflow, s'il vous plaît aidez-moi. J'ai vérifié ici et ne peux pas sembler trouver une erreur semblable. – Yohanelly

+2

Copie possible de [autorisation Android refusée pour le type de fenêtre 2010 dans Marshmallow ou supérieur] (https://stackoverflow.com/questions/37982167/android-permission-denied-for-window-type-2010-in-marshmallow-or- supérieur) – ventiseis

+0

Oui j'ai lu ceci, l'erreur est similaire. Mais mon code est radicalement différent, j'ai compilé 'com.txusballesteros: bubbles: 1.2.1' – Yohanelly

Répondre

0

Copier coller ce code .Je l'ai testé

import android.content.Intent; 
    import android.graphics.PixelFormat; 
    import android.net.Uri; 
    import android.os.Build; 
    import android.os.Bundle; 

    import android.provider.Settings; 
    import android.support.v7.app.AppCompatActivity; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.view.WindowManager; 
    import android.widget.Toast; 

    import com.txusballesteros.bubbles.BubbleLayout; 
    import com.txusballesteros.bubbles.BubblesManager; 
    import com.txusballesteros.bubbles.OnInitializedCallback; 
    /** 
    * Created by yohanson on 20/09/17. 
    */ 

    public class MainActivity extends AppCompatActivity { 

     private BubblesManager bubblesManager; 
     private WindowManager windowManager; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 


      findViewById(R.id.add).setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        checkDrawOverlayPermission(); 


       } 
      }); 
     } 


public void checkDrawOverlayPermission() { 
    /** check if we already have permission to draw over other apps */ 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (!Settings.canDrawOverlays(this)) { 
      /** if not construct intent to request permission */ 
      Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, 
        Uri.parse("package:" + getPackageName())); 
      /** request permission via start activity for result */ 
      startActivityForResult(intent, 2); 
     } 
     else 
     { 
      initializeBubblesManager(); 
      addNewBubble(); 

     } 
    } 
} 
     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      /** check if received result code 
      is equal our requested code for draw permission */ 
      if (requestCode == 2) { 
       initializeBubblesManager(); 
       addNewBubble(); 

      } 
     } 

     private void addNewBubble() { 


      windowManager = (WindowManager)getSystemService(WINDOW_SERVICE); 
      //here is all the science of params 
      final WindowManager.LayoutParams myParams = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT, 
        WindowManager.LayoutParams.WRAP_CONTENT, 
        WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, 
        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 
          | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON 
          | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, 
        PixelFormat.TRANSLUCENT 
      ); 
      BubbleLayout bubbleView = (BubbleLayout)LayoutInflater.from(MainActivity.this).inflate(R.layout.bubble_layout, null); 
      bubbleView.setLayoutParams(myParams); 

      bubbleView.setOnBubbleRemoveListener(new BubbleLayout.OnBubbleRemoveListener() { 
       @Override 
       public void onBubbleRemoved(BubbleLayout bubble) { } 
      }); 
      bubbleView.setOnBubbleClickListener(new BubbleLayout.OnBubbleClickListener() { 

       @Override 
       public void onBubbleClick(BubbleLayout bubble) { 
        Toast.makeText(getApplicationContext(), "Clicked !", 
          Toast.LENGTH_SHORT).show(); 
       } 
      }); 
      bubbleView.setShouldStickToWall(true); 
      bubblesManager.addBubble(bubbleView, 60, 20); 
     } 

     private void initializeBubblesManager() { 
      bubblesManager = new BubblesManager.Builder(this) 
        .setTrashLayout(R.layout.bubble_trash_layout) 
        .setInitializationCallback(new OnInitializedCallback() { 
         @Override 
         public void onInitialized() { 
          addNewBubble(); 
         } 
        }) 
        .build(); 
      bubblesManager.initialize(); 
     } 

     @Override 
     protected void onDestroy() { 
      super.onDestroy(); 
      bubblesManager.recycle(); 
     } 
    } 
+0

Merci beaucoup, l'application s'ouvre maintenant. Bien que, la première fois que je l'ai ouvert, cela a fonctionné parfaitement. J'ai fermé l'application et l'ai courue encore. L'application s'ouvre mais le bouton AJOUTER BUBBLE ne crée plus de bulle. Est-ce que cela s'est produit quand vous l'avez exécuté? – Yohanelly

+0

J'oublie de faire la partie else dans checkDrawOverlayPermission(), j'ai édité le code ..il suffit de le vérifier – Saneesh

+0

J'ai donc utilisé un autre émulateur. Ive réalisé, cela fonctionne la première fois depuis que l'application s'ouvre la première fois, il demande la permission. Ensuite, la bulle est créée. Toutefois, en ouvrant l'application pour la deuxième fois, elle ne demande pas d'autorisation et l'application s'ouvre, mais le bouton Ajouter une bulle ne crée pas de nouvelle bulle. De toute façon autour de cela? – Yohanelly

0

fonction UiThread

runOnUiThread(new Runnable() { 
     public void run() { 
     addNewBubble() 
     } 
    }); 

Appel addNewBubble() comme celui-ci.

+0

Ok monsieur, je le ferai. Mais dans quelle partie de MainActivity.java dois-je entrer ce code? – Yohanelly

+0

findViewById (R.id.add) .setOnClickListener (nouveau View.OnClickListener() { @Override public void onClick (Voir v) { runOnUiThread (nouveau Runnable() { public void run() { addNewBubble() } }); } }); – Saneesh

+0

Merci beaucoup, je vais mettre à jour mon application et vous le faire savoir. Je suis en train de télécharger un sdk, je vous mettrai à jour dès que cela sera fait. – Yohanelly