当应用程序不在前台时,Android服务停止工作 [英] Android Service stops working, when app isn't on the foreground

查看:129
本文介绍了当应用程序不在前台时,Android服务停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题:我从Service收到LocationListener给定的速度值.但是,当我关闭应用程序的用户界面时,locationlistener停止发送更新.有谁知道该怎么办?即使没有使用该应用程序,我也需要它来继续更新...

I've got a little problem: I recieve from a Service a speed value given by the LocationListener. But when i close the ui of my app then the locationlistener stops sending updates. Does anyone have an idea what to do? I need it to continue the updates, even if the app is not in use...

这是我的代码:

public class BackroundService extends Service {


//initializing the Location Manager and Listener
LocationManager locationManager;
LocationListener locationListener;


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

@SuppressLint("MissingPermission")
@Override
public void onCreate() {

}

@SuppressLint("MissingPermission")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    //Instantiating the device manager an  listener
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationListener = new MyLocationListener();
    locationManager.requestLocationUpdates("gps", 500, 0, locationListener);

    return START_STICKY;
}




}


class MyLocationListener extends Service implements LocationListener
{
public float speed;

public void onLocationChanged(final Location location)
{
    //when the location changed

    Log.d("Location", ""+location);
    Log.d("Float Location", ""+ location.getLongitude() + "     " + location.getLatitude());
    Log.d("Speed", "    "+location.getSpeed());

    //initializing the variable speed with the speed number given by the LocationListener
    speed = location.getSpeed();

    //creating a broadcast reciever
    Intent intent = new Intent("speed_update");
    intent.putExtra("speed", speed);
    //sending speed to main activity
    sendBroadcast(intent);

    //checking if speed is in walking range
    if (speed < 4.0 && speed > 0.4){
        Log.d("######Checker######", "++++++++++turn off+++++++++++");
        //Toast.makeText(MyLocationListener.this, "turn off", Toast.LENGTH_SHORT).show();
    }
}


public void onProviderDisabled(String provider)
{

}


public void onProviderEnabled(String provider)
{

}


public void onStatusChanged(String provider, int status, Bundle extras)
{

}

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

推荐答案

从奥利奥(Oreo)开始,Android后台政策发生了重大变化,请参考

From oreo there's major changes in android background policy, please refer this https://developer.android.com/about/versions/oreo/background for more details, in order to avoid your service from getting killed by android you have to run it as a foreground service

第1步:为清单文件添加权限

Step 1: Add permission to manifest file

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

此处提供了有关此权限的更多详细信息 https://developer.android.com/reference/android/Manifest.permission#FOREGROUND_SERVICE

heres more details on this permission https://developer.android.com/reference/android/Manifest.permission#FOREGROUND_SERVICE

第2步:创建服务,我为您共享示例代码

Step 2: Create service, I'm sharing example code for you

public class DemoService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("SSB Log", "onStartCommand");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // For foreground service
            Intent notificationIntent = new Intent(this, DemoService.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

            // Creating channel for notification
            String id = DemoService.class.getSimpleName();
            String name = DemoService.class.getSimpleName();
            NotificationChannel notificationChannel = new NotificationChannel(id,
                    name, NotificationManager.IMPORTANCE_NONE);
            NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            service.createNotificationChannel(notificationChannel);

            // Foreground notification
            Notification notification = new Notification.Builder(this, id)
                    .setContentTitle(getText(R.string.app_name))
                    .setContentText("Show service running reason to user")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentIntent(pendingIntent)
                    .setTicker("Ticker text")
                    .build();

            startForeground(9, notification);
        }
        // Service logic here

        return Service.START_NOT_STICKY;
    }
}

第3步:在清单中声明服务

Step 3: Declare service in manifest

<service android:name=".DemoService" />

第4步:从活动开始服务

Step 4: Start service from activity

Intent startDemoService = new Intent(this,DemoService.class);
    startService(startDemoService);

这篇关于当应用程序不在前台时,Android服务停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