如何检测Android设备上的屏幕是否为触摸屏

时间:2015-01-02 08:37:26

标签: android broadcastreceiver ontouchlistener

我正在写一个不使用dayDream的应用屏幕服务器。

ScreenSaverActivity我覆盖ontouch:

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

        Long time = new GregorianCalendar().getTimeInMillis()+ 10*1000;

        Intent intentAlarm = new Intent(this, AlarmReciever.class);

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
        startService(new Intent(this, MyService.class));

        this.onBackPressed(); // turn off app
        return false;   }

AlrmService.class

public class AlarmReciever extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        // TODO Auto-generated method stub

        Intent i = new Intent();
        i.setClassName("edu.com.screensaver", "edu.com.screensaver.ScreenSaverActivity");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i); // turn on again
    }

}

我想要alrmActivity的更新时间,所以我添加了MyService.class句柄触摸事件

public class MyService extends Service implements OnTouchListener{
 @Override
    public boolean onTouch(View v, MotionEvent event) {
        Toast.makeText(this, "MyService click", Toast.LENGTH_LONG).show();
        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());

        return true;
    }
}

" MyService点击"消息永远不会打电话。当我在10秒后触摸主要活动时,应用程序再次打开。

欢迎任何解决方案。

由于

1 个答案:

答案 0 :(得分:0)

试试这段代码:

boolean isTouchSupported = getPackageManager()。hasSystemFeature(“android.hardware.touchscreen”);