前台服务在 android oreo 中不工作

Foreground service not working in android oreo

在我的应用程序中,它在后台每 15 秒加载一次 url(API)。我的应用程序中不需要任何类型的 UI,所以我只在我的服务 class 中加载 url。为了在 oreo+ 设备上正常工作,我使用了 startForgroundService()。但是在应用程序被杀死后它在奥利奥中不起作用。我的测试设备是OPPO Oreo 8.1.0。它在 Samsung 7.0 上完美运行。

这是我的服务代码:

public class MyService extends Service {
    Handler handler = new Handler();
    Runnable runnable;
    int delay = 15 * 1000;
    String data;

    @Override
    public IBinder onBind(Intent intent) {
            return null;
    }

    @Override
    public int onStartCommand(final Intent intent, int flags, int startId) {
            data = intent.getStringExtra("data");
            loadURL(data);

            Intent notificationIntent = new Intent(this, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

            Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setContentTitle("URL App status :")
                    .setContentText("The app is running in background")
                    .setSmallIcon(R.drawable.ic_launcher_foreground)
                    .setContentIntent(pendingIntent)
                    .build();
            startForeground(1, notification);

            handler.postDelayed(runnable = new Runnable() {
                    public void run() {
                            Toast.makeText(MyService.this, "" + data, Toast.LENGTH_SHORT).show();
                            loadURL(data);

                            handler.postDelayed(runnable, delay);
                    }
            }, delay);
            return START_STICKY;
    }

    @Override
    public void onDestroy() {
            stopSelf();
    }

    public void loadURL(String data) {
            try {
                    RequestFuture<JSONObject> requestFuture = RequestFuture.newFuture();
                    final String mURL = "http://localhost/att.php?emp_id=" + data + "&status=&submit=Insert";
                    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,
                            mURL, new JSONObject(), requestFuture, requestFuture);
                    MySingleton.getInstance(this).addToRequestQueue(request);
                    Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();

            } catch (Exception e) {
                    Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
            }
    }
} 

App.class 通知:

public class App extends Application {
public static final String CHANNEL_ID = "channel ID";

@Override
public void onCreate() {
    super.onCreate();
    createnotificationChannel();
}
private void createnotificationChannel(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        NotificationChannel serviceChannel = new NotificationChannel(CHANNEL_ID, "URL channel ID",
                NotificationManager.IMPORTANCE_DEFAULT);
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(serviceChannel);
    }
}

}

以及启动前台服务的MainActivity :

public class MainActivity extends AppCompatActivity {

String data;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    data = getIntent().getStringExtra("ID");
    Intent intent = new Intent(MainActivity.this, MyService.class);
    intent.putExtra("data", data);
    ContextCompat.startForegroundService(this,intent);
}

我搜索了很多,但我无法找出我的代码有什么问题。只需要帮助!

读这个answer

Every service can be killed depending on its phone situation. From Android 8, there are some limits for Background Execution.

Foreground services continue running even when the user isn't interacting with the app.but it may be killed after the app closed.

如果您想在关闭应用程序后运行您的服务,请阅读此

这是由于制造商本身的电池优化器。您已在设置中手动允许背景。