android.gradleDep=;implementation 'com.google.android.gms:play-services-location:10.0.1';
private void showSettingDialog() {
com.google.android.gms.location.LocationRequest locationRequest = com.google.android.gms.location.LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);//Setting priotity of Location request to high
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);//5 sec Time interval for location update
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
file or directory '/tmp/build4005083318529358370xxx/main/src/release/java', not found
Compiling with JDK Java compiler API.
/tmp/build4005083318529358370xxx/main/src/main/java/com/yallagym/nativeInterfaceImpl.java:122: error: cannot access zza
com.google.android.gms.location.LocationRequest locationRequest = com.google.android.gms.location.LocationRequest.create();
^
class file for com.google.android.gms.common.internal.safeparcel.zza not found
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
:compileReleaseJavaWithJavac FAILED
:compileReleaseJavaWithJavac (Thread[Daemon worker,5,main]) completed. Took 16.386 secs.
FAILURE: Build failed with an exception.nativeInterface
import com.codename1.system.NativeInterface;
public interface nativeInterface extends NativeInterface {
public void enableGPSAPI();}
import com.codename1.impl.android.CodenameOneActivity;
import com.codename1.impl.android.AndroidNativeUtil;import com.codename1.impl.android.IntentResultListener;import android.provider.Settings;import android.app.Activity;import android.os.Bundle;import android.content.Context;import android.Manifest;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.content.IntentSender;import android.content.pm.PackageManager;import android.location.LocationManager;import android.os.Build;import android.os.Bundle;import android.os.Handler;import android.widget.Toast;import android.support.annotation.NonNull;import android.support.v4.app.ActivityCompat;import android.support.v4.content.ContextCompat;import android.support.v7.app.AppCompatActivity;import android.util.Log;import com.google.android.gms.common.api.GoogleApiClient;import com.google.android.gms.common.api.PendingResult;import com.google.android.gms.common.api.ResultCallback;import com.google.android.gms.common.api.Status;import com.google.android.gms.location.LocationRequest;import com.google.android.gms.location.LocationServices;import com.google.android.gms.location.LocationSettingsRequest;import com.google.android.gms.location.LocationSettingsResult;import com.google.android.gms.location.LocationSettingsStates;import com.google.android.gms.location.LocationSettingsStatusCodes;
public class nativeInterfaceImpl extends CodenameOneActivity {
final CodenameOneActivity cn1activity = (CodenameOneActivity) AndroidNativeUtil.getActivity();
Context context = AndroidNativeUtil.getContext(); Activity activity = AndroidNativeUtil.getActivity(); private static final int REQUEST_CHECK_SETTINGS = 0x1; private static GoogleApiClient mGoogleApiClient; private static final int ACCESS_FINE_LOCATION_INTENT_ID = 3; private static final String BROADCAST_ACTION = "android.location.PROVIDERS_CHANGED";
public void enableGPSAPI() { initGoogleAPIClient(); checkPermissions(); }
/* Initiate Google API Client */ private void initGoogleAPIClient() { //Without Google API Client Auto Location Dialog will not work mGoogleApiClient = new GoogleApiClient.Builder(activity) .addApi(LocationServices.API) .build(); mGoogleApiClient.connect(); }
/* Check Location Permission for Marshmallow Devices */ private void checkPermissions() { if (Build.VERSION.SDK_INT >= 23) { if (ContextCompat.checkSelfPermission(activity, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { requestLocationPermission(); } else { showSettingDialog(); } } else { showSettingDialog(); }
}
/* Show Popup to access User Permission */ private void requestLocationPermission() { if (ActivityCompat.shouldShowRequestPermissionRationale(activity, android.Manifest.permission.ACCESS_FINE_LOCATION)) { ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, ACCESS_FINE_LOCATION_INTENT_ID);
} else { ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, ACCESS_FINE_LOCATION_INTENT_ID); } }//// /* Show Location Access Dialog */
private void showSettingDialog() { com.google.android.gms.location.LocationRequest locationRequest = com.google.android.gms.location.LocationRequest.create(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);//Setting priotity of Location request to high locationRequest.setInterval(30 * 1000); locationRequest.setFastestInterval(5 * 1000);//5 sec Time interval for location update LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() .addLocationRequest(locationRequest); builder.setAlwaysShow(true); //this is the key ingredient to show dialog always when GPS is off
PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build()); result.setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(LocationSettingsResult result) { final Status status = result.getStatus(); final LocationSettingsStates state = result.getLocationSettingsStates(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: // All location settings are satisfied. The client can initialize location // requests here. updateGPSStatus("GPS is Enabled in your device"); break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: // Location settings are not satisfied. But could be fixed by showing the user // a dialog. try { // Show the dialog by calling startResolutionForResult(), // and check the result in onActivityResult(). status.startResolutionForResult(activity, REQUEST_CHECK_SETTINGS); } catch (IntentSender.SendIntentException e) { e.printStackTrace(); // Ignore the error. } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: // Location settings are not satisfied. However, we have no way to fix the // settings so we won't show the dialog. break; } } }); }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { // Check for the integer request code originally supplied to startResolutionForResult(). case REQUEST_CHECK_SETTINGS: switch (resultCode) { case -1: Log.e("Settings", "Result OK"); updateGPSStatus("GPS is Enabled in your device"); //startLocationUpdates(); break; case 0: Log.e("Settings", "Result Cancel"); updateGPSStatus("GPS is Disabled in your device"); break; } break; } } private void updateGPSStatus(String status) { //... } @Override protected void onResume() { super.onResume(); registerReceiver(gpsLocationReceiver, new IntentFilter(BROADCAST_ACTION));//Register broadcast receiver to check the status of GPS } @Override protected void onDestroy() { super.onDestroy(); //Unregister receiver on destroy if (gpsLocationReceiver != null) { unregisterReceiver(gpsLocationReceiver); } } //Run on UI private Runnable sendUpdatesToUI = new Runnable() { public void run() { showSettingDialog(); } };
/* Broadcast receiver to check status of GPS */ private BroadcastReceiver gpsLocationReceiver = new BroadcastReceiver() {
@Override public void onReceive(Context context, Intent intent) {
//If Action is Location if (intent.getAction().matches(BROADCAST_ACTION)) { LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); //Check if GPS is turned ON or OFF if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { Log.e("About GPS", "GPS is Enabled in your device"); updateGPSStatus("GPS is Enabled in your device"); } else { //If GPS turned OFF show Location Dialog new Handler().postDelayed(sendUpdatesToUI, 10); // showSettingDialog(); updateGPSStatus("GPS is Disabled in your device"); Log.e("About GPS", "GPS is Disabled in your device"); }
} } };
/* On Request permission method to check the permisison is granted or not for Marshmallow+ Devices */ @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case ACCESS_FINE_LOCATION_INTENT_ID: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//If permission granted show location dialog if APIClient is not null if (mGoogleApiClient == null) { initGoogleAPIClient(); showSettingDialog(); } else { showSettingDialog(); }
} else { Toast.makeText(activity, "Location Permission denied.", Toast.LENGTH_SHORT).show(); // permission denied, boo! Disable the // functionality that depends on this permission. } return; } } }
public boolean isSupported() { return true; }
}
android.playService.location=true
AndroidNativeUtil.addLifecycleListener(new LifecycleListener() {
public void onCreate(Bundle savedInstanceState) { .... }
public void onResume() { .... }
public void onPause() { .... }
public void onDestroy() { .... }
public void onSaveInstanceState(Bundle b) { .... }
public void onLowMemory() { .... }
}