2016-10-24 17 views
6

Tôi đang chạy dịch vụ nền để phát hiện MotionEvent.ACTION_DOWN trong Android 5.0. Tôi đã sử dụng mã dưới đây và nó có thể phát hiện sự kiện chạm của tôi nhưng tôi không thể chạm vào các ứng dụng khác. Làm thế nào tôi có thể sửa chữa nó? Nếu bạn có một giải pháp tốt hơn, xin vui lòng cho tôi. Cảm ơn tất cả.Cách phát hiện MotionEvent.ACTION_DOWN trong dịch vụ Android

public class TouchServiceListener extends Service implements OnTouchListener { 
    private WindowManager mWindowManager; 
    // linear layout will use to detect touch event 
    private LinearLayout touchLayout; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     touchLayout = new LinearLayout(this); 
      // set layout width 30 px and height is equal to full screen 
      LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
      touchLayout.setLayoutParams(lp); 
      // set color if you want layout visible on screen 
      touchLayout.setBackgroundColor(Color.CYAN); 
      // set on touch listener 
      touchLayout.setOnTouchListener(this); 

      // fetch window manager object 
      mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); 
      // set layout parameter of window manager 
      WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.MATCH_PARENT, // width of layout 30 px 
        WindowManager.LayoutParams.MATCH_PARENT, // height is equal to full screen 
        WindowManager.LayoutParams.TYPE_PHONE, // Type Ohone, These are non-application windows providing user interaction with the phone (in particular incoming calls). 
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, // this window won't ever get key input focus 
        PixelFormat.TRANSLUCENT); 
      mParams.gravity = Gravity.LEFT | Gravity.TOP; 
      Log.i(TAG, "add View"); 

      mWindowManager.addView(touchLayout, mParams); 
    } 


    @Override 
    public boolean onTouch(View view, MotionEvent motionEvent) {   
     if(motionEvent.getAction() == MotionEvent.ACTION_UP|motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
      Log.d(TAG, "Touch me"); 
     } 
     return true; 
    } 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
    } 
} 

Manifest

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

Trả lời

6

Trước hết làm Layout của bạn theo overlay.you đang thiết lập chiều rộng và chiều cao match_parent đó là bao gồm toàn bộ màn hình để bạn không truy cập vào ứng dụng mà là bên ngoài của ứng dụng của bạn .

package anew.mikka.myapplication; 

    import android.app.Service; 
    import android.content.Intent; 
    import android.graphics.PixelFormat; 
    import android.os.IBinder; 
    import android.view.Gravity; 
    import android.view.MotionEvent; 
    import android.view.View; 
    import android.view.WindowManager; 
    import android.view.View.OnTouchListener; 
    import android.widget.Button; 
    import android.widget.Toast; 

    public class max extends Service implements View.OnTouchListener { 
     Button mButton; 
     @Override 
     public IBinder 
     onBind(Intent intent) { 
      return null; 
     } 

     @Override 
     public void onCreate() { 
      super.onCreate(); 
      //mView = new HUDView(this); 
      mButton = new Button(this); 
      mButton.setText("Overlay button"); 
      mButton.setOnTouchListener(this); 

      WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT, 
        WindowManager.LayoutParams.WRAP_CONTENT, 
        WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, 
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 
          | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL 
          | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, 
        PixelFormat.TRANSLUCENT); 
      params.gravity = Gravity.BOTTOM | Gravity.RIGHT; 
      params.setTitle("Load Average"); 
      WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); 
      wm.addView(mButton, params); 

     } 

     @Override 
     public void onDestroy() { 
      super.onDestroy(); 
      if(mButton != null) 
      { 
       ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mButton); 
       mButton = null; 
      } 
     } 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      Toast.makeText(this,"Overlay button event", Toast.LENGTH_SHORT).show(); 
      return false; 
     } 


    } 
+0

Tôi đã thử trước đây. Nhưng nó không thể nhấn bất kỳ ứng dụng nào khi tôi sử dụng mã trên. Bạn có thể thử? – Jame

+0

Tôi đã thử mã này sau khi đăng bài ở đây.Nếu bạn muốn truy cập một điều khiển ứng dụng khác thì không thể. –

+0

Cảm ơn bạn. Tôi đã thử nghiệm nó. Xin lỗi về sự chậm trễ. Mã của bạn đã hoạt động. Tuy nhiên, nó cho thấy một nút trên màn hình của tôi. Tôi đã cố gắng để làm cho nó vô hình, nhưng các liên lạc sẽ không hiển thị khi tôi làm cho nó vô hình. – Jame

Các vấn đề liên quan