提醒通知

时间:2014-05-05 12:01:19

标签: android

这是特定日期和时间的通话提醒代码。问题是,一旦用户点击提交按钮,就会显示提醒通知。可能是bcoz通知是在系统的时间(模拟器或电话时间)显示的,而不是用户选择的时间和日期。有可能放置" timePicked = c.getTimeInMillis();"或onDateSet()或onTimeSet()方法中的问题。任何建议?这对我来说非常重要。

MainActivity.java

public class MainActivity extends Activity {
    public static EditText text;
    public static EditText text2;
    //public static int hour,minute,month,day,year;
public static long timePicked = -1;

static Calendar c;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (EditText)findViewById(R.id.editText1);
        text2 = (EditText)findViewById(R.id.editText2);
        final Button btn = (Button)findViewById(R.id.button3);
        c= new GregorianCalendar();

        btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                timePicked = c.getTimeInMillis();
                Log.d("test", "timePicked: " + timePicked);
                Log.d("test", "current: " + System.currentTimeMillis() );
                if (timePicked != -1) {
                    MyBroadcastReceiver alarm = new MyBroadcastReceiver();
                    if(alarm != null) alarm.setOnetimeTimer(MainActivity.this,timePicked);

                    btn.setText("ALARM SET");
                    Log.d("test", "ALARM SET!!!");
                } else {
                    //warn the user for ALARM not set?
                    Log.w("test", "ALARM not SET!!!");
                }
            }
        });

    }






public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
//final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // Do something with the time chosen by the user
         //Calendar c = new GregorianCalendar();
            c.set(Calendar.HOUR_OF_DAY, hourOfDay);
            c.set(Calendar.MINUTE, minute);

         //timePicked = c.getTimeInMillis();
           // Log.d("test", "timePicked: " + timePicked);
            //Log.d("test", "current: " + System.currentTimeMillis() );
    }

}

public void showTimePickerDialog(View v) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getFragmentManager(),"timePicker");
}
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
//final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
//long millis=c.getTimeInMillis(); 
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}

public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
    //Calendar c = new GregorianCalendar();
    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DAY_OF_MONTH, month);
    //timePicked = c.getTimeInMillis();
    //Log.d("test", "timePicked: " + timePicked);
    //Log.d("test", "current: " + System.currentTimeMillis() );
}
}
public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(),"datePicker");
}




}

MyBroadcastReceiver.java

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "test");

        //Acquire the lock
        wl.acquire();

        Toast.makeText(context, "Call"+MainActivity.text.getText()+MainActivity.text2.getText(), Toast.LENGTH_LONG).show();
        Log.i("test", "ALARM!!!");
        showNotification(context);
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(2000);

        //Release the lock

        wl.release();
    }
    private void showNotification(Context context) {
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, MainActivity.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("My notification")
                .setContentText("Call"+MainActivity.text.getText()+MainActivity.text2.getText());
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());
    }
    public void setOnetimeTimer(Context context, long when) {
        AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, MyBroadcastReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
        am.set(AlarmManager.RTC_WAKEUP, when, pi);
    }
}

1 个答案:

答案 0 :(得分:0)

从timepicker对话框中选择时间时设置闹钟。

使用

更新按钮点击代码
 btn.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) 
{

 DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(),"datePicker");


                }
            });

在选择时间时开启

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    String am_pm = "";

   c= Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
    c.set(Calendar.MINUTE, minute);

               timePicked = c.getTimeInMillis();
                    Log.d("test", "timePicked: " + timePicked);
                    Log.d("test", "current: " + System.currentTimeMillis() );
                    if (timePicked != -1) {
                        MyBroadcastReceiver alarm = new MyBroadcastReceiver();
                        if(alarm != null) alarm.setOnetimeTimer(MainActivity.this,timePicked);

                        btn.setText("ALARM SET");
                        Log.d("test", "ALARM SET!!!");
                    } else {
                        //warn the user for ALARM not set?
                        Log.w("test", "ALARM not SET!!!");
                    }
        }

您也可以取消之前设置的闹钟。