在我的應用程序重新啓動設備後位置更新停止。 我正在使用gps和網絡來查找我的應用程序中的當前位置,重新啓動後gps工作正常,但是當我關閉gps時,應用程序不幸關閉。 這是我的問題重新啓動設備後位置更新使用NETWORK_PROVIDER停止
這是我的代碼Servicestart.java
public class Servicestart extends Service {
boolean gps_enabled = false;
boolean network_enabled = false;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.i("service started", "start");
final LocationManager locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if(userFunctions.isUserLoggedIn(getApplicationContext())){
if(Broadcast.check==false)
{
LocationListener locatioListner = new LocationListener() {
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
get();
}
public void onLocationChanged(Location location) {
Log.i("location", "loc");
String latitude=String.valueOf(location.getLatitude());
String longtitude=String.valueOf(location.getLongitude());
//location updated starts here
}
};
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locatioListner);
}
}
}
void get()
{
final LocationManager locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
gps_enabled = locMan.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!gps_enabled && !network_enabled) { Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, "nothing is enabled", duration);
toast.show();
}
LocationListener locatioListnerGps = new LocationListener() {
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "gpsenabled", 1000).show();
}
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "gps disable", 1000).show();
get();
}
public void onLocationChanged(Location location) {
String latitude=String.valueOf(location.getLatitude());
String longtitude=String.valueOf(location.getLongitude());
//location updated starts here
}
};
LocationListener locationListenerNetwork = new LocationListener() {
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "gpsenabled", 1000).show();
}
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "gps disable", 1000).show();
get();
}
public void onLocationChanged(Location location) {
String latitude=String.valueOf(location.getLatitude());
String longtitude=String.valueOf(location.getLongitude());
//location updated starts here
}
};
if (gps_enabled)
{
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locatioListnerGps);
locMan.removeUpdates(locationListenerNetwork);
}
if(network_enabled){
locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
locationListenerNetwork);
locMan.removeUpdates(locatioListnerGps);
}
}
}
請幫助和指導我...
因爲我提前beginer
謝謝...
我相信你沒有編碼它重新啓動後自動運行。請訪問此問題[如何在啓動時啓動應用程序?](http://stackoverflow.com/q/6391902/2567598) – Vigbyor
@Vigbyor我有一個代碼,在重新啓動後自動運行。但我的問題不是network_provider在重新啓動後不工作.. – rohit