赞
踩
https://developer.huawei.com/consumer/cn/codelabsPortal/serviceTypes/hmscore-cn
https://developer.huawei.com/consumer/cn/hms
https://developer.huawei.com/consumer/cn/codelabsPortal/carddetails/HMSLocationKit
public class RequestLocationPermission {
public static final String TAG = "RequestPermission";
public static void requestLocationPermission(Context context) {
// check location permisiion
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
Log.i(TAG, "sdk < 28 Q");
if (ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
String[] strings =
{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
ActivityCompat.requestPermissions((Activity)context, strings, 1);
}
} else {
if (ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context,
"android.permission.ACCESS_BACKGROUND_LOCATION") != PackageManager.PERMISSION_GRANTED) {
String[] strings = {Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
"android.permission.ACCESS_BACKGROUND_LOCATION"};
ActivityCompat.requestPermissions((Activity)context, strings, 2);
}
}
}
}
/**
* Example of Using requestLocationUpdates and removeLocationUpdates.
* Requests a location update and calls back on the specified Looper thread. This method requires that the requester process exist for continuous callback.
* If you still want to receive the callback after the process is killed, see requestLocationUpdates (LocationRequest request,PendingIntent callbackIntent)
*/
/**使用requestLocationUpdates和removeLocationUpdates的示例。
* 请求位置更新并在指定的循环线程上回调。 此方法要求存在用于持续回调的请求者流程。
* 如果你仍然想在进程被杀死后收到回调,参见requestLocationUpdates (LocationRequest request,PendingIntent callbackIntent)*/
public class RequestLocationUpdatesWithCallbackActivity extends Activity implements OnClickListener {
public static final String TAG = "LocationUpdatesCallback";
LocationCallback mLocationCallback;
LocationRequest mLocationRequest;
private FusedLocationProviderClient mFusedLocationProviderClient;
private SettingsClient mSettingsClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_request_location_updates_callback);
RequestLocationPermission.requestLocationPermission(this);
// todo Button click listeners
findViewById(R.id.location_requestLocationUpdatesWithCallback).setOnClickListener(this);
findViewById(R.id.location_removeLocationUpdatesWithCallback).setOnClickListener(this);
// todo addLogFragment();
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
mSettingsClient = LocationServices.getSettingsClient(this);
mLocationRequest = new LocationRequest();
// todo Sets the interval for location update (unit: Millisecond)
//todo 设置位置更新的间隔(单位:毫秒)
mLocationRequest.setInterval(5000);
// todo Sets the priority
//todo 设置优先级
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (null == mLocationCallback) {
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult != null) {
List<Location> locations = locationResult.getLocations();
if (!locations.isEmpty()) {
for (Location location : locations) {
Log.i("RequestLocationUpdatesWithCallbackActivity","经度:" + location.getLongitude() + "纬度:" + location.getLatitude() + "精确度:" + location.getAccuracy());
Toast.makeText(RequestLocationUpdatesWithCallbackActivity.this,
"onLocationResult location[Longitude,Latitude,Accuracy]:" + location.getLongitude()
+ "," + location.getLatitude() + "," + location.getAccuracy(), Toast.LENGTH_LONG).show();
}
}
}
}
@Override
public void onLocationAvailability(LocationAvailability locationAvailability) {
if (locationAvailability != null) {
boolean flag = locationAvailability.isLocationAvailable();
Log.i(TAG, "onLocationAvailability isLocationAvailable:" + flag);
}
}
};
}
}
/**
* Requests a location update and calls back on the specified Looper thread.
*/
private void requestLocationUpdatesWithCallback() {
try {
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
LocationSettingsRequest locationSettingsRequest = builder.build();
// todo Before requesting location update, invoke checkLocationSettings to check device settings.
//todo 在请求位置更新之前,调用checkLocationSettings检查设备设置。
Task<LocationSettingsResponse> locationSettingsResponseTask = mSettingsClient.checkLocationSettings(locationSettingsRequest);
locationSettingsResponseTask.addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
Log.i(TAG, "check location settings success");
mFusedLocationProviderClient
.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.getMainLooper())
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(RequestLocationUpdatesWithCallbackActivity.this, "requestLocationUpdatesWithCallback onSuccess", Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.e(TAG,
"requestLocationUpdatesWithCallback onFailure:" + e.getMessage());
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.i(TAG, "checkLocationSetting onFailure:" + e.getMessage());
int statusCode = ((ApiException) e).getStatusCode();
switch (statusCode) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
//When the startResolutionForResult is invoked, a dialog box is displayed, asking you to open the corresponding permission.
ResolvableApiException rae = (ResolvableApiException) e;
rae.startResolutionForResult(RequestLocationUpdatesWithCallbackActivity.this, 0);
} catch (IntentSender.SendIntentException sie) {
Log.e(TAG, "PendingIntent unable to execute request.");
}
break;
default:
break;
}
}
});
} catch (Exception e) {
Log.i(TAG, "requestLocationUpdatesWithCallback exception:" + e.getMessage());
}
}
@Override
protected void onDestroy() {
// todo Removed when the location update is no longer required.
//todo 当位置更新不再需要时删除。
removeLocationUpdatesWithCallback();
super.onDestroy();
}
/**
* Removed when the location update is no longer required.当位置更新不需要时删除
*/
private void removeLocationUpdatesWithCallback() {
try {
Task<Void> voidTask = mFusedLocationProviderClient.removeLocationUpdates(mLocationCallback);
voidTask.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(RequestLocationUpdatesWithCallbackActivity.this,"removeLocationUpdatesWithCallback onSuccess", Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(new OnFailureListener() {
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。