![](/img/trans.png)
[英]Foreground service killed in Doze mode for some devices like oppo,vivo,MI, etc
[英]Background service in android is not running some devices like oppo,vivo etc
即使應用程序從堆棧中被殺死,我也想在后台運行服務。 此功能在某些設備上運行良好。 但是在 oppo 和 vivo 手機中,如果應用程序被殺死,它就不會運行。 是否有任何解決方案。 如果沒有,那么我如何打開允許權限屏幕。
這是我的代碼:
定時器服務.java
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.text.LoginFilter;
import android.util.Log;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import static com.gov.ojas1.WebView.TimerReceiver.CUSTOM_INTENT;
public class Timer_Service extends Service {
public static String str_receiver = "com.countdowntimerservice.receiver";
private Handler mHandler = new Handler();
Calendar calendar;
SimpleDateFormat simpleDateFormat;
String strDate;
Date date_current, date_diff;
SharedPreferences mpref;
SharedPreferences.Editor mEditor;
private Timer mTimer = null;
public static final long NOTIFY_INTERVAL = 1000;
Intent intent;
long int_timer;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mpref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
mEditor = mpref.edit();
calendar = Calendar.getInstance();
simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
mTimer = new Timer();
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 5, NOTIFY_INTERVAL);
intent = new Intent(str_receiver);
Random r = new Random();
int i1 = r.nextInt(10800000 - 7200000) + 7200000;
// int i1 = r.nextInt(300000 - 180000) + 180000;
// int_timer = (long) (int) i1;
int_timer =10000;
}
class TimeDisplayTimerTask extends TimerTask {
@Override
public void run() {
mHandler.post(new Runnable() {
@Override
public void run() {
calendar = Calendar.getInstance();
simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
strDate = simpleDateFormat.format(calendar.getTime());
Log.e("strDate", strDate);
twoDatesBetweenTime();
}
});
}
}
public String twoDatesBetweenTime() {
try {
date_current = simpleDateFormat.parse(strDate);
} catch (Exception e) {
}
try {
date_diff = simpleDateFormat.parse(mpref.getString("data", ""));
} catch (Exception e) {
}
try {
long diff = date_current.getTime() - date_diff.getTime();
Log.e("Diff", diff + "");
Log.e("TImer", int_timer + "");
long long_hours = int_timer - diff;
long diffSeconds2 = long_hours / 1000 % 60;
long diffMinutes2 = long_hours / (60 * 1000) % 60;
long diffHours2 = long_hours / (60 * 60 * 1000) % 24;
if (long_hours > 0) {
String str_testing = diffHours2 + ":" + diffMinutes2 + ":" + diffSeconds2;
Log.e("TIME", str_testing);
} else {
String str_testing = diffHours2 + ":" + diffMinutes2 + ":" + diffSeconds2;
mEditor.putString("data", strDate).commit();
fn_update();
Log.e("TIME", "Finish");
}
} catch (Exception e) {
mTimer.cancel();
mTimer.purge();
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 5, NOTIFY_INTERVAL);
e.printStackTrace();
}
return "";
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("Service finish", "Finish");
}
private void fn_update() {
Intent i = new Intent();
i.setAction(CUSTOM_INTENT);
i.putExtra("boolean", true);
sendBroadcast(i);
}
}
提前致謝。
對我有用的是在服務標簽的清單中使用android:process
標簽:
<service [name, etc]
android:process=":externalprocess"/>
這使它在單獨的進程上運行,因此服務不會與應用程序一起終止。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.