简体   繁体   English

尝试在Android中启动服务时出错

[英]Error when trying to start a Service in Android

I'm trying to write a service to locate the user by it's GPs location in Android. 我正在尝试编写服务以通过Android中的GP位置来定位用户。 I wrotre a service for that. 我为此提供了服务。 But alwasy when I try to start the service the error 但是当我尝试启动该服务时出现错误

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? appears. 出现。 I already searched for this problem on google and added the follwoing line to my sourcecode: 我已经在Google上搜索了此问题,并在我的源代码中添加了以下行:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

But the Error still appears. 但是错误仍然出现。 What can I do in order to make the service start without any errors? 为了使服务启动时没有出现任何错误,我该怎么办? Here is my code: MainActivity.java: 这是我的代码: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": “ 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) {}
}

Instead of running a service to retreive the user's location via GPS, use Google's Fused Location API . 无需运行服务来通过GPS检索用户的位置,而是使用Google的融合位置API

The link provided has a detailed guide about how to implement it and is much more reliable and accurate in receiving location data. 所提供的链接提供了有关如何实现它的详细指南,并且在接收位置数据时更加可靠和准确。

Don't forget this gradle dependency as well: 也不要忘记此gradle依赖项:

compile 'com.google.android.gms:play-services-location:8.4.0'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM