0

次の関数を作成した、設定した日時に起動する通知を作成したいと思います。

public void notify(View v){
    Context context = AllClasses.this;
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_WEEK, 4);
    calendar.set(Calendar.HOUR_OF_DAY, 17);
    calendar.set(Calendar.MINUTE, 31);
    calendar.set(Calendar.SECOND, 0);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, Notify.class), PendingIntent.FLAG_UPDATE_CURRENT);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
}

および対応するクラス:

public class Notify extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
    NotificationManager nm;
    nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);        
    CharSequence from = "VIPUL";
    CharSequence message = "Crazy About Android...";
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
      new Intent(), 0);
    Notification notif = new Notification(R.drawable.android_logo,
      "Crazy About Android...", System.currentTimeMillis());
    notif.setLatestEventInfo(context, from, message, contentIntent);
    nm.notify(1, notif);

}
}

何が欠けているのかわかりません。特定の権限または何かが必要ですか。どんな助けでも大歓迎です!

4

1 に答える 1

1

PendingIntent.getBroadcast(context, requestCode, intent, flags)代わりにgetServiceメソッドを使用する必要があると思います。

于 2012-11-09T13:02:28.220 に答える