美文网首页
android 进行一些系统设置(定位、网络、WIFI)

android 进行一些系统设置(定位、网络、WIFI)

作者: Nj_第一批老去的90后 | 来源:发表于2017-05-22 14:25 被阅读129次

    判断定位权限是否开启

        //注意:请使用自己程序的Application
        public static boolean isGPSEnabled() {
            if (BaiduRiderApplication.instance() == null) {
                return false;
            }
            LocationManager manager = (LocationManager) BaiduRiderApplication.instance().getSystemService(Context.LOCATION_SERVICE);
            if (manager == null) {
                return false;
            }
            return manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        }
     
    

    设置定位

    public static void setupGPS(Context context) {
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            try {
                context.startActivity(intent);
            } catch (ActivityNotFoundException ex) {
                intent.setAction(Settings.ACTION_SETTINGS);
                try {
                    context.startActivity(intent);
                } catch (Exception e) {
                }
            }
        }
    

    设置网络

     public static void setupNetWork(Context context) {
            try {
                final Intent intent;
                if (VERSION.SDK_INT > 10) {
                    intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
                } else {
                    intent = new Intent();
                    ComponentName component =
                            new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
                    intent.setComponent(component);
                    intent.setAction(Intent.ACTION_VIEW);
                }
                context.startActivity(intent);
            } catch (Throwable t) {
                if (null != t) {
                    t.printStackTrace();
                }
            }
        }
    
    

    设置WIFI

    
        public static void setupWifi(Context context) {
            final Intent intent;
            try {
                intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
                context.startActivity(intent);
            } catch (Throwable t) {
                if (null != t) {
                    t.printStackTrace();
                    setupSetting(context);
                }
            }
        }
    
    
    

    设置

     public static void setupSetting(Context context) {
            try {
                Intent intent = new Intent(Settings.ACTION_SETTINGS);
                context.startActivity(intent);
            } catch (Throwable t) {
                if (null != t) {
                    t.printStackTrace();
                    setupNetWork(context);
                }
            }
        }
    

    退出APP

     public static void exitAPP(Context context) {
            if (context instanceof Activity) {
                ((Activity) context).finish();
            } else if (context instanceof Service) {
                ((Service) context).stopSelf();
            }
        }
    

    震动设置

    //设置震动的时间
    public static void vibrate(Context context, long duration) {
            if (context == null || duration <= 0) {
                return;
            }
            Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.vibrate(duration);
        }
    
    //取消震动
     vibrator.cancel();//取消震动,立即停止震动
    
    //权限
    <uses-permission Android:name="android.permission.VIBRATE"/>
    

    相关文章

      网友评论

          本文标题:android 进行一些系统设置(定位、网络、WIFI)

          本文链接:https://www.haomeiwen.com/subject/angbxxtx.html