发送位置更新IntentService [英] Send location updates to IntentService

查看:295
本文介绍了发送位置更新IntentService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何才能位置更新直接发送到意向服务?下面的方法是行不通的。 OnConnected函数被调用但随后的意图从来没有在服务接收到的:

  ...
    私人PendingIntent getLocationPendingIntent(布尔shouldCreate){
        意图广播=新的意图(m_context,LocationUpdateService.class);
        诠释标志= shouldCreate? 0:PendingIntent.FLAG_NO_CREATE;
        返回PendingIntent.getService(m_context,0,广播,旗);
    }



    @覆盖
    公共无效onConnected(捆绑为arg0){
        PendingIntent locationPendingIntent = getLocationPendingIntent(真正的);
        LocationRequest locationRequest =新LocationRequest();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(LOCATION_UPDATE_INTERVAL);
        locationRequest.setFastestInterval(LOCATION_FASTEST_UPDATE_INTERVAL);
        LocationServices.FusedLocationApi.requestLocationUpdates(m_googleApiClient,locationRequest,locationPendingIntent);
}
...
 

意图服务:

 进口android.app.IntentService;
进口android.content.Intent;
进口android.util.Log;

公共类LocationUpdateService扩展IntentService {

    公共LocationUpdateService(){
        超级(LocationUpdateService.class.getName());
    }


    @覆盖
    公众诠释onStartCommand(意向意图,诠释标志,诠释startID){
        super.onStartCommand(意向,标志,startID);
        Log.d(LocationUpdateService,位置接到);
        返回START_REDELIVER_INTENT;
    }

    @覆盖
    保护无效onHandleIntent(意向意图){
        Log.d(LocationUpdateService,意图收到);

    }
}
 

清单文件:

  ...
<服务机器人:名称=。LocationUpdateService/>
...
 

解决方案

编辑:最近,我想通了,你可以直接有一个IntentService手柄位置更新

下面是工作和测试code表示成功设置了一个IntentService作为接收机中包含的Pe​​ndingIntent用于位置更新的意图,基于code发现的这里

首先, IntentService

 进口android.app.IntentService;
进口android.content.Intent;
进口android.location.Location;
进口android.util.Log;
进口com.google.android.gms.location.FusedLocationProviderApi;

公共类LocationUpdateService扩展IntentService {

    私人最终字符串变量=LocationUpdateService;
    地点位置;

    公共LocationUpdateService(){

        超级(LocationUpdateService);
    }

    @覆盖
    保护无效onHandleIntent(意向意图){

        如果(intent.hasExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED)){

            位置= intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);

            Log.d(locationtesting,准确性:+ location.getAccuracy()+纬度:+ location.getLatitude()+离子:+ location.getLongitude());
        }
    }
}
 

这里是活动code的注册与位置更新一个 PendingIntent 被发送到 IntentService

 进口android.app.PendingIntent;
进口android.os.Bundle;
进口android.content.Intent;
进口android.app.Activity;
进口android.widget.Toast;
进口com.google.android.gms.common.ConnectionResult;
进口com.google.android.gms.common.api.GoogleApiClient;
进口com.google.android.gms.location.LocationRequest;
进口com.google.android.gms.location.LocationServices;

