【问题标题】:Foreground service not working in android oreo前台服务在android oreo中不起作用
【发布时间】:2019-07-22 12:07:39
【问题描述】:

在我的应用程序中,它每 15 秒在后台加载一个 url(API)。我的应用程序不需要任何类型的 UI,所以我只需在我的服务类中加载 url。为了在 oreo+ 设备上正常工作,我使用了 startForgroundService()。但是在应用程序被杀死后,它在 Oreo 中不起作用。我的测试设备是OPPO Oreo 8.1.0。它在三星 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);
}

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

【问题讨论】:

  • 请检查第一个服务是否在前台运行。
  • 杀死应用时没有服务没有运行
  • 成为前台并不意味着你不能被杀死,它只会降低你在优先级列表中的位置。您仍然不能指望在 Android 中永远运行的服务。而且我非常非常怀疑您是否需要在后台每 15 秒下载一个 url,这可能是您真正想做的任何事情的糟糕实现。

标签: android android-service android-volley foreground-service


【解决方案1】:

阅读此answer

每个服务都可以根据其电话情况被终止。从 Android 8 开始,有一些 limits 用于后台执行。

前台services 继续运行,即使用户不在 与应用交互。但应用关闭后可能会被杀死。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2019-10-26
      • 1970-01-01
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-14
      • 2019-03-25
      • 1970-01-01
      相关资源
      最近更新 更多