2013-07-23 99 views
5

我已經創建了一個運行服務的應用程序。該服務基本上有一個覆蓋按鈕,點擊按鈕做一些事情。但是,每次點擊按鈕時,按鈕背景中的應用程序都會響應。請有人可以幫忙嗎?我已經提到linkAndroid:覆蓋按鈕onClick永遠不會被調用

這裏是我的代碼:

服務代碼:

public class HUD extends Service implements OnTouchListener{ 
    Button mButton; 
    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     String s; 
     s = "Hari"; 

     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_OVERLAY, 
       WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, 
       PixelFormat.TRANSLUCENT); 
     params.gravity = Gravity.RIGHT | Gravity.TOP; 
     params.setTitle("Load Average"); 
     WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); 
     wm.addView(mButton, params); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Toast.makeText(getBaseContext(),"onDestroy", Toast.LENGTH_LONG).show(); 
     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; 
    } 
} 

清單具有以下權限,以及:

我試圖改變類型

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
     WindowManager.LayoutParams.WRAP_CONTENT, 
     WindowManager.LayoutParams.WRAP_CONTENT, 
     WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, 
     WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, 

現在,無論我點擊什麼,觸摸事件都會被調用。請有人可以幫我嗎?

+0

在onTouch中返回false將告訴應用程序繼續處理觸摸,就好像按鈕沒有被觸摸一樣。嘗試返回true。 – anthonycr

回答

6

問題是你使用WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY。我認爲從某些Android 4.x開始,它不再可用。這是我認識的覆蓋按鈕的方式:

public class OverlayButton extends Service implements OnTouchListener { 

    Button mButton; 
    WindowManager wm; 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     mButton = new Button(this); 
     mButton.setText("Overlay button"); 
     mButton.setTextColor(Color.BLACK); 
     mButton.setOnTouchListener(this); 

     WindowManager.LayoutParams params = new WindowManager.LayoutParams(150, 
        150, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL 
        | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT); 

     params.gravity = Gravity.LEFT | Gravity.CENTER; 
     wm = (WindowManager) getSystemService(WINDOW_SERVICE); 
     wm.addView(mButton, params); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Toast.makeText(getBaseContext(), "onDestroy", Toast.LENGTH_LONG).show(); 
     if (mButton != null) { 
      ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mButton); 
      mButton = null; 
     } 
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 

     if (event.getAction() == MotionEvent.ACTION_UP) { 
      Log.d("OverlayButton onTouch", "touched the button"); 
      stopSelf(); 
     } 
     return true; 
    } 
} 

我做什麼,我也給了他再次消失,如果我碰他的能力。

這是我讓他出現的方式:

getActivity().startService(new Intent(getActivity().getBaseContext(), OverlayButton.class)); 

但是,如果你從你的活動啓動它,只需使用:

而且不要忘記設置這些在你的表現:

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

而且標籤內也不要忘記:

<service android:name="OverlayButton" ></service> 
1

當您使用此WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH標誌時,它會通知應用程序監視您視圖外的所有觸摸。

請嘗試使用WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL代替。這隻會攔截你的觀點。

要確認,您需要使用WindowManager.LayoutParams.TYPE_SYSTEM_ALERT使視圖可點擊。

相關問題