Android 在实时应用程序的后台运行重复任务
我正在编写一个应用程序,它不断地监听和检查传感器(几乎所有可用的传感器),并将数据保存到设备中的数据库中 我需要用这些数据每隔X秒进行一些计算,如果计算结果显示是这样的话,我会抛出一个新事件。 我正在考虑在使用应用程序时请求将设备插入(关于电池耗电) 对于需要进行计算并抛出事件的任务,最好的方法是什么?计时器?线程?AsynkTask?报警管理器?另一种方法 我想继续获取传感器数据并将其保存到数据库,即使应用程序不在前台……只要应用程序没有被用户停止,它就应该保存这些值。 其中一个选项是唤醒锁(部分唤醒锁,保持CPU运行) 我想听听不同的意见。Android 在实时应用程序的后台运行重复任务,android,real-time,alarmmanager,Android,Real Time,Alarmmanager,我正在编写一个应用程序,它不断地监听和检查传感器(几乎所有可用的传感器),并将数据保存到设备中的数据库中 我需要用这些数据每隔X秒进行一些计算,如果计算结果显示是这样的话,我会抛出一个新事件。 我正在考虑在使用应用程序时请求将设备插入(关于电池耗电) 对于需要进行计算并抛出事件的任务,最好的方法是什么?计时器?线程?AsynkTask?报警管理器?另一种方法 我想继续获取传感器数据并将其保存到数据库,即使应用程序不在前台……只要应用程序没有被用户停止,它就应该保存这些值。 其中一个选项是唤醒锁(
提前谢谢!Guillermo.您可以使用
AlarmManager
设置重复任务(这是Android首选的设置未来/重复任务的方式)。要使计算使用服务
(如果您认为计算会很昂贵,请考虑将其移动到单独的工作线程或使用IntentService
)
关于唤醒锁(来自AlarmManager参考):
报警管理器保持CPU唤醒
只要报警接收器
正在执行onReceive()方法。这
保证手机不会
睡到你处理完为止
广播。一旦接收()
返回,则报警管理器释放
这是尾流锁。这意味着
在某些情况下,手机会尽快入睡
当onReceive()方法完成时。
如果你的闹钟响了
Context.startService(),这是可能的
手机会在开机前休眠
请求的服务已启动。到
防止这种情况,你的广播接收器
服务将需要实现一个
单独的唤醒锁定策略以确保
手机会一直运行到
该服务可用
这是我不久前为记录CPU频率而编写的服务的修改片段。它缺少
应用程序
和活动
部分,但说明了我如何编写服务
以保持每10秒记录一次日志。当手机进入深度睡眠时,它不会记录,所以如果你想在没有中断的情况下登录,那么你就需要获取代码> PialalxWaKeYOLKION/COD>S,但是考虑到电池寿命会严重减少。
public class YOURCLASS_Service extends Service {
private long mStartTime = 0L;
private final Handler mHandler = new Handler();
private Runnable mUpdateTimeTask;
private YOURAPP app;
@Override
public void onCreate() {
super.onCreate();
app = (YOURAPP) getApplicationContext();
}
@Override
public void onDestroy() {
Toast.makeText(this, "Service finished.", Toast.LENGTH_SHORT).show();
stopLog ();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (app.isRunning())
return START_STICKY;
try {
File file = new File(Environment.getExternalStorageDirectory(), "yourlog.csv");
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file, false));
out.write("Log title");
out.close();
} catch (java.io.IOException e) {
stopLog ();
Toast.makeText(this, "Error creating log file. Aborting.", Toast.LENGTH_SHORT).show();
}
mUpdateTimeTask = new Runnable() {
public void run() {
long millis = SystemClock.uptimeMillis() - mStartTime;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
readYourSensors ();
if (!writeLog (str)) stopLog();
mHandler.postAtTime(this, mStartTime + (((minutes * 60) + seconds + 10) * 1000));
mHandler.postDelayed (mUpdateTimeTask, 10000);
}};
mStartTime = SystemClock.uptimeMillis();
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 100);
Notification notification = new Notification(R.drawable.notification_icon, "App title", System.currentTimeMillis());
Intent notificationIntent = new Intent(this, YOURCLASS.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), "App title", "Please see /sdcard/yourlog.csv", contentIntent);
startForeground(startId, notification);
app.isRunning(true);
return START_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
public void stopLog () {
mHandler.removeCallbacks(mUpdateTimeTask);
}
}
对不起,你能进一步解释一下吗?我应该在哪里启动服务和警报?警报和服务之间的关系是什么?我经常考虑使用广播接收器并在那里进行计算,为什么“让计算使用服务”?首先通过AlarmManager设置警报,然后在警报接收器中启动服务。你不需要UI,所以你不会使用活动,你应该考虑为这种情况提供服务。我明白了,谢谢!我来试一试。杰夫·阿特伍德已经关闭了这个问题的原始副本,所以我想我们应该保留这个问题。