公共类MainActivity扩展活动器具
        GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {

    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    PendingIntent mRequestLocationUpdatesPendingIntent;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        buildGoogleApiClient();
        mGoogleApiClient.connect();
    }

    @覆盖
    保护无效的onPause(){
        super.onPause();
        如果(mGoogleApiClient!= NULL){
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,mRequestLocationUpdatesPendingIntent);
        }
    }

    保护同步无效buildGoogleApiClient(){
        Toast.makeText(这一点,buildGoogleApiClient,Toast.LENGTH_SHORT).show();
        mGoogleApiClient =新GoogleApiClient.Builder(本)
                .addConnectionCallbacks(本)
                .addOnConnectionFailedListener(本)
                .addApi(LocationServices.API)
                。建立();
    }

    @覆盖
    公共无效onConnected(束捆){
        Toast.makeText(这一点,onConnected,Toast.LENGTH_SHORT).show();

        mLocationRequest =新LocationRequest();
        mLocationRequest.setInterval(10);
        mLocationRequest.setFastestInterval(10);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        //mLocationRequest.setSmallestDisplacement(0.1F);

        //创建意图使用WebViewActivity处理结果
        意图mRequestLocationUpdatesIntent =新的意图(这一点,LocationUpdateService.class);

        //创建一个PendingIntent
        mRequestLocationUpdatesPendingIntent = PendingIntent.getService(getApplicationContext(),0,
                mRequestLocationUpdatesIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        //请求位置更新
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
                mLocationRequest,
                mRequestLocationUpdatesPendingIntent);
    }

    @覆盖
    公共无效onConnectionSuspended(int i)以{
        Toast.makeText(这一点,onConnectionSuspended,Toast.LENGTH_SHORT).show();
    }

    @覆盖
    公共无效onConnectionFailed(ConnectionResult connectionResult){
        Toast.makeText(这一点,onConnectionFailed,Toast.LENGTH_SHORT).show();
    }
}
 

生成的日志:

  D / locationtesting:准确度:10.0纬度:37.779702经度:-122.3931595
 D / locationtesting:准确度:10.0纬度:37.7797023经度:-122.3931594
 D / locationtesting:准确度:10.0纬度:37.7797022经度:-122.3931596
 D / locationtesting:准确度:10.0纬度:37.7797021经度:-122.3931597
 D / locationtesting:准确度:10.0纬度:37.7797021经度:-122.3931596
 D / locationtesting:准确度:10.0纬度:37.7797019经度:-122.3931597
 D / locationtesting:准确度:10.0纬度:37.7797019经度:-122.3931597
 

原来的答案,在这里把这个作为一个替代实现:

这是另一种实现仅仅是让 LocationListener的本地,只是发送意图 IntentService 每个 onLocationChanged()回调。

我刚刚得到一个执行的工作和测试,我用了一个经纬度对象的意图,因为它小,它的Parcelable。 请注意,位置对象是Parcelable还有,如果你需要额外的信息,它包括,你可以只发送位置在发送给IntentService的意图附加使用下文概述的同样的方法。 请参见文档

请注意,您可以使用 PendingIntent.send() Context.startService()的意图发送到该IntentService,对于差异<一个href="http://stackoverflow.com/questions/9152924/android-start-service-with-context-startservice-vs-pendingintent-getservice">see这里。对于这个简单的例子,我用 Context.startService()

我用了一个活动的 LocationListener的在这种情况下,但你可以使用实现的任何类 LocationListener的接口。

首先,略作修改 IntentService

 进口android.app.IntentService;
进口android.content.Intent;
进口android.util.Log;
进口com.google.android.gms.maps.model.LatLng;

公共类LocationUpdateService扩展IntentService {

    公共LocationUpdateService(){
        超级(LocationUpdateService.class.getName());
    }

    @覆盖
    公众诠释onStartCommand(意向意图,诠释标志,诠释startID){
        super.onStartCommand(意向,标志,startID);
        Log.d(LocationUpdateService,位置接到);
        返回START_REDELIVER_INTENT;
    }

    @覆盖
    保护无效onHandleIntent(意向意图){
        Log.d(LocationUpdateService,意图收到);

        经纬度经纬度= intent.getParcelableExtra(位置);
        Log.d(LocationUpdateService,意图收到:+ latLng.latitude ++ latLng.longitude);
    }
}
 

AndroidManifest.xml中:

 &LT;服务机器人:启用=真正的机器人。LocationUpdateServiceNAME = /&GT;
 

然后,将活动作为LocationListener的:

  mLocationRequest =新LocationRequest();
    mLocationRequest.setInterval(10); //小测试值
    mLocationRequest.setFastestInterval(1); //小测试值
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,这一点);
 

然后每个 onLocationChanged()回调,发送意图的IntentService,并发送经纬度的意图通过使用 putParcelable()在一个软件包:

  @覆盖
