【问题标题】:How To Enable Mobile Data Through Intent on App StartUp for MapView如何在 MapView 的应用启动时通过 Intent 启用移动数据
【发布时间】:2015-12-18 12:17:44
【问题描述】:

我是 Android 新手,我想在我的应用中查找我的位置,然后再查看我的位置我想查看我的 Gps 和 Internet 连接

ie:如果 Gps 未通过调用 Intent 启用,我们是呼叫设置,我们启用它同样,如果 Internet 移动数据未启用,我们如何通过 Intent 启用移动数据

例如如果我们想通过 Intent 打开 Gps 我们调用Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);

lisewise 用于启用移动数据 我们称之为什么意图?

这是我的代码:

    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

        private GoogleMap mMap;
        LocationManager locationManager;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);

            checkGPSStatus();
                SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map);
                mapFragment.getMapAsync(this);

        }



     public void onSearch(View view)
        {
            EditText location_tf = (EditText)findViewById(R.id.search);
            String location = location_tf.getText().toString();
            List<Address> addressList = null;
            if(location != null || !location.equals(""))
            {
                Geocoder geocoder = new Geocoder(this);
                try {
                    addressList = geocoder.getFromLocationName(location , 1);


                } catch (IOException e) {
                    e.printStackTrace();
                }

                Address address = addressList.get(0);
                LatLng latLng = new LatLng(address.getLatitude() , address.getLongitude());
                mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
                mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));

            }
        }

@Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
      //  LatLng sydney = new LatLng(0, 0);
        mMap.addMarker(new MarkerOptions().position(new LatLng(0,0)).title("Marker"));

    }
    private void checkGPSStatus() {
        LocationManager locationManager = null;
        boolean gps_enabled = false;
        boolean network_enabled = false;
        if ( locationManager == null) {
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        }
        try {
            gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception ex){}
        try {
            network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ex){}
        if ( !gps_enabled && !network_enabled ){
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setMessage("GPS not enabled");
            dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //this will navigate user to the device location settings screen
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(intent);
                }
            });
            AlertDialog alert = dialog.create();
            alert.show();
        }
    }

【问题讨论】:

标签: android gps android-mapview


【解决方案1】:
With The Following permissions:

        <!-- Internet Permissions -->
        <uses-permission android:name="android.permission.INTERNET" />

        <!-- Network State Permissions -->
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Try This

    public boolean isConnectingToInternet(){
            ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
              if (connectivity != null) 
              {
                  NetworkInfo[] info = connectivity.getAllNetworkInfo();
                  if (info != null) 
                      for (int i = 0; i < info.length; i++) 
                          if (info[i].getState() == NetworkInfo.State.CONNECTED)
                          {
                              return true;
                          }

              }
              return false;
        }

Use This as follow:

    if (isConnectingToInternet()) {
                        // Internet Connection is Present
                        // make HTTP requests
                        showAlertDialog(AndroidDetectInternetConnectionActivity.this, "Internet Connection",
                                "You have internet connection", true);
                    } else {
                        // Internet connection is not present
                        // Ask user to connect to Internet
                         AlertDialog alertDialog = new AlertDialog.Builder(context).create();

                        // Setting Dialog Title
                        alertDialog.setTitle("Internet Connection");

                       // Setting Dialog Message
                       alertDialog.setMessage("You don't have internet connection.");

                      // Setting alert dialog icon
                      alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

                      // Setting OK Button
                      alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int which) {
                      Intent intent = new Intent(Intent.ACTION_MAIN);
                      intent.setClassName("com.android.phone",                       "com.android.phone.NetworkSetting");
                      startActivity(intent);
                      }
                     });

                     // Showing Alert Message
                     alertDialog.show();
        }

【讨论】:

  • 在 onCreate() 之外写这个函数。
  • 如果 Gps 未启用通过调用 Intent 我们是调用设置并且我们启用它同样如果 Internet 移动数据未启用我们如何通过 Intent 启用移动数据例如如果我们想通过 Intent 打开 Gps我们调用 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); lisewise 用于启用移动数据 我们称之为什么意图? @艾米
猜你喜欢
  • 2021-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-07
  • 1970-01-01
相关资源
最近更新 更多