Я хочу провести измерения сигнала.Мое действие приложения принимает измерения (и предоставляют диаграммы), когда на переднем плане и в событии onPause я вызываю и связываю службу для проведения измерений (и сохранения их в базе данных), чтобы заменить действие.Однако, если телефон отключен, приложение прекратит измерения.Я изучил много других постов и реализовал их на переднем плане с уведомлением.Вот пример кода из Службы
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent,START_STICKY,startId);
goForeground();
return Service.START_STICKY; }
private void goForeground() {
Log.i(TAG ,"goForeground");
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Notification n = new Notification.Builder(this)
.setContentTitle("Measurements Service")
.setContentText("App still collects measurements.")
.setSmallIcon(R.drawable.myApp)
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.build();
NotificationManager notificationManger =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManger.notify(01, n);
startForeground(FOREGROUND_NOTIFICATION_ID, n);
}
и MainActivity
Intent serviceIntent = new Intent(MainActivity.this, SnoopService.class);
if (SnoopService.isRunning()) {
Log.d(TAG, "Service is running");
doBindService(intent);
} else {
Log.d(TAG, "Service will run");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
} else
startService(intent);
doBindService(intent);
}
isBound = true;
}
Кто-нибудь знает, если я что-то не так делаю?Я тестировал его с Samsung Galaxy S8.