[英]How to detect whether screen is touched in a background service in Android Studio?
我正在研究android studio上的一個項目。 我需要在后台服務中檢測屏幕是否被觸摸 (並彈出一條消息)。 但是,我在后台服務中檢測屏幕是否被觸摸有問題而不影響使用智能手機的用戶 。
當我說“在后台檢測”時,我的意思是應用程序的效果不會干擾用戶在屏幕上做的事情。 我在下面的進展應該解釋我的意思:
我做了一個應用程序,通過在android中實現onTouchListener並使用onTouchListener的布局來檢測用戶是否在后台觸摸屏幕。 我是按照網站“ http://kpbird.blogspot.com.au/2013/03/android-detect-global-touch-event.html ”中的說明做到這一點的,我對布局進行了一些更改以涵蓋整個屏幕,所以它可以檢測整個屏幕。
但是,在我運行創建了持續檢測屏幕觸摸的服務的應用程序之后,每當用戶觸摸屏幕時,該觸摸被我的服務(布局)吸收,因此用戶無法正常使用他們的手機(比如按圖標啟動其他應用程序)。 這是因為布局覆蓋整個屏幕並阻止任何觸摸信息到它下面的圖標(布局檢測屏幕觸摸),因此圖標不知道有觸摸因此不會對用戶做出反應。 但我希望我的應用和服務允許用戶正常使用他們的手機。
我聽說android中的服務被設計為不與用戶交互(即屏幕觸摸)並在后台工作,但我想知道是否有辦法解決這個問題。
以下是我的服務代碼:
public class GlobalTouchService extends Service implements OnTouchListener{
private String TAG = this.getClass().getSimpleName();
// window manager
private WindowManager mWindowManager;
// linear layout will use to detect touch event
private LinearLayout touchLayout;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
// create linear layout
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 is equal to full screen
WindowManager.LayoutParams.MATCH_PARENT, // height is equal to full screen
WindowManager.LayoutParams.TYPE_PHONE, // Type Phone, 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 void onDestroy() {
if(mWindowManager != null) {
if(touchLayout != null) mWindowManager.removeView(touchLayout);
}
super.onDestroy();
}
public void showAlert(View view) {
AlertDialog.Builder myAlertBuilder = new AlertDialog.Builder(this);
myAlertBuilder.setMessage(getString(R.string.alertMessage))
.setTitle(getString(R.string.alertTitle))
.setPositiveButton(getString(R.string.alertPositiveChoice), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setNegativeButton(getString(R.string.alertNegativeChoice), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
onDestroy();
}
});
AlertDialog myAlert = myAlertBuilder.create();
myAlert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
myAlert.show();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
//if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP)
// Log.i(TAG, "Action :" + event.getAction() + "\t X :" + event.getRawX() + "\t Y :" + event.getRawY());
////////////////////
View myView = null;
//showAlert(myView);
///////////////////////
return false;
}
}
你有onTouch方法。 只是返回false並且觸摸不會被您的應用程序吸收。
另請查看: https : //stackoverflow.com/a/6384443/1723095
編輯:這可能不是你想要的,但如果你有root設備,你可以這樣做:
adb shell getevent dev/input/event1
在我的情況下,event1負責觸摸事件,但它可以有所不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.