简体   繁体   English

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

[英]How do I detect whether or not the screen on an android device is a touchscreen

I'm writting an app Screenserver not using dayDream. 我正在编写不使用dayDream的应用程序Screenserver。

ScreenSaverActivity I override ontouch: 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 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
    }

}

I want update time for alrmActivity so I added MyService.class handle touch event 我想要更新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 click" message is never call. 永远不会调用“ MyService click”消息。 When i touch in main activity after 10 seconds the app open again. 当我在10秒后触摸主要活动时,该应用程序再次打开。

Any solution is welcome. 任何解决方案都欢迎。

Thanks 谢谢

Try this code: 试试这个代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM