San*_*t A 7 android location android-gps
我的应用程序使用两个独立的Process.我拥有所有的Services一个Process和所有其他组件在另一个Process.当我试图从获得lastKnownLocation LocationManger的时候Intent收到与行动public int onStartCommand(Intent intent, int flags, int startId)从Activity,我得到了Location空,当我看到的LocationManager物体在其显示调试器Context has changed, operation is not possible.感谢帮助
.以下是代码片段 -
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String action = intent.getAction();
if (action != null) {
if (action.equalsIgnoreCase(Synchronize.ACTION_UPDATE_LOCATION)) {
Location location = getLastKnownLocation();
if (location != null) {
new Synchronize(getApplicationContext()).
updateLocation(prefs.getInt(Utils.PROPERTY_USER_ID, 0),
location.getLatitude(), location.getLongitude(),
Utils.formatDateMilliSecondToUTC(location.getTime()));
}
} else if (action.equalsIgnoreCase(Synchronize.ACTION_SYNC_CONTACTS)) {
new Synchronize(getApplicationContext()).syncContacts();
} else if (action.equalsIgnoreCase(Synchronize.ACTION_SYNC_EVENTS)) {
new Synchronize(getApplicationContext()).syncEvents();
} else if (action.equalsIgnoreCase(Synchronize.ACTION_SYNC_OFFERS)) {
new Synchronize(getApplicationContext()).syncOffers();
} else if (action.equalsIgnoreCase(Synchronize.ACTION_SYNC_DISCOVERS)) {
new Synchronize(getApplicationContext()).syncDiscoverEvents();
}
}
return START_STICKY;
}
private Location getLastKnownLocation() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
} else {
return locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
810 次 |
| 最近记录: |