【问题标题】:android detect locatoin using WIFIandroid使用WIFI检测位置
【发布时间】:2009-11-23 04:39:01
【问题描述】:

我们可以使用gps检测代码,我想通过wifi检测位置???

我该怎么做?我希望两个线程同时运行一个使用 GPS 检测位置,另一个使用 WIFI 检测。

【问题讨论】:

    标签: android location wifi


    【解决方案1】:

    查看该页面上的this link 和“public static final String NETWORK_PROVIDER”。

    另外我认为这个开源项目同时使用 GPS 和网络位置:Link。希望是一个很好的例子。

    【讨论】:

    • gReporter 的链接现已断开 :-( 也许他把它当作商业用途?
    【解决方案2】:
    public class FastMainActivity extends Activity {
    
        Button searchBtn = null;
        Intent locatorService = null;
        AlertDialog alertDialog = null;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            searchBtn = (Button) findViewById(R.id.searchBtn);
    
            searchBtn.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
    
                    if (!startService()) {
                        CreateAlert("Error!", "Service Cannot be started");
                    } else {
                        Toast.makeText(FastMainActivity.this, "Service Started",
                                Toast.LENGTH_LONG).show();
                    }
    
                }
            });
    
        }
    
        public boolean stopService() {
            if (this.locatorService != null) {
                this.locatorService = null;
            }
            return true;
        }
    
        public boolean startService() {
            try {
                // this.locatorService= new
                // Intent(FastMainActivity.this,LocatorService.class);
                // startService(this.locatorService);
    
                FetchCordinates fetchCordinates = new FetchCordinates();
                fetchCordinates.execute();
                return true;
            } catch (Exception error) {
                return false;
            }
    
        }
    
        public AlertDialog CreateAlert(String title, String message) {
            AlertDialog alert = new AlertDialog.Builder(this).create();
    
            alert.setTitle(title);
    
            alert.setMessage(message);
    
            return alert;
    
        }
    
        public class FetchCordinates extends AsyncTask<String, Integer, String> {
            ProgressDialog progDailog = null;
    
            public double lati = 0.0;
            public double longi = 0.0;
    
            public LocationManager mLocationManager;
            public VeggsterLocationListener mVeggsterLocationListener;
    
            @Override
            protected void onPreExecute() {
                mVeggsterLocationListener = new VeggsterLocationListener();
                mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    
                mLocationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, 0, 0,
                        mVeggsterLocationListener);
    
                progDailog = new ProgressDialog(FastMainActivity.this);
                progDailog.setOnCancelListener(new OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        FetchCordinates.this.cancel(true);
                    }
                });
                progDailog.setMessage("Loading...");
                progDailog.setIndeterminate(true);
                progDailog.setCancelable(true);
                progDailog.show();
    
            }
    
            @Override
            protected void onCancelled(){
                System.out.println("Cancelled by user!");
                progDialog.dismiss();
                mLocationManager.removeUpdates(mVeggsterLocationListener);
            }
    
            @Override
            protected void onPostExecute(String result) {
                progDailog.dismiss();
    
                Toast.makeText(FastMainActivity.this,
                        "LATITUDE :" + lati + " LONGITUDE :" + longi,
                        Toast.LENGTH_LONG).show();
            }
    
            @Override
            protected String doInBackground(String... params) {
                // TODO Auto-generated method stub
    
                while (this.lati == 0.0) {
    
                }
                return null;
            }
    
            public class VeggsterLocationListener implements LocationListener {
    
                @Override
                public void onLocationChanged(Location location) {
    
                    int lat = (int) location.getLatitude(); // * 1E6);
                    int log = (int) location.getLongitude(); // * 1E6);
                    int acc = (int) (location.getAccuracy());
    
                    String info = location.getProvider();
                    try {
    
                        // LocatorService.myLatitude=location.getLatitude();
    
                        // LocatorService.myLongitude=location.getLongitude();
    
                        lati = location.getLatitude();
                        longi = location.getLongitude();
    
                    } catch (Exception e) {
                        // progDailog.dismiss();
                        // Toast.makeText(getApplicationContext(),"Unable to get Location"
                        // , Toast.LENGTH_LONG).show();
                    }
    
                }
    
                @Override
                public void onProviderDisabled(String provider) {
                    Log.i("OnProviderDisabled", "OnProviderDisabled");
                }
    
                @Override
                public void onProviderEnabled(String provider) {
                    Log.i("onProviderEnabled", "onProviderEnabled");
                }
    
                @Override
                public void onStatusChanged(String provider, int status,
                        Bundle extras) {
                    Log.i("onStatusChanged", "onStatusChanged");
    
                }
    
            }
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-10
      相关资源
      最近更新 更多