所以我有一个恼人的问题。我目前正在开发一个用于跟踪用户睡眠的应用程序。我们有一项服务可以发送声纳并收听返回的波。为此,该服务需要整夜运行,并且由于其他一些问题,我们需要锁定屏幕。该服务由一个对象启动,我们将接口传递给该对象以处理与处理相关的回调。当前,调用活动正在实现此接口以正确响应回调。
现在我遇到的问题有时是在监视会话期间,服务被杀死或应用程序被杀死。为了消除被系统杀死的可能性,我正在寻找策略来尝试向系统识别应用程序正在运行并且即使屏幕被锁定也需要整夜运行。
首先我尝试了一个唤醒锁:PowerManager.PARTIAL_WAKE_LOCK
我最近在会话运行时添加了一个托盘通知,以尝试使其保持活动状态,但效果不佳
public class Notification extends ContextWrapper {
Context context;
public Notification(Context base) {
super(base);
this.context = base;
createChannels();
}
private NotificationManager mManager;
public void createChannels() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel sessionChannel = new NotificationChannel(Constants.Notification.TRACKING_CHANNEL,
Constants.Notification.ANDROID_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
sessionChannel.enableLights(false);
sessionChannel.enableVibration(false);
getManager().createNotificationChannel(sessionChannel);
NotificationChannel bedtimeChannel = new NotificationChannel(Constants.Notification.BEDTIME_CHANNEL,
Constants.Notification.ANDROID_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
getManager().createNotificationChannel(bedtimeChannel);
}
}
public NotificationManager getManager() {
if (mManager == null) {
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return mManager;
}
public NotificationCompat.Builder getTrackingChannelNotification(String title, String body) {
Intent trackingIntent = new Intent(context, SessionRecordingActivity.class);
trackingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, trackingIntent, 0);
return new NotificationCompat.Builder(getApplicationContext(), Constants.Notification.TRACKING_CHANNEL)
.setContentTitle(title)
.setContentText(body)
.setContentIntent(pendingIntent)
.setOngoing(true)
.setSmallIcon(R.mipmap.ic_launcher_new);
}
public NotificationCompat.Builder getBedTimeChannelNotification(String title, String body, Intent actionIntent) {
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, actionIntent, 0);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
return new NotificationCompat.Builder(getApplicationContext(), Constants.Notification.TRACKING_CHANNEL)
.setSmallIcon(R.mipmap.ic_launcher_new)
.setContentTitle(title)
.setContentText(body)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setSound(defaultSoundUri)
.setAutoCancel(true);
}
}
这是服务启动后我所做的:
NotificationCompat.Builder nb = mNotification.getTrackingChannelNotification(getString(R.string.tracking_in_progress), getString(R.string.sleep_well));
mNotification.getManager().notify(Constants.Notification.TRACKING_ID, nb.build());
然后在会话结束时执行此操作:
mNotification.getManager().cancel(Constants.Notification.TRACKING_ID);
所以我的问题是:我还能做些什么来识别我的应用程序需要继续运行直到用户结束它的系统?(作为一个快速的旁注,服务不是由我的应用程序直接启动的,它是启动服务的第三方库)。
编辑:经过进一步调查,现在看来可能是我的活动/应用程序被杀死而不是服务。当屏幕锁定时,除了唤醒锁定或托盘通知之外,还有其他方法可以让我的应用程序保持活动状态吗?