我想从location
获取background
并将其提交至server
,因此最好的选择是Job scheduler
或Service
。为什么?我还想了解job scheduler
保存的电池,同时我们会不断拨打web api
电话。
答案 0 :(得分:0)
这是我的实施,我不使用Job Scheduler
或Service
,因为没有必要。我使用Application
课程,这样您就可以在所有应用中获得用户的位置。
首先,您需要创建一个LocationHelper
类,它将为您完成所有工作:
public class LocationHelper implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {
private static final int REQUEST_LOCATION_PERMISSION = 0;
private static final int REQUEST_RESOLVE_ERROR = 1;
private static GoogleApiClient mGoogleApiClient;
private Fragment mFragment;
private final Activity mActivity;
private final Callback mCallback;
private Location mLastLocation;
private boolean mResolvingError;
private LocationRequest mLocationRequest;
private boolean mRegisterLocationUpdates;
public interface Callback {
void onLastLocation(Location userLocation);
}
public LocationHelper(Fragment fragment, Callback callback) {
this(fragment.getActivity(), callback);
mFragment = fragment;
}
public LocationHelper(Activity activity, Callback callback) {
mActivity = activity;
mCallback = callback;
mLocationRequest = new LocationRequest();
mGoogleApiClient = new GoogleApiClient.Builder(mActivity)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
obtainLastLocation();
}
private void obtainLastLocation() {
// Verifies if user give us permission to obtain its suggestionLocationV2.
if (ActivityCompat.checkSelfPermission(mActivity,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(mActivity,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity,
Manifest.permission.ACCESS_COARSE_LOCATION)) {
// Show an explanation to the user why we need its suggestionLocationV2.
requestPermissionRationale();
} else {
requestPermission();
}
// We don't have user permission to get its geo suggestionLocationV2, abort mission.
return;
}
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
return;
}
Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (lastLocation != null) {
onLocationChanged(lastLocation);
} else {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
mRegisterLocationUpdates = true;
}
}
@Override
public void onLocationChanged(Location location) {
if (location == null) return;
removeLocationUpdatesIfNeed();
mLastLocation = location;
DirectoryApp.getInstance().setLastLocation(mLastLocation);
if (mCallback != null) {
mCallback.onLastLocation(mLastLocation);
}
}
private void removeLocationUpdatesIfNeed() {
if (mRegisterLocationUpdates && mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mRegisterLocationUpdates = false;
}
}
private void requestPermission() {
// Lets ask suggestionLocationV2 permission to user.
if (mFragment != null) {
mFragment.requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_LOCATION_PERMISSION);
} else {
ActivityCompat.requestPermissions(mActivity,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_LOCATION_PERMISSION);
}
}
private void requestPermissionRationale() {
new AlertDialog.Builder(mActivity)
.setMessage("We need the suggestionLocationV2 to provide you best results.")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
requestPermission();
}
})
.show();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult result) {
// If not already attempting to resolve an error.
if (!mResolvingError) {
if (result.hasResolution()) {
try {
mResolvingError = true;
result.startResolutionForResult(mActivity, REQUEST_RESOLVE_ERROR);
} catch (IntentSender.SendIntentException e) {
// There was an error with the resolution intent. Try again.
mGoogleApiClient.connect();
}
} else {
GooglePlayServicesUtil.showErrorDialogFragment(result.getErrorCode(), mActivity,
null, REQUEST_RESOLVE_ERROR, null);
mResolvingError = true;
}
}
}
// The follow methods should be called in Activity or Fragment.
public void onStart() {
mGoogleApiClient.connect();
}
public void onStop() {
removeLocationUpdatesIfNeed();
mGoogleApiClient.disconnect();
}
public void onRequestPermissionResult(int requestCode, String[] permissions,
int[] grantResults) {
if (requestCode == REQUEST_LOCATION_PERMISSION
&& grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted. Uhull lets get its suggestionLocationV2 now.
obtainLastLocation();
}
}
}
请注意,当位置发生更改时,我们调用Application类来设置新位置,因此在Application类中必须创建方法:
public class Application extends MultiDexApplication {
private static App instance;
private Location mLastLocation;
@Override
public void onCreate() {
super.onCreate();
instance = this;
}
public void setLastLocation(Location lastLocation) {
mLastLocation = lastLocation;
}
public Location getLastLocation() {
return mLastLocation;
}
最后,当您必须在Fragment
或Activity
上使用该位置时,只需使用正确的方法启动和停止该位置。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationHelper = new LocationHelper(this, this);
}
@Override
public void onStart() {
super.onStart();
locationHelper.onStart();
}
@Override
public void onStop() {
super.onStop();
locationHelper.onStop();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
locationHelper.onRequestPermissionResult(requestCode, permissions, grantResults);
}
答案 1 :(得分:0)
如果您将“最佳选项”定义为“电池效率最高的方式”:尽量减少激活连接互联网以读取或传输数据的呼叫。
而是实现广播接收器,告诉您的应用已经有互联网流量。当您的应用收到广播通知时,它可以添加自己的互联网流量。通过这种方式,您的应用可以避免连接到互联网的电池开销,这非常耗电。
有关详情,请参阅https://developer.android.com/training/efficient-downloads/efficient-network-access.html
处的“捆绑转移”