【问题标题】:How to update the android app when the Location toggle is enabled in Settings?在设置中启用位置切换时如何更新 android 应用程序?
【发布时间】:2015-09-24 10:53:22
【问题描述】:

我的应用通过导航到位置设置活动来提示用户打开位置服务。 现在如何在用户切换设置活动中的位置选项时签入活动代码。

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();
    }

}

【问题讨论】:

    标签: android android-activity gps android-location


    【解决方案1】:

    我想你正在寻找这个,希望 - 它会解决你的问题

    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
        }
    
    
    
    }
    

    【讨论】:

    • 当我打开 GPS 时,覆盖的方法 onLocationChanged onProviderDisabled onStatusChanged 没有被调用。
    • 我正在使用 startActivityForResult。但是有没有更好的方法?
    • 您需要在回到您的活动后再次检查以确保状态,实际上是用户已选择作为 ON 位置选项,如果选项处于活动状态,那么您可以使用 onProviderDisabled onStatusChanged 覆盖方法进行跟踪跨度>
    • 同意。但是如何处理这种情况-用户从通知栏打开gps?
    • onProviderDisabled(String providerName) 和 onProviderEnabled(String providerName) 在您通过通知或设置激活或停用位置时将被调用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 2022-07-17
    相关资源
    最近更新 更多