根据TimePicker对话框在特定时间设置通知

时间:2015-11-09 09:53:42

标签: android notifications dialog timepicker

以下是我的完整代码

public class set_tazkeer extends AppCompatActivity implements View.OnClickListener {

    Toolbar toolbar;
    Button btnS, btnM;
    Calendar mCalen,current;
    int hourOfDay, minute, ampm,timePickerInput;
    static final int Time_DIALOG_ID = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_tazkeer);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        btnS = (Button) findViewById(R.id.set_sabah);
        btnM = (Button) findViewById(R.id.set_masaa);

        mCalen = Calendar.getInstance();
        hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
        minute = mCalen.get(Calendar.MINUTE);
        ampm = mCalen.get(Calendar.AM_PM);


        btnS.setOnClickListener(this);
        btnM.setOnClickListener(this);
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {

        switch (id) {
            case Time_DIALOG_ID:
                return new TimePickerDialog(this, TimePickerListener, hourOfDay, minute, false);
        }
        return null;
    }

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


                // while dialog box is closed, below method is called.
                public void onTimeSet(TimePicker view, int hour, int minute) {
                    switch (timePickerInput) {
                        case R.id.set_sabah:

                            mCalen.set(Calendar.HOUR_OF_DAY, hour);
                            mCalen.set(Calendar.MINUTE, minute);

                            int hour12format = mCalen.get(Calendar.HOUR);
                            hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
                            minute = mCalen.get(Calendar.MINUTE);
                            ampm = mCalen.get(Calendar.AM_PM);
                            String ampmStr = (ampm == 0) ? "AM" : "PM";

                            // Set the Time String in Button
                            btnS.setText(hour12format + " : " + minute + " / " + ampmStr);
                            break;

                        case R.id.set_masaa:
                            mCalen.set(Calendar.HOUR_OF_DAY, hour);
                            mCalen.set(Calendar.MINUTE, minute);

                            int hour12format2 = mCalen.get(Calendar.HOUR);
                            hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
                            minute = mCalen.get(Calendar.MINUTE);
                            ampm = mCalen.get(Calendar.AM_PM);
                            String ampmStr2 = (ampm == 0) ? "AM" : "PM";
                            btnM.setText(hour12format2 + " : " + minute + " / " + ampmStr2);
                            break;

                    }
                    if (mCalen.compareTo(current) <= 0) {
                        //The set Date/Time already passed
                        Toast.makeText(getApplicationContext(),
                                "Invalid Date/Time",
                                Toast.LENGTH_LONG).show();
                    } else {
                        setAlarm(mCalen);
                    }
                }
            };

    private void setAlarm(Calendar targetmCalen) {
        Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, targetmCalen.getTimeInMillis(), pendingIntent);
    }


    @Override
    public void onClick(View v) {
        timePickerInput = v.getId();
        showDialog(Time_DIALOG_ID);

    }
}

和AlarmReceiver

public class AlarmReceiver extends BroadcastReceiver {

    private static final int MY_NOTIFICATION_ID = 0;
    NotificationManager notificationManager;
    Notification myNotification;

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

        Intent myIntent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                context,
                0,
                myIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        myNotification = new NotificationCompat.Builder(context)
                .setContentTitle("الاذكار")
                .setContentText("حان وقت الاذكار")
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .setSmallIcon(R.drawable.icon)
                .build();

        notificationManager =
                (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
    }
}

当我在TimePicker对话框中选择时间并按下确定&gt;时,我不知道错误在哪里。 &#34; Unfortunately the app has stopped&#34;

下面是logcat

11-09 12:02:00.894 28370-28370/mydesign.abdel_quddous.azkaralyoum E/AndroidRuntime: FATAL EXCEPTION: main
11-09 12:02:00.894 28370-28370/mydesign.abdel_quddous.azkaralyoum E/AndroidRuntime: Process: mydesign.abdel_quddous.azkaralyoum, PID: 28370
11-09 12:02:00.894 28370-28370/mydesign.abdel_quddous.azkaralyoum E/AndroidRuntime: java.lang.NullPointerException: anotherCalendar == null
11-09 12:02:00.894 28370-28370/mydesign.abdel_quddous.azkaralyoum E/AndroidRuntime:     at java.util.Calendar.compareTo(Calendar.java:1260)
11-09 12:02:00.894 28370-28370/mydesign.abdel_quddous.azkaralyoum E/AndroidRuntime:     at abdel_quddous.mydesign.azkaralyoum.set_tazkeer$2.onTimeSet(set_tazkeer.java:102)
11-09 12:02:00.894 28370-28370/mydesign.abdel_quddous.azkaralyoum E/AndroidRuntime:     at android.app.TimePickerDialog.onClick(TimePickerDialog.java:133)
11-09 12:02:00.894 28370-28370/mydesign.abdel_quddous.azkaralyoum E/AndroidRuntime:     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:162)
11-09 12:02:00.894 28370-28370/mydesign.abdel_quddous.azkaralyoum E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-09 12:02:00.894 28370-28370/mydesign.abdel_quddous.azkaralyoum E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:211)
11-09 12:02:00.894 28370-28370/mydesign.abdel_quddous.azkaralyoum E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5389)
11-09 12:02:00.894 28370-28370/mydesign.abdel_quddous.azkaralyoum E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-09 12:02:00.894 28370-28370/mydesign.abdel_quddous.azkaralyoum E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
11-09 12:02:00.894 28370-28370/mydesign.abdel_quddous.azkaralyoum E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
11-09 12:02:00.894 28370-28370/mydesign.abdel_quddous.azkaralyoum E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

0 个答案:

没有答案