首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ACTION_WIRELESS_SETTINGS进行连接设置

使用ACTION_WIRELESS_SETTINGS进行连接设置
EN

Stack Overflow用户
提问于 2013-07-12 01:52:02
回答 1查看 928关注 0票数 0

有没有人可以指导一下如何检查有条件的网络连接设置?我需要类似于这组代码的东西。

代码语言:javascript
复制
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service
  .isProviderEnabled(LocationManager.GPS_PROVIDER);

if (!enabled) {
  Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  startActivity(intent);
}

下面是我的代码,我想取代GPS检查到互联网连接检查。

代码语言:javascript
复制
public class MainActivity extends FragmentActivity implements LocationListener, LocationSource{

private GoogleMap map;
private OnLocationChangedListener mListener;
private LocationManager locationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
        boolean enabled = service
          .isProviderEnabled(LocationManager.GPS_PROVIDER);

        if (!enabled) {
          Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
          startActivity(intent);
        }

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        if(locationManager != null)
        {
            boolean gpsIsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            boolean networkIsEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            if(gpsIsEnabled)
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000L, 10F, this); 
            else if(networkIsEnabled)   
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000L, 10F, this);     
            }
        setUpMapIfNeeded();
    } 

    //after user install/update Google Play Service, user might return to this activity
    //to stop or resume the activity, onPause and onResume is needed
    @Override
    public void onPause()
    {
        if(locationManager != null)
        {
            locationManager.removeUpdates(this);
        }
        super.onPause();
    }

    @Override
    public void onResume()
    {
        super.onResume();
        setUpMapIfNeeded();
        if(locationManager != null)
        {
            map.setMyLocationEnabled(true); //detect current location
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2014-02-14 15:16:09

你可以使用像这样的东西..

代码语言:javascript
复制
public boolean isOnline(final Context context) {
ConnectivityManager cm = (ConnectivityManager)         context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
Builder  dialogb = new AlertDialog.Builder(context);
if (netInfo != null && netInfo.isConnected()) {
    return true;
}
       dialogb.setTitle("No Internet.. :(");
       dialogb.setMessage("We need internet to work. Kindly switch it on.");
       dialogb.setPositiveButton("Turn on", new DialogInterface.OnClickListener() {

           @Override
           public void onClick(DialogInterface paramDialogInterface, int paramInt) {
               // TODO Auto-generated method stub
               Intent myIntent = new Intent( Settings.ACTION_WIRELESS_SETTINGS);
               context.startActivity(myIntent);
               //get gps

           }
       });
       dialogb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

           @Override
           public void onClick(DialogInterface paramDialogInterface, int paramInt) {
               // TODO Auto-generated method stub


           }
       });
       dialogb.show();
return false;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17600162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档