android使用WIFI检测locatoin

android使用WIFI检测locatoin,android,location,wifi,Android,Location,Wifi,我们可以使用gps检测代码,我想通过wifi检测位置 我该怎么做?我想要两个线程模拟运行一个使用GPS的检测位置,另一个使用WIFI的检测位置。签出并在该页面上显示“公共静态最终字符串网络\u提供程序” 此外,我认为这个开源项目同时使用gps和网络位置:。希望这是一个很好的例子。公共类FastMainActivity扩展了活动{ public class FastMainActivity extends Activity { Button searchBtn = null; I

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

我该怎么做?我想要两个线程模拟运行一个使用GPS的检测位置,另一个使用WIFI的检测位置。

签出并在该页面上显示“公共静态最终字符串网络\u提供程序”

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

公共类FastMainActivity扩展了活动{
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");

            }

        }

    }

}
按钮searchBtn=null; 意向定位器服务=null; AlertDialog AlertDialog=null; /**在首次创建活动时调用*/ @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); searchBtn=(按钮)findviewbyd(R.id.searchBtn); searchBtn.setOnClickListener(新的OnClickListener(){ @凌驾 公共void onClick(视图v){ //TODO自动生成的方法存根 如果(!startService()){ CreateAlert(“错误!”,“服务无法启动”); }否则{ Toast.makeText(FastMainActivity.this,“服务已启动”, Toast.LENGTH_LONG).show(); } } }); } 公共布尔停止服务(){ if(this.locatorService!=null){ this.locatorService=null; } 返回true; } 公共布尔startService(){ 试一试{ //this.locatorService=new //意图(FastMainActivity.this、LocatorService.class); //startService(此.locatorService); FetchCordinates FetchCordinates=新的FetchCordinates(); fetchCordinates.execute(); 返回true; }捕获(异常错误){ 返回false; } } 公共警报对话框CreateAlert(字符串标题、字符串消息){ AlertDialog alert=新建AlertDialog.Builder(this.create(); 警报。设置标题(标题); 警报。设置消息(消息); 返回警报; } 公共类FetchCordinates扩展异步任务{ ProgressDialog progDailog=null; 公共双纬度=0.0; 公共双长=0.0; 公共场所经理; 公共蔬菜位置监听器mVeggsterLocationListener; @凌驾 受保护的void onPreExecute(){ mVeggsterLocationListener=新的VeggsterLocationListener(); mLocationManager=(LocationManager)getSystemService(Context.LOCATION\u服务); mLocationManager.RequestLocationUpdate( LocationManager.NETWORK_提供程序,0,0, mVeggsterLocationListener); progDailog=新建ProgressDialog(FastMainActivity.this); setOnCancelListener(新的OnCancelListener(){ @凌驾 public void onCancel(对话框接口对话框){ FetchCordinates.this.cancel(true); } }); setMessage(“正在加载…”); progDailog.setUndeterminate(true); progDailog.setCancelable(真); progDailog.show(); } @凌驾 受保护的void onCancelled(){ System.out.println(“由用户取消!”); progDialog.discouse(); mLocationManager.RemoveUpdate(mVeggsterLocationListener); } @凌驾 受保护的void onPostExecute(字符串结果){ progDailog.disclose(); Toast.makeText(FastMainActivity.this, “纬度:”+lati+“经度:”+longi, Toast.LENGTH_LONG).show(); } @凌驾 受保护的字符串doInBackground(字符串…参数){ //TODO自动生成的方法存根 而(this.lati==0.0){ } 返回null; } 公共类VeggsterLocationListener实现LocationListener{ @凌驾 已更改位置上的公共无效(位置){ int lat=(int)location.getLatitude();//*1E6); int log=(int)location.getLongitude();//*1E6); int acc=(int)(location.getAccurance()); String info=location.getProvider(); 试一试{ //LocatorService.myLatitude=location.getLatitude(); //LocatorService.MyLongQuantity=location.GetLength(); lati=location.getLatitude(); longi=location.getLongitude(); }捕获(例外e){ //progDailog.disclose(); //Toast.makeText(getApplicationContext(),“无法获取位置” //,Toast.LENGTH_LONG)show(); } } @凌驾 公共无效onProviderDisabled(字符串提供程序){ Log.i(“OnProviderDisabled”、“OnProviderDisabled”); } @凌驾 公共无效onProviderEnabled(字符串提供程序){ Log.i(“onProviderEnabled”、“onProviderEnabled”); } @凌驾 public void onStatusChanged(字符串提供程序,int状态, 捆绑(附加){ Log.i(“onStatusChanged”、“onStatusChanged”); } } } }
到gReporter的链接现在断开了:-(也许他把它当成了商业广告?