公共无效onLocationChanged(位置定位){

    mLatitudeText.setText(将String.valueOf(location.getLatitude()));
    mLongitudeText.setText(将String.valueOf(location.getLongitude()));

    意图广播=新的意图(这一点,LocationUpdateService.class);
    叠B =新包();
    b.putParcelable(位置,新的经纬度(location.getLatitude(),location.getLongitude()));
    broadcast.putExtras(B);

    。getApplicationContext()startService(广播);
}
 

全部放在一起,下面是完整的活动code:

 进口android.content.Intent;
进口android.os.Bundle;
进口android.app.Activity;
进口android.location.Location;
进口android.widget.TextView;
进口android.widget.Toast;
进口com.google.android.gms.common.ConnectionResult;
进口com.google.android.gms.common.api.GoogleApiClient;
进口com.google.android.gms.location.LocationRequest;
进口com.google.android.gms.location.LocationServices;
进口com.google.android.gms.location.LocationListener;
进口com.google.android.gms.maps.model.LatLng;

公共类MainActivity扩展活动器具
        GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener的{

    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    私人TextView的mLatitudeText;
    私人TextView的mLongitudeText;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        mLatitudeText =(TextView中)findViewById(R.id.latitudeTextView);
        mLongitudeText =(TextView中)findViewById(R.id.longitudeTextView);

        buildGoogleApiClient();

        mGoogleApiClient.connect();

    }

    @覆盖
    保护无效的onPause(){
        super.onPause();
        如果(mGoogleApiClient!= NULL){
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,这一点);
        }
    }

    保护同步无效buildGoogleApiClient(){
        Toast.makeText(这一点,buildGoogleApiClient,Toast.LENGTH_SHORT).show();
        mGoogleApiClient =新GoogleApiClient.Builder(本)
                .addConnectionCallbacks(本)
                .addOnConnectionFailedListener(本)
                .addApi(LocationServices.API)
                。建立();
    }

    @覆盖
    公共无效onConnected(束捆){
        Toast.makeText(这一点,onConnected,Toast.LENGTH_SHORT).show();
        位置mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        如果(mLastLocation!= NULL){
            mLatitudeText.setText(将String.valueOf(mLastLocation.getLatitude()));
            mLongitudeText.setText(将String.valueOf(mLastLocation.getLongitude()));
        }

        mLocationRequest =新LocationRequest();
        mLocationRequest.setInterval(10);
        mLocationRequest.setFastestInterval(1);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        //mLocationRequest.setSmallestDisplacement(0.1F);

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,这一点);
    }

    @覆盖
    公共无效onConnectionSuspended(int i)以{
        Toast.makeText(这一点,onConnectionSuspended,Toast.LENGTH_SHORT).show();
    }

    @覆盖
    公共无效onConnectionFailed(ConnectionResult connectionResult){
        Toast.makeText(这一点,onConnectionFailed,Toast.LENGTH_SHORT).show();
    }

    @覆盖
    公共无效onLocationChanged(位置定位){

        mLatitudeText.setText(将String.valueOf(location.getLatitude()));
        mLongitudeText.setText(将String.valueOf(location.getLongitude()));

        意图广播=新的意图(这一点,LocationUpdateService.class);
        叠B =新包();
        b.putParcelable(位置,新的经纬度(location.getLatitude(),location.getLongitude()));
        broadcast.putExtras(B);

        。getApplicationContext()startService(广播);
    }

}
 

最后,证明它的作品!下面是从IntentService日志:

  D / LocationUpdateService:接收的位置
 D / LocationUpdateService:意向接收
 D / LocationUpdateService:收到的意向:37.96396396396396 -122.10874797086674
 D / LocationUpdateService:位置获得
 D / LocationUpdateService:意向接收
 D / LocationUpdateService:收到的意向:37.96396396396396 -122.10874797086674
 

How can location updates be sent directly to Intent Service? The following approach does not work. OnConnected function is called but then the intent is never received in the service:

