Androidで1日に1回アプリケーションを起動したときにサービスクラスを呼びたいとします。サービスクラスを呼び出すアプリケーションが1日に1回Androidで起動し、バッテリー消費量が少ない
マニフェストコード:
<service android:name=".DailyRemainder" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</service>
Javaコード:コードの上
// for Repeating Process
Intent myIntent = new Intent(sign_in.this,.DailyRemainder.class);
pendingIntent1 = PendingIntent.getService(sign_in.this, 0,myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 20); alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent1);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent1);
バックグラウンドで実行されていることは、私の携帯電話のバッテリー、 を排出するが、ユーザがアプリケーションその時間サービスを開始したときに私が必要1日にを実行する必要があります。これを達成する方法。
を実行し、そこに来るアラームマネージャの一部を削除それを持つ多くの設定。必要に応じて設定することができます。 – UMESH0492
これを明確にします。アプリケーションを開いたときにサービスが開始されますか?アプリケーションが終了した時点でサービスを開始しません。共有環境設定でサービスの最後の開始のタイムスタンプを保存し、アプリケーションを起動する日をチェックすることができます。あなたの場合ですか? –
Job SchedulerはAPI <21では使用できません。 – Talha