0

我试图通过一个意图传递我的价值,如果它是从 Activity 到另一个 Activity,它工作得很好。但这次是从一个Activity到一个Service。我认为这是正确的方法?但我收到一个错误服务类

我的活动

@Override
    protected void onPause() {
        // TODO Auto-generated method stub
        myLocation.disableCompass();
        super.onPause();
        locationManager.removeUpdates(this);


    Intent intent = new Intent(this, LocationLoggerService.class);
    intent.putExtra("midllat", midllat);
    intent.putExtra("midlongi", midlongi);
    startActivity(intent);

}

我的服务等级

@Override
public void onCreate() {
    // Find my current location
    locationManager = (LocationManager) this
            .getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            2000, 0, this);
    Criteria crit = new Criteria();
    towers = locationManager.getBestProvider(crit, false);
    Location location = locationManager.getLastKnownLocation(towers);
    Toast.makeText(this, "The app is know running in the background",
            Toast.LENGTH_LONG).show();
    gm = new GoogleMaps();

我在 getIntent() 中遇到错误它找不到方法,是因为在服务类中吗?该怎么办?

    Intent intent = getIntent();
    String test1 = intent.getStringExtra("midllat");
    String test2 = intent.getStringExtra("midlongi");

    midllat = Double.parseDouble(test1);
    midlongi = Double.parseDouble(test2);

}
4

1 回答 1

0

一个问题当然是这样的:

startActivity(intent);

如果您尝试启动 a Service,则不应调用startActivity. 试试startService吧。

假设您只是忘记在问题中更改它,那么您指出的另一个问题是getIntent. AService没有那个方法。那是一种Activity方法。服务通常与许多意图相关联,通常是同时发生的,因此一种getIntent方法实际上没有意义。通读文档Service,尤其是生命周期部分

您可能希望将您的Intent相关代码放入onStartCommand.

于 2013-01-09T21:14:13.500 回答