无法重新启动 GPS

发布于 2024-12-26 17:59:05 字数 5856 浏览 0 评论 0原文

我正在尝试停止并启动 Gps 位置。当我的应用程序启动时,Gps 已启动。在我的广播接收器中,我需要停止 GPS 并再次启动它。但我的 Gps 并未停止。任何人请纠正帮助我。

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    latituteField = (TextView) findViewById(R.id.TextView02);
    longitudeField = (TextView) findViewById(R.id.TextView04);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // Define the criteria how to select the locatioin provider -> use
    // default
    Criteria criteria = new Criteria();
    Log.i("ConnTest",locationManager.toString());
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);

// Initialize the location fields
if (location != null) {
    System.out.println("Provider " + provider + " has been selected.");
    float lat = (float) (location.getLatitude());
    float lng = (float) (location.getLongitude());
    latituteField.setText(String.valueOf(lat));
    longitudeField.setText(String.valueOf(lng));
    final   double  a=(int) (location.getLatitude() * 1E6);
 final  double  b=(int) (location.getLongitude() * 1E6);
str = Double.toString(a);
str2=Double.toString(b);

} else {
    latituteField.setText("provider not available");
    longitudeField.setText("provider not available");
}

    txtNetworkInfo = (TextView)findViewById(R.id.txtNetworkInfo);

    receiver = new ConnectivityReceiver();

            registerReceiver(receiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));


}
@Override
protected void onResume() {
    super.onResume();
    //locationManager.requestLocationUpdates(provider,1000, 1, this);
    locationManager.requestLocationUpdates(
            provider, 
            MINIMUM_TIME_BETWEEN_UPDATES, 
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
            this
    );
}

/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    float lat = (float) (location.getLatitude());
    float lng = (float) (location.getLongitude());
    longitudeField.setText(String.valueOf(lng));
    latituteField.setText(String.valueOf(lat));


}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "Disenabled provider " + provider,
            Toast.LENGTH_SHORT).show();

}
protected void Display(Cursor c) {
    Toast.makeText(this, "rowid: " + c.getString(0) + "\n" +
     "Latitude: " + c.getString(1) + "\n" + "Longitude: " + c.getString(2) + "\n" +
     Toast.LENGTH_LONG, 0).show();    
}

@Override
    protected void onDestroy() {
            unregisterReceiver(receiver);

            super.onDestroy();
    }

private String getNetworkStateString(NetworkInfo.State state){
    String stateString = "Unknown";

    switch(state)
    {
            case CONNECTED:         stateString = "Connected";break;
            case CONNECTING:        stateString = "Connecting";break;
            case DISCONNECTED:      stateString = "Disconnected"; break;
            case DISCONNECTING:     stateString = "Disconnecting";break;
            case SUSPENDED:         stateString = "Suspended";  break;
            default:                        stateString = "Unknown";break;
    }

    return stateString;
}

    private class ConnectivityReceiver extends BroadcastReceiver{

            private Timer mTimer;
            private TimerTask mTimerTask;

            @Override
            public void onReceive(Context context, Intent intent) {
                    NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

                    if(null != info)
                    {
                            String state = getNetworkStateString(info.getState());
                            if(state.equals("Connected")){
                                mTimer = new Timer();
                                mTimerTask = new TimerTask() {
                                    @Override
                                    public void run() {
                                        MainActivity.this.runOnUiThread(new Runnable() {

                                            @Override
                                            public void run() {
                                                locationManager.removeUpdates(this);
                                                stop();
                                            }
                                        });

                                   }
                                };
                                mTimer.scheduleAtFixedRate(mTimerTask,180000,180000);
                            }


                            }
            }   
                    }

            }
    private void stop() {
        mTimer = new Timer();
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                MainActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                          locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                    }
                });

           }
        };
        mTimer.scheduleAtFixedRate(mTimerTask,180000,180000);

    }
    }

