android中的后台服务没有运行像oppo,vivo等设备

听闻少年二字,当与平庸相斥。这篇文章主要讲述android中的后台服务没有运行像oppo,vivo等设备相关的知识,希望能为你提供帮助。
我想在后台运行服务,即使应用程序被从堆栈中杀死。此功能在某些设备上正常运行。但是在oppo和vivo手机中,如果应用程序被杀,它就无法运行。对此有什么解决方案吗?如果没有,那么我如何打开允许权限屏幕。
这是我的代码:
timer_service.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中的后台服务没有运行像oppo,vivo等设备】提前致谢。
答案您可以使您的服务在前台运行,这应该可以解决此问题。
您可以参考以下链接执行相同操作:
Android - implementing startForeground for a service?
另一答案对我有用的是在服务标签的清单中使用android:process标签:
< service [name, etc] android:process=":externalprocess"/>

这使得它在一个单独的进程上运行,因此服务不会与应用程序一起被杀死。

    推荐阅读