I tried to use the GPS check method below by calling MainActivity, but I couldn't use it It's bouncing off. I need your advice.
===GPS Verification Class===
public class CheckGPS extends Activity{
public CheckGPS() {
}
public boolean chkGpsService() {
//Confirmed that GPS is turned on.
String gpsEnabled = android.provider.Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (!(gpsEnabled.matches(".*gps.*") && gpsEnabled.matches(".*network.*"))) {
If //gps is not enabled,
new AlertDialog.Builder(this).setTitle ("GPS Settings").setMessage ("GPS is turned off). \nDo you want to enable GPS?").setPositiveButton("GPS On", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
//GPS setup screen displayed
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}).setNegativeButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).create().show();
}
return false;
}
}
===MainActivity==== strong text protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
//Part of the problem. I can't use it.
CheckGPS gps = new CheckGPS();
gps.chkGpsService();
Toast.makeText(getApplicationContext(), "Reading information.", Toast.LENGTH_LONG).show();
intent = new Intent (this, CurrentLocatinActivity.class); // Execute the int to display the current location screen.
startActivity(intent);
}
Even if you inherited activity, calling like that is a bit...
From MainActivity
gps.chkGpsService(this)
Call like this.
public class CheckGPS {
public boolean chkGpsService(Activity activity) {
//Confirmed that GPS is turned on.
String gpsEnabled = android.provider.Settings.Secure.getString(activity.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (!(gpsEnabled.matches(".*gps.*") && gpsEnabled.matches(".*network.*"))) {
If //gps is not enabled,
new AlertDialog.Builder(activity).setTitle ("GPS Settings").setMessage ("GPS is turned off). \nDo you want to enable GPS?").setPositiveButton("GPS On", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
//GPS setup screen displayed
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
activity.startActivity(intent);
}
}).setNegativeButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).create().show();
return false;
}
return true;
}
}
It's probably a little less edited in this way.
But wouldn't it be cleaner to create a function that checks if gps is turned on by creating a class like LocationUtil?
© 2024 OneMinuteCode. All rights reserved.