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

相關問題