0
我正在嘗試編寫一項服務,以便在Android中查找用戶的GP位置。我爲此提供服務。但alwasy當我嘗試啓動服務錯誤嘗試在Android中啓動服務時出錯
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
出現。我已經在谷歌搜索這個問題,並添加了follwoing線到我的源代碼:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
但仍然出現錯誤。我能做些什麼才能使服務無誤地啓動?這裏是我的代碼: MainActivity.java:
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
//GPs Standort bestimmung starten
Intent intent;
intent = new Intent(this, Gps_Service.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(intent);
initButtons();
}
}
「Gps_service.java」:
public class Gps_Service extends Service implements LocationListener{
private double längengrad = 0.0, breitengrad = 0.0;
LocationManager locationManager;
Address adresse;
String straße, Ort, Land;
@Override
public void onCreate(){
super.onCreate();
locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 10000, 100, this);
}
@Override
public void onDestroy(){
super.onDestroy();
locationManager.removeUpdates(this);
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onLocationChanged(Location location) {
längengrad = location.getLongitude();
breitengrad = location.getLatitude();
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
基於錯誤信息提'startActivity'我想你的錯誤,則可能在其他地方在代碼中發生的事情,你沒有顯示。 –
您確定錯誤是由啓動服務引起的嗎?你能顯示整個堆棧的異常嗎? –