我的应用程序正在通过导航到位置设置活动来促使用户打开位置服务。 现在,当用户切换设置活动中的“位置”选项时,如何检查活动代码。
public void checkGPS() {
LocationManager lm = (LocationManager) GlobalHome.this.getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
boolean network_enabled = false;
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
if (!gps_enabled && !network_enabled) {
// notify user
AlertDialog.Builder dialog = new AlertDialog.Builder(GlobalHome.this);
dialog.setMessage(MessagesString.LOCATION_DIALOG_MESSAGE);
dialog.setPositiveButton(MessagesString.LOCATION_DIALOG_POSTIVE_TEXT, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
GlobalHome.this.startActivity(myIntent);
//get gps
}
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
}
});
dialog.show();
}
}
答案 0 :(得分:0)
我认为你正在寻找这个,希望 - 它会解决你的问题
public class MainActivity extends Activity implements LocationListener {
LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
isLocationServiceActive();
}
private void isLocationServiceActive(){
locationManager=(LocationManager)getSystemService(LOCATION_SERVICE);
if(locationManager!=null){
LocationProvider gpsLocationProvider=locationManager.getProvider(LocationManager.GPS_PROVIDER);
LocationProvider networkLocationProvider=locationManager.getProvider(LocationManager.NETWORK_PROVIDER);
if(gpsLocationProvider==null && networkLocationProvider==null){
//device does'nt support location services
showDeviceNotSupportLocationsDialog();
}
else if(networkLocationProvider!=null && locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000L, 100.0F, this);
}
else if(gpsLocationProvider!=null && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000L, 100.0F, this);
}
else{
showNoGpsDialog();
}
}
}
public void showNoGpsDialog()
{
AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
localBuilder.setMessage(this.getResources().getString(R.string.gps_network_not_enabled));
localBuilder.setPositiveButton(this.getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
{
Intent localIntent = new Intent("android.settings.LOCATION_SOURCE_SETTINGS");
MainActivity.this.startActivity(localIntent);
}
});
localBuilder.setCancelable(false);
localBuilder.show();
}
public void showDeviceNotSupportLocationsDialog()
{
AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
localBuilder.setMessage(this.getResources().getString(R.string.device_not_support_locations));
localBuilder.setPositiveButton(this.getResources().getString(R.string.exit), new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
{
MainActivity.this.finish();
}
});
localBuilder.setCancelable(false);
localBuilder.show();
}
@Override
public void onLocationChanged(Location location) {
//location changed
}
@Override
public void onProviderDisabled(String s) {
//your code here
}
@Override
public void onProviderEnabled(String s) {
//your code here
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
//your code here
}
}