0

アクティビティから別のアクティビティへの場合は正常に機能するインテントを介して値を渡そうとしました。しかし今回は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

1つの問題は確かにこれです:

startActivity(intent);

を開始しようとしている場合はService、 を呼び出すべきではありませんstartActivitystartService代わりに試してください。

質問でそれを変更するのを忘れたと仮定すると、あなたが指摘した他の問題はgetIntent. AServiceにはその方法がありません。それはActivity方法です。サービスは通常、多くの場合同時に多くのインテントに関連付けられているため、getIntentメソッドはあまり意味がありません。のドキュメントService、特にライフサイクル セクションをよく読んでください。

Intentに関連するコードを入れたいと思うでしょうonStartCommand

于 2013-01-09T21:14:13.500 に答える