【发布时间】:2015-12-08 13:23:03
【问题描述】:
我一直在寻找这个问题的答案,但似乎没有人有答案。
我正在尝试使用 Google 最新的 FusedLocationProviderAPI 订阅位置更新。我已经在线学习了教程,它通过不时获取我的位置来工作,然后使用由 requestLocationUpdates 触发的 intentservice。
继续,我尝试将对象“用户”的自定义类添加到我的意图服务。这个自定义类将允许我将位置上传到我的服务器以进行后台位置更新。
我添加自定义对象的方式是将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
这让我发疯了。希望有人能对此有所了解。
谢谢。
【问题讨论】:
-
这些
Intents 中的额外内容似乎存在问题。见code.google.com/p/android/issues/detail?id=81812 -
尝试添加一个只是
String的额外对象,而不是您的User对象,看看是否有效。如果是这样,您只需将您的User对象序列化为String并添加Intent。然后,当您获得Intent时,只需将String额外提取出来并将其反序列化为User对象。 -
我尝试传递另一个字符串并删除了用户,但没有成功。我最终通过简单地使用 sharedpreference manager 将我需要使用的 String 传递给 User 方法来解决这个问题。
-
好吧,它看起来像是一个记录在案的已知问题。有时你只需要另一种方法。很高兴你能弄清楚一些事情。请回答您自己的问题并接受您的回答。这将从未回答的未解决问题列表中删除该问题,并可能对其他人有所帮助。
标签: android location bundle android-pendingintent fusedlocationproviderapi