使用intentservice的FusedLocationProvider进行后台位置更新:当未决的intent添加了捆绑包时,位置更新不起作用 [英] FusedLocationProvider using intentservice for background location update: location update does not work when pendingintent has a bundle added

查看:97
本文介绍了使用intentservice的FusedLocationProvider进行后台位置更新:当未决的intent添加了捆绑包时,位置更新不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长一段时间以来,我一直在寻找答案,似乎没人能找到答案.

I have been looking for an answer for this question for a long time and no one seemed to have an answer.

我正在尝试使用Google最新的FusedLocationProviderAPI订阅位置更新.我已经在线上关注了该教程,并且可以立即获取我的位置,然后使用由requestLocationUpdates触发的intentservice来工作.

I am trying to subscribe to location updates, using Google's latest FusedLocationProviderAPI. I have followed tutorial online and it works by getting my location every now and then using intentservice triggered by requestLocationUpdates.

继续,我尝试向我的intentservice添加传递一个对象"User"的自定义类.这个自定义类将允许我将位置上传到我的服务器以进行后台位置更新.

Moving on, I try to add pass a custom class of object "User" to my intentservice. This custom class would allow me to upload the location to my server for background location update.

我添加自定义对象的方式是将包添加到该intent,然后将其添加到触发IntentService的pendingPending中.不幸的是,intentservice将立即被破坏,并在添加捆绑软件后立即返回null.

The way I add the custom object is by adding bundle to the intent, which is then added to the pendingIntent that triggers the IntentService. Unfortunately, the intentservice would immediately become broken and return null as soon as the bundle is added.

这是我在MainActivity中触发IntentService的代码:

Here is my code, in MainActivity where I trigger the IntentService:

@Override
public void onConnected(Bundle arg0) {

    // This is for creating the intent that is used for handler class
    Intent intent = new Intent(MainActivity.this, MyLocationHandler.class);
    Bundle b = new Bundle();
    b.putParcelable("user", user);
    //intent.putExtras(b);
    intent.putExtra("bundle",b);

    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

    // Then the off screen periodic update
    PendingResult pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
          mLocationRequest, pendingIntent);

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

}

从上面可以看到,我将在意图中添加一个包"b",将其添加到未决的Intent中并触发LocationHandler类:

As you can see from the above, I would add a bundle 'b' to the intent, which is added to the pendingIntent and triggers the LocationHandler class:

public class MyLocationHandler extends IntentService {

User user;
private String TAG = this.getClass().getSimpleName();

public MyLocationHandler() {
    super("MyLocationHandler");
}


@Override
protected void onHandleIntent(Intent intent) {

     Bundle bundle = intent.getBundleExtra("bundle");
    //Bundle bundle = intent.getExtras();
    if (bundle == null) {
        Log.e(TAG, "bundle is null");
    } else {
        bundle.setClassLoader(User.class.getClassLoader());
    }
    user = (User) bundle.getParcelable("user");

    if (user == null) {
        Log.e(TAG, "user is null");
    }

    if (LocationResult.hasResult(intent)) {
        LocationResult locationResult = LocationResult.extractResult(intent);
        Location location = locationResult.getLastLocation();
        Log.i(TAG, Double.toString(location.getLatitude()) + ", " + Double.toString(location.getLongitude()));

        if (user != null) {
            user.updateLocation(location); // This is a method in the custom class that sends location to the server
        }
    } else
        Log.e(TAG, "Null object returned for location API");
}

每当我尝试添加捆绑包时,LocationHandler类中的location都将返回null,但是如果我删除捆绑包,则会恢复位置更新.

Whenever I try to add a bundle, location would return null in LocationHandler class, but if I remove the bundle, the location updates are resumed.

附上我的清单,以防万一:

Attached my manifest just in case:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.example.projecttesting.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.example.projecttesting.permission.C2D_MESSAGE" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<application
    android:allowBackup="true"
    tools:replace="icon, label"
    android:icon="@drawable/ic_launcher"
    android:label="SamePage"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar" >

    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSyCDY8ulp1VGKwGdaRU19G4sfuXsymZGgoY" />


    <activity
        android:name=".LoginActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".EventLocationInput"
        android:windowSoftInputMode="adjustPan" />
    <activity android:name=".EventPeopleInput" />
    <activity android:name=".EventCreation"/>
    <service android:enabled="true" android:name=".MyLocationHandler"/>

    <activity android:name="com.facebook.FacebookActivity"
        android:configChanges=
            "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"
        android:label="@string/app_name" />
    <receiver
        android:name=".GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.example.projecttesting" />
        </intent-filter>
    </receiver>
    <service android:name=".GcmMessageHandler" />
    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
</application>

</manifest>

到目前为止,我只在这里看到一个类似的问题: Android FusedLocationProviderApi:传入意图没有LocationResult或LocationAvailability

So far I only see a similar question raised here otherwise: Android FusedLocationProviderApi: Incoming intent has no LocationResult or LocationAvailability

这让我发疯了.希望有人对此有所帮助.

This has been driving me crazy. Hope someone can shred some light on this.

谢谢.

推荐答案

根据建议,我将在此处发布答案.它并不能完全解决我遇到的问题,但是我设法通过使用SharedPreferenceManager将我需要的内容存储在另一个活动中并在Handler类中检索了它,从而使其能够正常工作.但是,这不是最优雅的解决方案,而是有效的解决方案.如果您需要的尺寸很小并且不经常更改,则这可能是一种解决方法.

As suggested I will post my answer here. It is not exactly solving the problem that I had, but I managed to make it work by using SharedPreferenceManager to store what I needed in another activity and retrieved it in the Handler class. Not the most elegant solution but effectiveness nonetheless. This may be a workaround if what you need is small in size and does not change often.

这篇关于使用intentservice的FusedLocationProvider进行后台位置更新:当未决的intent添加了捆绑包时,位置更新不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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