2016-08-14 8 views
1

私は、特定のアクティビティーが開いている場合にのみ、私のデータベースから(現在の時刻よりも古い)特定の行を削除、自動を実装する必要がある場合this articleによると、私はResultReceiverを使用する必要があります。ResultreceiverとAlarmManagerを併用するには?

ResultReceiver - 送信するための一般的なコールバックインタフェースサービスと活動の結果。サービスが親アプリケーションと1か所で接続する必要がある場合は、この方法を使用してください。

とAlarmManager:将来的にまたは定期的な間隔

で指定した時刻に

トリガーが、私はResultReceiverでAlarmManagerを使用する方法を知ってはいけない問題に直面しました。私は私のIntentServiceをActivityから呼び出す方法を理解していません。

@Override 
    protected void onHandleIntent(Intent intent) { 
     Log.d(LOG_TAG, "Inside onHandleIntent"); 
     ResultReceiver rec = intent.getParcelableExtra(IntentConstants.UPDATE_REMINDERS_SERVICE_RECEIVER); 
     int rowsDeleted = notificationService.deleteAllOld(Calendar.getInstance().getTimeInMillis()); 

     Bundle bundle = new Bundle(); 
     bundle.putString(IntentConstants.UPDATE_REMINDERS_SERVICE_NOTIFICATIONS_DELETED, "Rows deleted: " + rowsDeleted); 
     // Here we call send passing a resultCode and the bundle of extras 
     rec.send(Activity.RESULT_OK, bundle); 

    } 

と活動中:私はonStartServiceメソッド内AlarmManagerを実装しようとしましたが、それは動作しません

// Starts the IntentService 
    public void onStartService() { 
     Log.d("UpdateRemindersLog", "Inside onStartService"); 
     final Intent i = new Intent(this, UpdateRemindersService.class); 
     i.putExtra(IntentConstants.UPDATE_REMINDERS_SERVICE_RECEIVER, remindersReceiver); 
     startService(i); 
    } 

私の意向サービスで

は、私はそのようなものを持っています。

Intent intent = new Intent(getApplicationContext(), UpdateRemindersReceiver.class); 
intent.putExtra(IntentConstants.UPDATE_REMINDERS_SERVICE_RECEIVER, remindersReceiver); 
// Create a PendingIntent to be triggered when the alarm goes off 
final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, UpdateRemindersService.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

long firstMillis = System.currentTimeMillis(); // alarm is set right away 
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, 10*1000, pendingIntent); 

だから、誰かが私はIntentServiceでAlarmManagerを実装する方法を私に説明することができます。私はそれがつもり、このようなことだと思いましたか? ありがとうございます。

答えて

0

PendingIntent.getBroadcast()コールのターゲットは、メソッドが示唆するように、ブロードキャストレシーバである必要があります。 AlarmManagerは、Intentでのみ動作します。

ですから、AlarmManagerの信号を処理してからサービスを開始し、放送受信装置を追加する必要があります。また

public class AlarmReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     // Start your service here 
    } 
} 

、あなたのapplicationの内側(このように、あなたのAndroidManifest.xmlファイルにあなたの受信機を登録することを忘れないでくださいタグ):

<receiver 
    android:name=".AlarmReceiver" 
    android:enabled="true" /> 

そして、あなたはあなたのIntentあなたが今、あなたの新しい受信機のクラス(AlarmReceiver.class)を標的とすべきを作成します。

+0

IntentServiceとAlarmManagerが両方とも「ランチャー」であることがわかりました。だから私のコードは地獄のように馬鹿だった。しかし、どんなに助けてくれる男なのか! –

関連する問題