0

アプリケーションは非常にシンプルです。私はすべての活動などを備えた通常のアプリを持っています...

アプリケーションの最初の起動時および/またはデバイスの再起動時に起動するサービスがあります。

通知インテントを処理するようにBroadcastReceiverを設定しましたが、これも非常に基本的で、完全に機能します。

通知は、Androidデバイスのディスプレイがオンのときに完全に機能します。しかし、画面をオフにすると、通知は作成されなくなります。

私は通知に慣れていないので、おそらく見落としている単純なものですが、それを見つけることができないようです。

私のサービスでは、1分に1回起動し、scheduleメソッドの一部としてTimerTaskを受け取るTimerを含むワーカースレッドを構築します。


@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    boolean isStopping = false;
    if (intent != null)  // if the system has to shut us down, go down gracefully
    {
        String intentAction = intent.getStringExtra(AppData.BROADCAST_ACTION);
        if (intentAction != null && intentAction.equals("android.intent.action.REBOOT"))
        {
            boolean isRebooting = intent.getBooleanExtra(AppData.SYSTEM_ACTION, false);
            if (isRebooting)
            {
                isStopping = true;
                stopSelf();
            }
        }
    }

    if (!isStopping && !_workerThread.isRunning())
    {
        _workerThread.run();
    }

    return Service.START_STICKY;
}

_timer.schedule(_service.getWorker(), startDelay, executionDelay);

NotificationTask(TimerTask)は、最終的にはタイマーから1分ごとに起動されます。次に、いくつかのロジックをチェックして、通知を作成する必要があるかどうかを確認します。作成する必要がある場合は、次のような通知を作成して送信します。


Notification notification = new NotificationCompat.Builder(_context)
        .setContentTitle(getString(R.string.notification))
        .setContentText(message)
        .setLargeIcon(decodeResource(_context.getResources(), iconId))
        .setSmallIcon(R.drawable.logo_mark)
        .setTicker(ticker)
        .setAutoCancel(true)
        .setSound(getRingtone(ringtoneUrl))
        .setVibrate(VIBRATION_PATTERN)
        .setLights(0xfff89829, 300, 5000)
        .setContentIntent(intent)
        .build();

_notificationManager.notify(notificationId, notification);

通知は通知の種類によって異なり、次の通知で上書きする前にユーザーが応答したかどうかは関係ありません。私の問題は、画面がオフの場合に通知が届かないことです。

助けてください。


アップデート

誰かが私と同じように行き詰まった場合に備えて、コードを追加したかったのです。

AndroidManifestから始めましょう。2つのBroadcastReceiversとあなたのサービスを含める


<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<service android:name=".services.NotificationService"/>

<receiver android:name=".receivers.BootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>
<receiver android:name=".receivers.NotificationReceiver" />

ブートレシーバーは、デバイスが再起動されたときにアラームをスケジュールするだけです。次のように、他の受信者を呼び出すように設定します。


AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, NotificationReceiver.class), 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance(), 60 * 1000, pendingIntent);

メインレシーバーがサービスを起動します。サービスは、通常のサービスではなくIntentServiceを拡張する必要があります。主な受信機は次のとおりです。


context.startService(new Intent(context, NotificationService.class));

そしてサービス


public class NotificationService extends IntentService
{
    public NotificationService()
    {
        super("Notification Service");
    }

    @Override
    public void onHandleIntent(Intent intent)
    {
        // this is where I created my notifications
    }
}

最後のちょっとしたコツは、アプリケーションでAlarmManagerを再度作成することです。これにより、ブートレシーバーで作成された可能性のあるAlarmManagerが置き換えられます。ContinuedIntent.FLAG_CANCEL_CURRENTに注意してください。これは、ユーザーがアプリケーションを起動したときに、デバイスの起動時にまだ登録されていない場合に、AlarmManagerが登録されるようにするためです。


AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, NotificationReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, Utils.getNow(), 60 * 1000, pendingIntent);

お役に立てれば。

4

1 に答える 1

2

私はTimersとTimerTasksの経験があまりないので、間違っていると言っているわけではありませんが、AlarmManagerは、画面がオフになっているときなど、将来実行するようにスケジュールする標準的な方法だと思いました。

于 2012-10-31T22:09:57.513 に答える