java - 如何保持设备传感器唤醒?
问题描述
使用传感器我正在检测一些计算的运动,我在Service
.
sensorManager.registerListener(sensorListener, sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION), SensorManager.SENSOR_DELAY_NORMAL);
为此,我已经在使用带有通知的前台服务来保持传感器处于活动状态。
下面是我在Service
课堂上使用的代码(对于返回粘性的通知具有更高的优先级)。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
registerSensor();
Notification notification = new NotificationCompat.Builder(this, ServiceNotification.CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_stat_name)
.setPriority(NotificationCompat.PRIORITY_HIGH)
//.setCategory(NotificationCompat.CATEGORY_SERVICE)
//.setOngoing(true)
//.setAutoCancel(true)
//.setOnlyAlertOnce(true)
.setColor(getResources().getColor(R.color.colorPrimaryDark))
.setContentIntent(pendingIntent)
.build();
startForeground(13, notification);
return START_STICKY;
}
Android 系统不会杀死我的应用程序,它会运行一整夜。但是在我打开屏幕之前传感器没有响应。
尝试获取解决了我的问题的 Wakelock。
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MService: WakeLock");
mWakeLock.acquire();
但是我不想用WakeLock
这么长时间。
有没有一种方法可以让手机的传感器长期保持活跃,或者我的前台服务/通知有什么问题?但是,我对具有更高优先级的前台服务进行了很多研究。
解决方案
推荐阅读
- jenkins - Jelly - 如何使用 jelly 标签中的参数调用 java 方法
- c# - What are the differences in code execution between Console App and Windows Service
- javascript - 如何在 html 中的 javascript 中显示内部图像
- java - 将查询转换为 java jdbc 模板中的参数化准备语句
- html - 使用 HTML /CSS /Angular 6 动画单击按钮后打开一个 div(如弹出窗口)
- javascript - Cordova 通用链接插件无法使用 IP 地址
- react-native - React Native createBottomTabNavigator 从常量添加自定义颜色
- java - 删除行后如何保证具有连续数字字段的列?
- php - how to make session login in wordpress
- android - 如何创建将用于支付应用程序用户 Uber 乘车费用的组织帐户?