Created
August 27, 2021 04:04
-
-
Save lokeshsuryan/98d707a7d7ced4ca5f08dddb5c91313b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hms.locationservice; | |
import ohos.aafwk.ability.Ability; | |
import ohos.aafwk.content.Intent; | |
import ohos.agp.components.Button; | |
import ohos.agp.components.Text; | |
import ohos.bundle.IBundleManager; | |
import ohos.hiviewdfx.HiLog; | |
import ohos.hiviewdfx.HiLogLabel; | |
import ohos.location.*; | |
import java.io.IOException; | |
public class MainAbility extends Ability { | |
Locator locator; | |
MyLocatorCallback locatorCallback; | |
HiLogLabel LABEL_LOG; | |
RequestParam requestParam; | |
public static final int MY_PERMISSIONS_REQUEST_LOCATION=100; | |
Button getCurrentAddressBtn,stopAccessLoation,getContinuousUserLocation; | |
Text userAddress; | |
@Override | |
public void onStart(Intent intent) { | |
super.onStart(intent); | |
super.setUIContent(ResourceTable.Layout_ability_main); | |
LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x00201, "Main_ability"); | |
userAddress=(Text)findComponentById(ResourceTable.Id_userAddress); | |
getCurrentAddressBtn=(Button)findComponentById(ResourceTable.Id_getCurrentAddressBtn); | |
getContinuousUserLocation=(Button)findComponentById(ResourceTable.Id_getContinuousUserLocation); | |
stopAccessLoation=(Button)findComponentById(ResourceTable.Id_stopAccessLoation); | |
locator = new Locator(MainAbility.this); | |
requestParam = new RequestParam(RequestParam.PRIORITY_ACCURACY, 10, 0); | |
locatorCallback = new MyLocatorCallback(); | |
requestPermission(); | |
getCurrentAddressBtn.setClickedListener(component->{ | |
locator.requestOnce(requestParam, locatorCallback); | |
}); | |
getContinuousUserLocation.setClickedListener(component->{ | |
locator.stopLocating(locatorCallback); | |
locator.startLocating(requestParam, locatorCallback); | |
}); | |
stopAccessLoation.setClickedListener(component->{ | |
locator.stopLocating(locatorCallback); | |
}); | |
} | |
private void requestPermission() { | |
if (verifySelfPermission("ohos.permission.LOCATION") != IBundleManager.PERMISSION_GRANTED||verifySelfPermission("ohos.permission.LOCATION_IN_BACKGROUND") != IBundleManager.PERMISSION_GRANTED) { | |
// The application has not been granted the permission. | |
if (canRequestPermission("ohos.permission.LOCATION")||canRequestPermission("ohos.permission.LOCATION_IN_BACKGROUND")) { | |
// Check whether permission authorization can be implemented via a dialog box (at initial request or when the user has not chosen the option of "don't ask again" after rejecting a previous request). | |
requestPermissionsFromUser( | |
new String[] { "ohos.permission.LOCATION","ohos.permission.LOCATION_IN_BACKGROUND" } , MY_PERMISSIONS_REQUEST_LOCATION); | |
} else { | |
// Display the reason why the application requests the permission and prompt the user to grant the permission. | |
} | |
} else { | |
// The permission has been granted. | |
} | |
} | |
public class MyLocatorCallback implements LocatorCallback { | |
@Override | |
public void onLocationReport(Location location) { | |
GeoConvert geoConvert = new GeoConvert(); | |
HiLog.info(LABEL_LOG, "geLatitude:" +location.getLatitude()+"\n geLatitude"+location.getLongitude()); | |
try { | |
userAddress.setText("User Current address: "+geoConvert.getAddressFromLocation(location.getLatitude(), location.getLongitude(), 1)); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public void onStatusChanged(int type) { | |
if (type == 3) { | |
HiLog.info(LABEL_LOG, "geLatitude:" +"Stop Location accessing"); | |
}else | |
HiLog.info(LABEL_LOG, "geLatitude:" +"onChange status"); | |
} | |
@Override | |
public void onErrorReport(int type) { | |
HiLog.info(LABEL_LOG, "geLatitude:" +"error"); | |
} | |
} | |
@Override | |
protected void onBackground() { | |
super.onBackground(); | |
locator.stopLocating(locatorCallback); | |
} | |
@Override | |
public void onRequestPermissionsFromUserResult (int requestCode, String[] permissions, int[] grantResults) { | |
switch (requestCode) { | |
case MY_PERMISSIONS_REQUEST_LOCATION: { | |
// Match requestCode of requestPermissions. | |
if (grantResults.length > 0 | |
&& grantResults[0] == IBundleManager.PERMISSION_GRANTED) { | |
locator.startLocating(requestParam, locatorCallback); | |
// The permission is granted. | |
//Note: During permission check, an interface may be considered to have no required permissions due to time difference. Therefore, it is necessary to capture and process the exception thrown by such an interface. | |
} else { | |
// The permission request is rejected. | |
} | |
return; | |
} | |
default: | |
throw new IllegalStateException("Unexpected value: " + requestCode); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment