Android - 在后台运行服务以获取我的位置,并在靠近确定位置时显示通知

时间:2015-11-18 16:40:30

标签: android google-maps android-intent service background-process

我正在尝试使用Google Maps API制作应用。我的应用程序没问题,获取我的位置,并且在SetOnMyLocationChangeListener中,我向我显示一个警报,如果它靠近我的目的地。 现在我需要在后台运行此应用程序,当用户的位置靠近目的地时,显示通知以提醒他。即使应用程序没有打开。

我搜索了服务课,但我不明白它是如何工作的。

1 个答案:

答案 0 :(得分:4)

您必须执行以下步骤:

编辑:使用LocationManager

创建一个扩展Service的类。在onStart方法中,设置LocationManager并从intent extras中读取目标。您需要使用intent传递目标位置。

您需要将您的服务添加到清单中:

    <service
        android:name=".MyService"
        android:enabled="true"
        android:exported="false" >
    </service>

从您的任何活动中使用Intent启动服务。例如像这样

修改:将目的地添加到意图

Intent intent = new Intent(this, MyService.class);
intent.putExtra("destination", destination);
startService(intent);

以下是一个示例服务,它为每个位置更新创建通知并显示到目标的距离。您可以根据自己的需要进行调整。

import android.Manifest;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;


public class MyService extends Service {

    private Location mDestination;

    private LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {

            float distance = mDestination.distanceTo(location);

            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(MyService.this)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle("Distance")
                            .setContentText(Float.toString(distance));
            NotificationManager mNotificationManager =
                    (NotificationManager) getSystemService(
                            Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(1, mBuilder.build());
        }

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

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onProviderDisabled(String provider) {
        }
    };

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("MyService", "onStart: " + intent);

        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        mDestination = intent.getParcelableExtra("destination");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) !=
                    PackageManager.PERMISSION_GRANTED &&
                    checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) !=
                            PackageManager.PERMISSION_GRANTED) {
                return START_NOT_STICKY;
            }
        }
        locationManager.removeUpdates(locationListener);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
                locationListener);

        return START_STICKY;
    }
}