1

我正在调用 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 方法没有调用请任何人帮助我

4

1 回答 1

10

我对您的代码不了解更多,但我认为您希望根据从 TimePickerDialog 获得的时间获取每日通知,所以我的建议是这个示例将对您有所帮助

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

于 2013-09-28T07:08:44.107 回答