0

したがって、私の目標は、1 日の特定の時間にユーザーに通知を作成するようにアプリを作成することです。このために、私は AlarmManager を使用してアラームを設定しています。アラームが「鳴る」と、通知が作成されます。設定方法は次のとおりです-

クラス 1 - アラームの作成 クラス 2 - 通知の作成 クラス 3 - 通知からの応答

アラームと通知の作成に「意図」を使用するため、これら 3 つのクラスを作成しました。

ここに私のコードがあります -

cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 30);
    calintent = new Intent(this, NotificationMaker.class);
    calpendingintent = PendingIntent.getActivity(this, 12345, calintent, PendingIntent.FLAG_CANCEL_CURRENT);
    am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), calpendingintent);

そして、それは NotificationMaker クラスを呼び出します -

    notifintent= new Intent(this, NotificationReceiver.class);
    notifpendingintent = PendingIntent.getActivity(this, 0, notifintent, 0);

    notif = new Notification.Builder(this)
        .setContentTitle("Time To Smoke!")
        .setContentIntent(notifpendingintent)
        .addAction(R.drawable.background_teal, "Open App", notifpendingintent)
        .addAction(R.drawable.background_red, "I smoked", notifpendingintent).build();
    notifm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    notif.flags |= Notification.PRIORITY_MAX;
    notif.flags |= Notification.FLAG_NO_CLEAR;
    notif.flags |= Notification.FLAG_AUTO_CANCEL;

    notifm.notify(0, notif);

通知が行われています。これに関していくつか質問があります。

1) 複数のクラスを使用せずに、やりたい 2 つのタスクを実行できますか? もしそうなら、どのように?(同じクラスでアラームと通知を行います)。2) アラームが鳴るたびに白紙のレイアウトが開き、通知が行われます。白紙のページを開かないようにするにはどうすればよいですか? 3) 2 つのタスクを実行するためのより効率的な方法はありますか (時間指定通知を作成するためにアラームを作成します)。

前もって感謝します!

4

1 に答える 1

0

Activity を使用して Notification を作成していることがわかる限り、BroadcastReciever を使用して、AlarmManager で保留中の意図を使用してブロードキャストを開始することをお勧めします。

BroadcastReceiverの詳細はこちら

于 2013-07-11T07:56:34.030 に答える