Android 前台服务在几个小时后停止
创建了Android服务,在后台更新位置并将其与Unity集成。服务开始时工作正常,但几个小时后服务停止,并且不会更新用户位置 下面是onStart命令的代码片段,OnCreate,Override方法Android 前台服务在几个小时后停止,android,unity3d,service,location,Android,Unity3d,Service,Location,创建了Android服务,在后台更新位置并将其与Unity集成。服务开始时工作正常,但几个小时后服务停止,并且不会更新用户位置 下面是onStart命令的代码片段,OnCreate,Override方法 public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); return START_STICKY; }
public int onStartCommand(Intent intent, int flags, int startId)
{
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
public void onCreate()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"channel_01",
"My Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
Notification.Builder builder = new Notification.Builder(getApplicationContext(), "channel_01");
startForeground(12345678, builder.build());
}
initializeLocationManager();
stopForeground(true);
}
任何帮助都将不胜感激。谢谢您是否尝试过扩展
JobService
?这对我来说很好,即使在重启手机之后
public class NotificationService extends JobService {
public static final String TASK_ID = "notification_task_id";
private static final String TAG = "NotificationJobService";
private boolean jobCancelled = false;
private String notificationTaskID;
@Override
public boolean onStartJob(JobParameters params) {
Log.d(TAG, "onStartJob: Job Started");
doBackgroundWork(params);
return true;
}
private void doBackgroundWork(final JobParameters params) {
new Thread(new Runnable() {
@Override
public void run() {
notifiy(params);
}
}).start();
}
public void notifiy(JobParameters params) { // dzieje się w przy notyfikacji
createNotification("Title", "message", 'channelID', getApplicationContext());
jobFinished(params, false);
}
public void createNotification(String aTitle, String aMessage, int channelID, Context context) {
// create your notification here
}
@Override
public boolean onStopJob(JobParameters params) {
Log.d(TAG, "onStopJob: Job Cancelled");
jobCancelled = true;
return true;
}
}
这就是你所说的:
ComponentName componentName = new ComponentName(this, NotificationJobService.class);
JobInfo info = new JobInfo.Builder(123, componentName)
.setRequiresCharging(false)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPersisted(true)
.setPeriodic(15*60*1000) // here you set the time
.build();
JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
int resultCode = scheduler.schedule(info);
if (resultCode == JobScheduler.RESULT_SUCCESS) {
Log.e(TAG, "scheduleJob: Job scheduled");
} else {
Log.e(TAG, "scheduleJob: Job schedule failed");
}
您是否尝试过扩展
JobService
?这对我来说很好,即使在重启手机之后
public class NotificationService extends JobService {
public static final String TASK_ID = "notification_task_id";
private static final String TAG = "NotificationJobService";
private boolean jobCancelled = false;
private String notificationTaskID;
@Override
public boolean onStartJob(JobParameters params) {
Log.d(TAG, "onStartJob: Job Started");
doBackgroundWork(params);
return true;
}
private void doBackgroundWork(final JobParameters params) {
new Thread(new Runnable() {
@Override
public void run() {
notifiy(params);
}
}).start();
}
public void notifiy(JobParameters params) { // dzieje się w przy notyfikacji
createNotification("Title", "message", 'channelID', getApplicationContext());
jobFinished(params, false);
}
public void createNotification(String aTitle, String aMessage, int channelID, Context context) {
// create your notification here
}
@Override
public boolean onStopJob(JobParameters params) {
Log.d(TAG, "onStopJob: Job Cancelled");
jobCancelled = true;
return true;
}
}
这就是你所说的:
ComponentName componentName = new ComponentName(this, NotificationJobService.class);
JobInfo info = new JobInfo.Builder(123, componentName)
.setRequiresCharging(false)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPersisted(true)
.setPeriodic(15*60*1000) // here you set the time
.build();
JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
int resultCode = scheduler.schedule(info);
if (resultCode == JobScheduler.RESULT_SUCCESS) {
Log.e(TAG, "scheduleJob: Job scheduled");
} else {
Log.e(TAG, "scheduleJob: Job schedule failed");
}
如果您需要“永远”运行,您的服务必须保持前台状态。问题是你正在停止你的前景 试试这个:
import android.app.Notification;
...
public void onCreate()
{
super.onCreate();
...
// Build the notification.
Notification notification = notificationBuilder.build();
// And Start Foreground.
startForeground(SERVICE_ID, notification); // Where SERVICE_ID is any integer.
}
以及:
注意:您可以参考此示例
编辑:
如果你真的需要在后台(而不是前台)进行维护,你可以参考Workers。更多信息请点击此处:
请记住,在后台的位置可能不太频繁(每15分钟更新一次)。但在前台,您可以使其更加频繁。如果您需要“永远”运行,您的服务必须保持前台状态。问题是你正在停止你的前景 试试这个:
import android.app.Notification;
...
public void onCreate()
{
super.onCreate();
...
// Build the notification.
Notification notification = notificationBuilder.build();
// And Start Foreground.
startForeground(SERVICE_ID, notification); // Where SERVICE_ID is any integer.
}
以及:
注意:您可以参考此示例
编辑:
如果你真的需要在后台(而不是前台)进行维护,你可以参考Workers。更多信息请点击此处:
请记住,在后台的位置可能不太频繁(每15分钟更新一次)。但在前台,你可以让它更频繁。不,我还没有试过求职服务。我的主要问题是,我需要编写一个服务库项目,以便以后可以将其与unity集成。但我也会尝试一下这个解决方案。希望它能起作用。Thankscan我将JobService时间设置为每隔几秒钟执行一次,以立即更新后台的用户位置。我对JobService和Job Scheduler进行了一些探索,我知道Job Scheduler在一小时内运行2-3次。我为您找到了类似的东西
builder.setMinimumLatency(1*1000);//至少等待builder.setOverridedAdline(3*1000);//最大延迟
我每10-15分钟使用一次,以保持对传入连接的控制,但我使用它的另一个目的是不,我没有尝试过作业服务。我的主要问题是,我需要编写一个服务库项目,以便以后可以将其与unity集成。但我也会尝试一下这个解决方案。希望它能起作用。Thankscan我将JobService时间设置为每隔几秒钟执行一次,以立即更新后台的用户位置。我对JobService和Job Scheduler进行了一些探索,我知道Job Scheduler在一小时内运行2-3次。我为您找到了类似的东西builder.setMinimumLatency(1*1000);//至少等待builder.setOverridedAdline(3*1000);//最大延迟
我每10-15分钟使用一次,以保持对传入连接的控制,但我使用它的另一个目的是感谢peter的详细回答。当应用程序进入后台时,是否可以在不显示通知栏的情况下作为前台运行服务?@AsadAli很抱歉,但如中所述,“前台服务必须显示通知”。如果该服务位于前台,则通知是强制性的,但如果它位于后台(而非前台),则通知不是强制性的。您好,您知道是否有可能知道Android是否会终止该服务吗?谢谢peter的详细回答。当应用程序进入后台时,是否可以在不显示通知栏的情况下作为前台运行服务?@AsadAli很抱歉,但如中所述,“前台服务必须显示通知”。如果服务在前台,那么通知是强制性的,但是如果它在后台(而不是前台),那么通知不是强制性的。您好,您知道是否有可能知道Android是否会终止服务吗?