0

12 時間ごとに繰り返す必要があるアラーム マネージャーがあります。

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent in = new Intent(this, myReceiver.class);
        myAlarmSender = PendingIntent.getBroadcast(getApplicationContext(), 0, in, 0);
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), time, myAlarmSender);

その後、ブロードキャストレシーバーを拡張するクラス myReceiver でトリガーイベントをキャッチします

@Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "text", Toast.LENGTH_LONG).show();
    }

ステータスバー通知を設定するか、通知用のコードがある通知クラスを開始したいのですが、新しいインテントを開始したり、onReceive() メソッド内に通知用のコードを記述したりできません。ヘルプ?ありがとう、ウルフ。

4

2 に答える 2

1

なぜだめですか?

@Override
public void onReceive(final Context context, final Intent bootintent)
{
    Intent anyService = new Intent(context, AnyService.class);
    context.startService(anyService);
}
于 2012-05-10T18:40:12.500 に答える
1

私の友人は私があなたのためにすること:

http://blog.blundell-apps.com/notification-for-a-user-chosen-time/

あなたはそれがサービスを開始するという点であなたのAlarmManagerがあなたのアラームを設定することを望むでしょう。次に、サービスが開始されたときに通知を表示できます。

アラームを設定します。

public class AlarmTask implements Runnable{
    // The date selected for the alarm
    private final Calendar date;
    // The android system alarm manager
    private final AlarmManager am;
    // Your context to retrieve the alarm manager from
    private final Context context;

    public AlarmTask(Context context, Calendar date) {
        this.context = context;
        this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        this.date = date;
    }

    @Override
    public void run() {
        // Request to start are service when the alarm date is upon us
        // We don't start an activity as we just want to pop up a notification into the system bar not a full activity
        Intent intent = new Intent(context, NotifyService.class);
        intent.putExtra(NotifyService.INTENT_NOTIFY, true);
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

        // Sets an alarm - note this alarm will be lost if the phone is turned off and on again
        am.set(AlarmManager.RTC, date.getTimeInMillis(), pendingIntent);
    }
}

これでサービスが開始されますNotifyService

次に、NotifyServiceクラスで:

private void showNotification() {
        // This is the 'title' of the notification
        CharSequence title = "Alarm!!";
        // This is the icon to use on the notification
        int icon = R.drawable.ic_dialog_alert;
        // This is the scrolling text of the notification
        CharSequence text = "Your notification time is upon us";
        // What time to show on the notification
        long time = System.currentTimeMillis();

        Notification notification = new Notification(icon, text, time);

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, SecondActivity.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, title, text, contentIntent);

        // Clear the notification when it is pressed
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Send the notification to the system.
        mNM.notify(NOTIFICATION, notification);

        // Stop the service when we are finished
        stopSelf();
    }
于 2012-05-10T18:42:06.650 に答える