2016-05-04 9 views
0

私はEclipseの下でAndroidのコードを書いています。私はMyService1MyService2という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を使用してMyService2MyService1から再度開始するつもりです。

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); 
    } 
} 
+0

を使用することができますContext Object

public void onReceive(Context con, Intent arg1) { } 

を持っていますか?他のスレッドでインテントを作成していますか? – momo

+0

親愛なる@momo私は最初のサービスを追加しました。 –

答えて

0

は、だからあなたはあなたが最初のサービスからのコードを共有することができ、この

Intent myIntent = new Intent(con, MyService1.class); 
Intent myIntent1 = new Intent(con, MyService2.class); 
関連する問題