2

我一直在寻找这个问题的答案很长时间,似乎没有人有答案。

我正在尝试使用 Google 最新的 FusedLocationProviderAPI 订阅位置更新。我已经在线学习了教程,它通过不时获取我的位置然后使用由 requestLocationUpdates 触发的意图服务来工作。

继续前进,我尝试将对象“用户”的自定义类添加到我的意图服务。这个自定义类将允许我将位置上传到我的服务器以进行后台位置更新。

我添加自定义对象的方式是将bundle添加到intent中,然后将其添加到触发IntentService的pendingIntent中。不幸的是,一旦添加了 bundle,intentservice 就会立即损坏并返回 null。

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

@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',它被添加到pendingIntent并触发LocationHandler类:

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 类中返回 null,但如果我删除包,位置更新将恢复。

附上我的清单以防万一:

<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: Incoming intent has no LocationResult or LocationAvailability

这一直让我发疯。希望有人可以对此有所了解。

谢谢。

4

1 回答 1

0

正如建议的那样,我将在这里发布我的答案。它并没有完全解决我遇到的问题,但我设法通过使用 SharedPreferenceManager 将我需要的内容存储在另一个活动中并在 Handler 类中检索它来使其工作。不是最优雅的解决方案,但仍然有效。如果您需要的东西很小并且不经常更改,这可能是一种解决方法。

于 2015-12-20T15:31:52.867 回答