私はEclipseの下でAndroidのコードを書いています。私はMyService1
とMyService2
という2つのサービスを扱います。AlarmManagerを使用して別のサービスからサービスを開始する
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, NumSec);
long time = cal.getTimeInMillis();
Intent i = new Intent(getApplication(), MyService1.class);
PendingIntent pintent = PendingIntent.getBroadcast(getApplication(), 0, i, 0);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, time, AlarmManager.INTERVAL_DAY, pintent);
これはOKですし、うまく機能:最初の1は、次のように `MainActivity」からAlaramManager
によってトリガされます。 今度は、同じ方法でAlarmManager
を使用してMyService2
をMyService1
から再度開始するつもりです。
Intent myIntent = new Intent(MyService1.this, MyService2.class);
私は機能Intent
2つの引数を持ち、最初のものはタイプContext
であることを知っている:しかし、私のこのコード行でエラーが発生しました。私はいくつかの方法を試しましたが、それらのものは動作しません!例:
Intent myIntent = new Intent(getBaseContext(), MyService2.class);
Intent myIntent = new Intent(getApplication(), MyService2.class);
何か提案がありますか? ありがとうございます。次のように
[編集] 私の最初のサービスは以下のとおりです。
public class MyService1 extends BroadcastReceiver {
@Override
public void onReceive(Context con, Intent arg1) {
AlarmManager am = (AlarmManager) con.getSystemService(Service.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
long time = cal.getTimeInMillis();
Intent i = new Intent(con, MyUpService.class);
PendingIntent pint = PendingIntent.getBroadcast(con, 0, i, 0);
am.set(AlarmManager.RTC_WAKEUP, time, pint);
}
}
を使用することができます
Context Object
を持っていますか?他のスレッドでインテントを作成していますか? – momo
親愛なる@momo私は最初のサービスを追加しました。 –