0

我正在尝试使用Google Maps API制作应用。我的应用程序是好的,获取我的位置,并在SetOnMyLocationChangeListener中显示给我一个警告,如果它靠近我的目的地。 现在我需要在后台运行这个应用程序,当用户的位置靠近目的地时,显示通知以提醒他。即使该应用程序未打开。Android - 在后台运行服务以获取我的位置并在接近确定位置时显示通知

我搜索了Service类,但我不明白它是如何工作的。

回答

2

你必须执行以下步骤:

编辑:使用的LocationManager

创建延伸服务类。在onStart方法中,设置LocationManager并从intent extras中读取目的地。您需要通过目的地的目的地。

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

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

从您的任何活动使用意图启动该服务。例如这样

编辑:添加目的地意图

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; 
    } 
} 
+0

感谢您的回答。使用此代码,服务将在应用程序打开或智能手机启动时启动? – FelipeRsN

+0

当您从其中一个活动调用startService(...)时,该服务就会启动。你想在电话启动时启动吗? –

+0

我试图实现这个代码,但GoogleApiClient无法连接。错误: getGoogleAppId失败,状态为:10 无法上传。应用程序测量已禁用 – FelipeRsN

相关问题