2012-02-14 18 views
0

ボタンをクリックしてアラームマネージャを使用してサービスを開始します。それも働いています。私はこのようにしました:ブロードキャスト受信機によるサービスの自動開始

Intent scheduleSMS = new Intent(SMSProjectDatabase.this,SendSMS.class); 
    PendingIntent pendingIntent = PendingIntent.getService(SMSProjectDatabase.this,0, scheduleSMS,0); 
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

    Calendar calendar = Calendar.getInstance(); 

    calendar.setTimeInMillis(System.currentTimeMillis()); 
    calendar.add(Calendar.SECOND,(int)GeneralUtil.calculateTimeDifference(getApplicationContext())); 
    Log.i("SMS is scheduled after ",""+GeneralUtil.calculateTimeDifference(getApplicationContext())); 

    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 

ここで、SendSMSはアラームマネージャによって開始されるサービスです。しかし、これは私のコードでサービスを開始する方法ではありません。私は、AlarmManagerを使用してBroadcast Receiverのブロードキャストをトリガーします。 Broadcastreceiverは私のためにサービスを開始します。

私のブロードキャストレシーバーは

パブリッククラスMyReceiverは{

@Override 
public void onReceive(Context context, Intent intent) { 
    //here i want to start the service through the alarm manager 

} 

}

しかし、BroadcastReceiverを拡張している私donno私は私が解決するために適切なソリューションを提供this.Please行うことになっていますかこのproblelm。

答えて

0

あなたはただ単にonReceive()

@Override 
public void onReceive(Context arg0, Intent arg1) { 
    // To get AlarmManager instance working you can use Context arg0 
    AlarmManager alarmManager = (AlarmManager) 
              arg0.getSystemService(ALARM_SERVICE); 
    // put your stuff to start Service using AlarmManager 
} 
AlarmManagerを使用して Serviceを呼び出すために自分のものを配置する必要があります
0

registerReceiver()関数を使用できます。 しかし、あなたはアララムマネジャーが放送を開始させたいと思っています。 Broadcastreceiverは、何かがアプリケーションにブロードキャストされたときにブロードキャストを受信するために使用されます。

関連する問題