[英]Why foreground service stops when screen is off on Android Oreo?
當我關閉屏幕時,Android Oreo會停止我的前台服務。 當設備被拔掉時會發生這種情況。 我在華為MediaPad T5上測試我的應用程序。 我使用Handler.postDelayed每30秒發送一次測試請求。
我在Android 8中閱讀了有關后台執行限制的內容。在編寫遷移指南時,該前台服務應該可以正常工作。 我不能使用JobScheduler,JobIntenService或WorkManager,因為它們只能每15分鍾重復一次。
我無法使用Firebase雲消息傳遞,因為我的應用程序可以脫機工作。
我也使用綁定服務,因為它不應該有后台限制。 不幸的是,我的應用仍然無法正常工作。
我嘗試使用WakeLock,為另一個進程提供服務,AlarmManager,將我的應用程序添加到白名單,它仍然無法正常工作。
我通過簡單的post reqest測試在后台運行。 我通過Retrofit Library連接到測試服務器。
主要活動
private LocalService mService;
private boolean mBound = false;
private Intent mIntent;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
LocalService.LocalBinder binder = (LocalService.LocalBinder) service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
mService = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIntent = new Intent(this, LocalService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ContextCompat.startForegroundService(getApplicationContext(), mIntent);
}
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
cpuLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "kb:wl");
}
@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(this, LocalService.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
cpuLock.acquire();
}
public void onButtonClick(View v) {
if (mBound) {
bindService(mIntent, connection, BIND_AUTO_CREATE);
mService.send();
}
}
@Override
protected void onRestart() {
super.onRestart();
cpuLock.release();
}
LocalService類
public class LocalService extends Service {
private IBinder mBinder = new LocalBinder();
Handler mHandler;
String DEBUG_TAG = "local";
public class LocalBinder extends Binder {
LocalService getService() {
return LocalService.this;
}
}
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onCreate() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = "some_channel_id";
CharSequence channelName = "Channel La Manche";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
notificationManager.createNotificationChannel(notificationChannel);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification =
new Notification.Builder(this, channelId)
.setContentTitle("post title")
.setContentText("post text")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(pendingIntent)
.setTicker("post ticker")
.setOngoing(true)
.build();
startForeground(1, notification);
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
mHandler = new Handler();
return mBinder;
}
public void send() {
mHandler.postDelayed(test, 1000);
}
private Runnable test = new Runnable() {
public void run() {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.baseUrl("https://test.server.url/")
.build();
Post servicePost = retrofit.create(Post.class);
Call<String> request = servicePost.send("");
request.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
Log.i(DEBUG_TAG, "Code " + response.code());
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
Log.i(DEBUG_TAG, "Not connected to server. " + t.getMessage());
}
});
mHandler.postDelayed(test, 30 * 1000);
}
};
發布界面
@Headers("Content-Type: application/json" )
@POST("api/test")
Call<String> send(@Body String empty);
當平板電腦正在充電或未充電且屏幕打開時,應用程序非常完美。 即使屏幕關閉,Endomondo也能正常工作。 我錯了什么?
您可以借助此存儲庫解決您的問題。 這個存儲庫是為了位置,但我相信它會對你有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.