...
    private PendingIntent getLocationPendingIntent(boolean shouldCreate) {
        Intent broadcast = new Intent(m_context,LocationUpdateService.class);
        int flags = shouldCreate ? 0 : PendingIntent.FLAG_NO_CREATE;
        return PendingIntent.getService(m_context, 0, broadcast, flags);
    }



    @Override
    public void onConnected(Bundle arg0) {
        PendingIntent locationPendingIntent = getLocationPendingIntent(true);        
        LocationRequest locationRequest = new LocationRequest();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(LOCATION_UPDATE_INTERVAL);
        locationRequest.setFastestInterval(LOCATION_FASTEST_UPDATE_INTERVAL);
        LocationServices.FusedLocationApi.requestLocationUpdates(m_googleApiClient, locationRequest,locationPendingIntent);
}
...

Intent Service:

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

public class LocationUpdateService extends IntentService {

    public LocationUpdateService() {
        super(LocationUpdateService.class.getName());
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startID) {
        super.onStartCommand(intent, flags, startID);
        Log.d("LocationUpdateService","Location received");
        return START_REDELIVER_INTENT;
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d("LocationUpdateService","Intent received");

    }   
}

Manifest file:

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

解决方案

Edit: I recently figured out that you can directly have an IntentService handle location updates

Here is working and tested code that successfully sets up an IntentService as the receiver for the Intent included in the PendingIntent used for location updates, based on code found here.

First, the IntentService:

import android.app.IntentService;
import android.content.Intent;
import android.location.Location;
import android.util.Log;
import com.google.android.gms.location.FusedLocationProviderApi;

public class LocationUpdateService extends IntentService {

    private final String TAG = "LocationUpdateService";
    Location location;

    public LocationUpdateService() {

        super("LocationUpdateService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        if (intent.hasExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED)) {

            location = intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);

            Log.d("locationtesting", "accuracy: " + location.getAccuracy() + " lat: " + location.getLatitude() + " lon: " + location.getLongitude());
        }
    }
}

And here is the Activity code that registers for location updates with a PendingIntent that is sent to the IntentService:

import android.app.PendingIntent;
import android.os.Bundle;
import android.content.Intent;
import android.app.Activity;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

public class MainActivity extends Activity implements
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    PendingIntent mRequestLocationUpdatesPendingIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buildGoogleApiClient();
        mGoogleApiClient.connect();
    }

    @Override
    protected void onPause(){
        super.onPause();
        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mRequestLocationUpdatesPendingIntent);
        }
    }

    protected synchronized void buildGoogleApiClient() {
        Toast.makeText(this,"buildGoogleApiClient",Toast.LENGTH_SHORT).show();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    @Override
    public void onConnected(Bundle bundle) {
        Toast.makeText(this,"onConnected",Toast.LENGTH_SHORT).show();

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(10);
        mLocationRequest.setFastestInterval(10);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        //mLocationRequest.setSmallestDisplacement(0.1F);

        // create the Intent to use WebViewActivity to handle results
        Intent mRequestLocationUpdatesIntent = new Intent(this, LocationUpdateService.class);

        // create a PendingIntent
        mRequestLocationUpdatesPendingIntent = PendingIntent.getService(getApplicationContext(), 0,
                mRequestLocationUpdatesIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        // request location updates
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
                mLocationRequest,
                mRequestLocationUpdatesPendingIntent);
    }

    @Override
    public void onConnectionSuspended(int i) {
        Toast.makeText(this,"onConnectionSuspended",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Toast.makeText(this,"onConnectionFailed",Toast.LENGTH_SHORT).show();
    }
}

Resulting logs:

 D/locationtesting﹕ accuracy: 10.0 lat: 37.779702 lon: -122.3931595
 D/locationtesting﹕ accuracy: 10.0 lat: 37.7797023 lon: -122.3931594
 D/locationtesting﹕ accuracy: 10.0 lat: 37.7797022 lon: -122.3931596
 D/locationtesting﹕ accuracy: 10.0 lat: 37.7797021 lon: -122.3931597
 D/locationtesting﹕ accuracy: 10.0 lat: 37.7797021 lon: -122.3931596
 D/locationtesting﹕ accuracy: 10.0 lat: 37.7797019 lon: -122.3931597
 D/locationtesting﹕ accuracy: 10.0 lat: 37.7797019 lon: -122.3931597

Original Answer, leaving this here as an alternate implementation:

An alternate implementation is just to make the LocationListener locally, and just send an Intent to the IntentService with each onLocationChanged() callback.

I just got an implementation working and tested, I used a LatLng Object in the Intent, since it's small and it's Parcelable. Note that the Location Object is Parcelable as well, and if you need the extra info that it includes, you can just send location in the Intent Extras sent to the IntentService using the same method outlined below. See documentation.

Note that you can use PendingIntent.send() or Context.startService() to send the Intent to the IntentService, for differences see here. For this simple example, I used Context.startService().

I used an Activity for the LocationListener in this case, but you could use any class that implements the LocationListener interface.

First, the slightly modified IntentService:

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
import com.google.android.gms.maps.model.LatLng;

public class LocationUpdateService extends IntentService {

    public LocationUpdateService() {
        super(LocationUpdateService.class.getName());
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startID) {
        super.onStartCommand(intent, flags, startID);
        Log.d("LocationUpdateService","Location received");
        return START_REDELIVER_INTENT;
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d("LocationUpdateService","Intent received");

        LatLng latLng = intent.getParcelableExtra("location");
        Log.d("LocationUpdateService","Intent received: " + latLng.latitude + " " + latLng.longitude);
    }
}

AndroidManifest.xml:

 <service android:enabled="true" android:name=".LocationUpdateService" />

Then, set the Activity as the LocationListener:

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10); //small testing value
    mLocationRequest.setFastestInterval(1); //small testing value
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

