2

我有以下代碼,我想獲得x和y座標。setOnTouchListener無法在RelativeLayout上使用擴展服務

public class HeadService extends Service { 

    private final static int FOREGROUND_ID = 999; 

    private WindowManager windowManager; 

    // private HeadLayer mHeadLayer; 

    ViewGroup mTopView; 
    LayoutInflater inflater; 

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

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); 

     inflater = (LayoutInflater) getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE); 
     mTopView = (ViewGroup) inflater.inflate(
       R.layout.activity_invisible, null); 


     RelativeLayout rl; 
     rl = (RelativeLayout) mTopView.findViewById(R.id.window); 




     final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
       WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 
       WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, 
       PixelFormat.TRANSLUCENT); 



     windowManager.addView(rl, params); 



     rl.setOnTouchListener(new View.OnTouchListener() { 
      private int initialX; 
      private int initialY; 
      private float initialTouchX; 
      private float initialTouchY; 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       switch (event.getAction()) { 
        case MotionEvent.ACTION_DOWN: 
         initialX = params.x; 
         initialY = params.y; 
         initialTouchX = event.getRawX(); 
         initialTouchY = event.getRawY(); 
         return true; 
        case MotionEvent.ACTION_UP: 
         return true; 
        case MotionEvent.ACTION_MOVE: 
         params.x = initialX + (int) (event.getRawX() - initialTouchX); 
         params.y = initialY + (int) (event.getRawY() - initialTouchY); 

         Toast.makeText(getApplicationContext(), String.valueOf(params.x), Toast.LENGTH_SHORT).show(); 
         return true; 
       } 
       return false; 
      } 
     }); 



    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     logServiceStarted(); 

     initHeadLayer(); 

     PendingIntent pendingIntent = createPendingIntent(); 
     Notification notification = createNotification(pendingIntent); 

     startForeground(FOREGROUND_ID, notification); 

     return START_STICKY; 
    } 

    @Override 
    public void onDestroy() { 
     destroyHeadLayer(); 
     stopForeground(true); 

     logServiceEnded(); 
     if (chatHead != null) windowManager.removeView(chatHead); 
    } 

    private void initHeadLayer() { 
     // mHeadLayer = new HeadLayer(this); 

    } 

    private void destroyHeadLayer() { 
     // mHeadLayer.destroy(); 
     // mHeadLayer = null; 
    } 

    private PendingIntent createPendingIntent() { 
     Intent intent = new Intent(this, MainActivity.class); 
     return PendingIntent.getActivity(this, 0, intent, 0); 
    } 

    private Notification createNotification(PendingIntent intent) { 
     return new Notification.Builder(this) 
       .setContentTitle(getText(R.string.notificationTitle)) 
       .setContentText(getText(R.string.notificationText)) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setContentIntent(intent) 
       .build(); 
    } 

    private void logServiceStarted() { 
     Toast.makeText(this, "Start", Toast.LENGTH_SHORT).show(); 
    } 

    private void logServiceEnded() { 
     Toast.makeText(this, "End", Toast.LENGTH_SHORT).show(); 
    } 
} 

XML文件:

<RelativeLayout 
    android:id="@+id/window" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#80000000" 
    android:clickable="false" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:orientation="vertical"> 

</RelativeLayout> 

現在,當我觸摸屏幕上的話,我不能夠得到x和y座標。

所以我怎樣才能得到座標相對佈局的觸摸?

+0

是調試器在onTouch方法停止? –

+0

@VivekMishra不,我已經把調試點,但它並沒有停止。 – deepak

+1

從此鏈接仔細閱讀第一行http://developer.android.com/guide/components/services.html –

回答

0

我已經解決了。

我剛取代WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAYWindowManager.LayoutParams.TYPE_SYSTEM_ALERT,它的工作原理。

謝謝