通过设置时间android每日本地通知

时间:2013-09-27 17:56:44

标签: android notifications broadcastreceiver alarmmanager android-notifications

我正在调用TimePickerDialog并根据用户设置的时间,根据用户每天获取状态栏上的通知的时间

我通过编写下面的代码来完成,

首先,我通过以下代码调用对话框

          @Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case TIME_DIALOG_ID:
        // set time picker as current time
        return new TimePickerDialog(this, timePickerListener, hour, minute,
                true);

    }
    return null;
}

监听方法

     private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minutes) {
        System.out.println("TimePicker: OnTimeSetLister: hour is "+hourOfDay+": mins"+minutes);
        DailyNotification.registerAlarm(SubscribeMsgTime.this, hourOfDay, minutes);
    }

};

我在AndroidMenifiast.xml文件中使用了BroadCastRecevier

        <receiver
        android:name="com.example.easynotification.DailyNotification"
        android:process=":remote" >
        <intent-filter>
            <action android:name="com.example.easynotification.DAILY_NOTIFICATION" />
            <action android:name="android.intent.action.EXTERNAL_APPLICATIONS_AVAILABLE" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

最后一个DailyNotification(BroadcastReceiver)类位于

之下
public class DailyNotification extends BroadcastReceiver {



public static void registerAlarm(Context paramContext, int mHour, int mMin) {
    Calendar calendar = Calendar.getInstance();
    if (calendar.get(Calendar.HOUR_OF_DAY) >= mHour) {
        calendar.add(Calendar.DATE, 1);
    }
    calendar.set(Calendar.HOUR_OF_DAY, mHour);
    calendar.set(Calendar.MINUTE, mMin);
    calendar.set(Calendar.SECOND, 00);


    PendingIntent localPendingIntent = PendingIntent.getBroadcast(
            paramContext, 22341, new Intent(
                    "com.example.easynotification.DAILY_NOTIFICATION"),
            PendingIntent.FLAG_UPDATE_CURRENT);

    ((AlarmManager) paramContext.getSystemService("alarm")).setRepeating(1,
            calendar.getTimeInMillis(), 24 * 60 * 60 * 1000,
            localPendingIntent);
    System.out.println("Alarm Registered on Hour :- "+mHour+" : Min "+mMin);
}

private void showNotification(Context paramContext) {
    try {
        Notification localNotification = new Notification(R.drawable.ic_launcher,
                paramContext.getString(R.string.app_name),
                System.currentTimeMillis());
        localNotification.flags = (0x10 | localNotification.flags);
        // PendingIntent that will start the EnterWeightActivity

        Intent intent = new Intent(paramContext, MainActivity.class);
        PendingIntent localPendingIntent = PendingIntent.getActivity(
                paramContext, 0, intent, 0);

        localNotification.setLatestEventInfo(paramContext, "hi", "Amit",
                localPendingIntent);
        // Retrieve a NotificationManager to show the notification
        ((NotificationManager) paramContext
                .getSystemService("notification")).notify(0,
                localNotification);

    } catch (Exception e) {
        e.printStackTrace();
    }
}
@Override
public void onReceive(Context paramContext, Intent paramIntent) {
    if (paramIntent.getAction().equals(
            "com.example.easynotification.DAILY_NOTIFICATION"))
        showNotification(paramContext);
}

}

但所有工作正常的对话框是开放时间正在注册但通知或onReceive方法没有调用请任何人帮助我

1 个答案:

答案 0 :(得分:10)

我不太了解您的代码,但我想您希望根据TimePickerDialog获取的时间获取每日通知,因此我的建议是此示例将帮助您

http://karanbalkar.com/2013/07/tutorial-41-using-alarmmanager-and-broadcastreceiver-in-android/