And then on each onLocationChanged() callback, send an Intent to the IntentService, and send a LatLng in the intent by using putParcelable() in a Bundle:

@Override
public void onLocationChanged(Location location) {

    mLatitudeText.setText(String.valueOf(location.getLatitude()));
    mLongitudeText.setText(String.valueOf(location.getLongitude()));

    Intent broadcast = new Intent(this,LocationUpdateService.class);
    Bundle b = new Bundle();
    b.putParcelable("location", new LatLng(location.getLatitude(), location.getLongitude()));
    broadcast.putExtras(b);

    getApplicationContext().startService(broadcast);
}

Putting it all together, here's the full Activity code:

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.location.Location;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.model.LatLng;

public class MainActivity extends Activity implements
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    private TextView mLatitudeText;
    private TextView mLongitudeText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLatitudeText=  (TextView) findViewById(R.id.latitudeTextView);
        mLongitudeText = (TextView) findViewById(R.id.longitudeTextView);

        buildGoogleApiClient();

        mGoogleApiClient.connect();

    }

    @Override
    protected void onPause(){
        super.onPause();
        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }
    }

    protected synchronized void buildGoogleApiClient() {
        Toast.makeText(this,"buildGoogleApiClient",Toast.LENGTH_SHORT).show();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    @Override
    public void onConnected(Bundle bundle) {
        Toast.makeText(this,"onConnected",Toast.LENGTH_SHORT).show();
        Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
            mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
        }

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(10);
        mLocationRequest.setFastestInterval(1);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        //mLocationRequest.setSmallestDisplacement(0.1F);

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

    @Override
    public void onConnectionSuspended(int i) {
        Toast.makeText(this,"onConnectionSuspended",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Toast.makeText(this,"onConnectionFailed",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLocationChanged(Location location) {

        mLatitudeText.setText(String.valueOf(location.getLatitude()));
        mLongitudeText.setText(String.valueOf(location.getLongitude()));

        Intent broadcast = new Intent(this,LocationUpdateService.class);
        Bundle b = new Bundle();
        b.putParcelable("location", new LatLng(location.getLatitude(), location.getLongitude()));
        broadcast.putExtras(b);

        getApplicationContext().startService(broadcast);
    }

}

And finally, proof that it works! Here are logs from the IntentService:

 D/LocationUpdateService﹕ Location received
 D/LocationUpdateService﹕ Intent received
 D/LocationUpdateService﹕ Intent received: 37.96396396396396 -122.10874797086674
 D/LocationUpdateService﹕ Location received
 D/LocationUpdateService﹕ Intent received
 D/LocationUpdateService﹕ Intent received: 37.96396396396396 -122.10874797086674

这篇关于发送位置更新IntentService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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