所以我有一个令人烦恼的问题。我目前正在开发用于跟踪用户睡眠的应用程序。我们有一项服务,可以发出声纳并收听回波。为此,该服务需要通宵运行,并且由于其他一些问题,我们需要锁定屏幕。该服务由一个对象启动,我们将一个接口传递给该对象以处理与处理相关的回调。当前,调用活动正在实现此接口,以对回调进行适当的响应。
现在,我遇到的问题有时是在监视会话期间出现的,要么服务被终止,要么应用程序被终止。为了消除被系统杀死的可能性,我正在寻找策略来尝试向系统识别该应用程序正在运行,并且即使屏幕被锁定也需要整夜运行。
首先,我尝试使用以下命令进行唤醒锁: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);
因此,我的问题是:我还能做些什么来识别应用程序需要保持运行直到用户结束的系统? (作为一个简短的说明,该服务不是直接由我的应用启动的,而是由第三方库启动的服务)。
编辑:经过进一步调查,现在看来可能是我的活动/应用程序被杀死了,而不是服务。当屏幕锁定时,除了唤醒锁定或托盘通知以外,还有其他方法可以使我的应用程序保持活动状态吗?
最佳答案
您必须为使用startForeground
的服务呼叫Oncreate()
中的Context.startForegroundService()
。
@Override
public void onCreate() {
super.onCreate();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String CHANNEL_ID = "my_channel_01";
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("")
.setContentText("").build();
startForeground(1, notification);
}
}
您可以从此link中找到更多信息