Hai am trying to stop and Start the Gps location.when my Application is Start the Gps is Started.In my broadCast receiver i need to stop the gps and again start it.But my Gps is not Stopped .Anybody please rectify help me.

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    latituteField = (TextView) findViewById(R.id.TextView02);
    longitudeField = (TextView) findViewById(R.id.TextView04);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // Define the criteria how to select the locatioin provider -> use
    // default
    Criteria criteria = new Criteria();
    Log.i("ConnTest",locationManager.toString());
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);

// Initialize the location fields
if (location != null) {
    System.out.println("Provider " + provider + " has been selected.");
    float lat = (float) (location.getLatitude());
    float lng = (float) (location.getLongitude());
    latituteField.setText(String.valueOf(lat));
    longitudeField.setText(String.valueOf(lng));
    final   double  a=(int) (location.getLatitude() * 1E6);
 final  double  b=(int) (location.getLongitude() * 1E6);
str = Double.toString(a);
str2=Double.toString(b);

} else {
    latituteField.setText("provider not available");
    longitudeField.setText("provider not available");
}

    txtNetworkInfo = (TextView)findViewById(R.id.txtNetworkInfo);

    receiver = new ConnectivityReceiver();

            registerReceiver(receiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));


}
@Override
protected void onResume() {
    super.onResume();
    //locationManager.requestLocationUpdates(provider,1000, 1, this);
    locationManager.requestLocationUpdates(
            provider, 
            MINIMUM_TIME_BETWEEN_UPDATES, 
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
            this
    );
}

/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    float lat = (float) (location.getLatitude());
    float lng = (float) (location.getLongitude());
    longitudeField.setText(String.valueOf(lng));
    latituteField.setText(String.valueOf(lat));


}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "Disenabled provider " + provider,
            Toast.LENGTH_SHORT).show();

}
protected void Display(Cursor c) {
    Toast.makeText(this, "rowid: " + c.getString(0) + "\n" +
     "Latitude: " + c.getString(1) + "\n" + "Longitude: " + c.getString(2) + "\n" +
     Toast.LENGTH_LONG, 0).show();    
}

@Override
    protected void onDestroy() {
            unregisterReceiver(receiver);

            super.onDestroy();
    }

private String getNetworkStateString(NetworkInfo.State state){
    String stateString = "Unknown";

    switch(state)
    {
            case CONNECTED:         stateString = "Connected";break;
            case CONNECTING:        stateString = "Connecting";break;
            case DISCONNECTED:      stateString = "Disconnected"; break;
            case DISCONNECTING:     stateString = "Disconnecting";break;
            case SUSPENDED:         stateString = "Suspended";  break;
            default:                        stateString = "Unknown";break;
    }

    return stateString;
}

    private class ConnectivityReceiver extends BroadcastReceiver{

            private Timer mTimer;
            private TimerTask mTimerTask;

            @Override
            public void onReceive(Context context, Intent intent) {
                    NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

                    if(null != info)
                    {
                            String state = getNetworkStateString(info.getState());
                            if(state.equals("Connected")){
                                mTimer = new Timer();
                                mTimerTask = new TimerTask() {
                                    @Override
                                    public void run() {
                                        MainActivity.this.runOnUiThread(new Runnable() {

                                            @Override
                                            public void run() {
                                                locationManager.removeUpdates(this);
                                                stop();
                                            }
                                        });

                                   }
                                };
                                mTimer.scheduleAtFixedRate(mTimerTask,180000,180000);
                            }


                            }
            }   
                    }

            }
    private void stop() {
        mTimer = new Timer();
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                MainActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                          locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                    }
                });

           }
        };
        mTimer.scheduleAtFixedRate(mTimerTask,180000,180000);

    }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再让梦枯萎 2025-01-02 17:59:05

您应该在调用 onPause() 时禁用更新。当你按回来时你的GPS没有被禁用?是否有其他正在运行的应用程序或服务正在使用 GPS?

You should be disabling the updates when onPause() is called. When you press back your gps isn't disabled? is there maybe another app or service running that is using GPS?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文