我一直在寻找这个问题的答案很长一段时间,似乎没有人有答案。
我正在尝试使用Google最新的FusedLocationProviderAPI订阅位置更新。我已经在线学习了教程,它的工作方式是不时地获取我的位置,然后使用requestLocationUpdates触发的intentservice。
继续,我尝试将自定义类对象“User”传递给我的intentservice。此自定义类允许我将位置上载到我的服务器以进行后台位置更新。
我添加自定义对象的方法是向intent添加bundle,然后将其添加到触发IntentService的pendingIntent中。不幸的是,一旦添加了bundle,intentservice就会立即被破坏并返回null。
这是我的代码,在MainActivity中我触发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);
}
从上面可以看出,我会在intent中添加一个包'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");
}
每当我尝试添加一个bundle时,location会在LocationHandler类中返回null,但是如果我删除了bundle,则会恢复位置更新。
附上我的清单,以防万一:
<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
这让我发疯了。希望有人能够对此有所了解。
感谢。
答案 0 :(得分:0)
根据建议,我会在这里发布我的答案。它并没有完全解决我遇到的问题,但我设法通过使用SharedPreferenceManager来存储我需要的另一个活动并在Handler类中检索它。虽然不是最优雅的解决方案,但有效性。如果你需要的东西很小而且不经常改变,这可能是一种解决方法。