テスト目的で、1分ごとにビープ音を鳴らすサービスを作成しました。(まだクライアント サーバー インターフェイスはありません)。画面がオンのときは問題なくビープ音が鳴りますが、スリープ状態になるとビープ音が止まります。
サーバーに何かを定期的にポーリングする必要があるアプリケーションを作成しています。
このために、バックグラウンドで常に実行されるサービスを作成し、1分ごとにサーバーをポーリングし、サーバーからの応答に基づいてタスクバー通知を生成しようとしています。
サービスを開始するボタンとサービスを停止するボタンの 2 つのボタンがあるテスト アクティビティがあります。そして、S_PS_PollService という名前の 1 つのサービス クラス
「Start Activity」ボタンの setOnClickListener には次が含まれます。
Thread pollServiceThread = new Thread() {
public void run() {
startService(new Intent(MM_MainMenu.this,
S_PS_PollService.class));
}
};
pollServiceThread.start();
「アクティビティを停止」ボタンには次の機能があります。
stopService(new Intent(MM_MainMenu.this, S_PS_PollService.class));
S_PS_PollService クラスのメソッドは次のとおりです。
public void onCreate() {
pollSound = MediaPlayer.create(this, R.raw.chirp);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent myIntent = new Intent(this, S_PS_PollService.class);
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
// for wake lock
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag")
// for calendar
calendar = Calendar.getInstance();
}
開始時:
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
wl.acquire();
pollSound.start();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MILLISECOND, 60000);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
wl.release();
}
アラームが開始されるたびに onStart() メソッドが実行され、ビープ音が鳴り、新しいアラームが設定されます。ただし、画面がオンになっている間のみ機能します。
https://github.com/commonsguy/cwac-wakefulを試しましたが、取得できませんでした。アンドロイドは比較的初心者...
私を助けてください、私は非常に必死です:)ありがとう、!