如何获取特定日期和时间的通知?

时间:2014-01-03 09:57:07

标签: android notifications android-contentprovider

您好我正在搜索当我选择日期和时间时提供通知的日期和时间,当日期和事件发生在该日期通知想要来的时候。

package com.example.addeventsofcal;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;


import com.examples.android.calendar.CalendarView;
import com.examples.android.calendar.R;


public class Addevent extends Activity implements OnClickListener {

    private EditText edtDate;
    private EditText edtTime;

    final Calendar c = Calendar.getInstance();


    final static int RQS_1 = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contentprovider);

        initializeView();
    }

    private void initializeView() {
        edtDate = (EditText) findViewById(R.id.txtDate);
        edtTime = (EditText) findViewById(R.id.txtTime);
        Intent currentIntent = getIntent();
        Bundle extras = currentIntent.getExtras();

        setCurrentDateOnView();
    }

    DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            final Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, monthOfYear);
            c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            // setCurrentDateOnView();
        }
    };

    protected DatePickerDialog Date;

    TimePickerDialog.OnTimeSetListener time = new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

            final Calendar c = Calendar.getInstance();
            c.set(Calendar.HOUR_OF_DAY, hourOfDay);
            c.set(Calendar.MINUTE, minute);
            Date = new DatePickerDialog(Addevent.this, date, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
            setAlarm(Date, true);
        }

        private void setAlarm(Date targetCal, boolean isEditMode) {
            Intent myIntent = new Intent(Addevent.this, MyReceiver.class);
            myIntent.putExtra("MODE", isEditMode);
            myIntent.putExtra("time", targetCal.getTime());

            pendingIntent = PendingIntent.getBroadcast(Addevent.this, 0, myIntent, 0);

            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC, targetCal.getTime(), pendingIntent);
        }
    };

    private PendingIntent pendingIntent;

    public void dateOnClick(View view) {
        new DatePickerDialog(Addevent.this, date,
                c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show();

    }

    public void timeOnClick(View view) {
        new TimePickerDialog(Addevent.this, time,
                c.get(Calendar.HOUR), c.get(Calendar.MINUTE), false).show();
    }

    public void setCurrentDateOnView() {
        String dateFormat = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.US);
        edtDate.setText(sdf.format(c.getTime()));

        String timeFormat = "hh:mm a";
        SimpleDateFormat stf = new SimpleDateFormat(timeFormat, Locale.US);
        edtTime.setText(stf.format(c.getTime()));
    }

    public void onClickAddName(View view) {
        // Add a new student record
        ContentValues values = new ContentValues();

        values.put(StudentsProviders.NAME,
                ((EditText) findViewById(R.id.txtName)).getText().toString());

        values.put(StudentsProviders.Description,
                ((EditText) findViewById(R.id.txtGrade)).getText().toString());


        values.put(StudentsProviders.DATE,
                ((EditText) findViewById(R.id.txtDate)).getText().toString());


        values.put(StudentsProviders.Time,
                ((EditText) findViewById(R.id.txtTime)).getText().toString());  

        Uri uri = getContentResolver().insert(StudentsProviders.CONTENT_URI, values);
        Toast.makeText(getBaseContext(), uri.toString(), Toast.LENGTH_LONG).show();  
        Intent returnIntent = new Intent(Addevent.this, CalendarView.class);
        startActivity(returnIntent);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub      
    }
}

请告诉我如何在我们的时间选择监听器时收到通知。当我添加set alaram时,我收到错误日期,当我在set alaram中转换日期时无法转换。请建议如何解决。

1 个答案:

答案 0 :(得分:1)

  

请使用以下书面方法

     

注意:TimeAlarm是广播接收器

private void setNotification(String dateTimeStr,String id)
        {


            SimpleDateFormat formatToCompare = new SimpleDateFormat(
                    "MM/dd/yyyy hh:mm");

            Date dateNotification = null;

                try {
                    dateNotification = formatToCompare
                            .parse(dateTimeStr);
                } catch (java.text.ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            Intent intent = null;
            intent = new Intent(getApplicationContext(), TimeAlarm.class);
            intent.putExtra("NOTIFICATION", "Your text.");
            intent.putExtra("ID", Integer.parseInt(id));
            intent.putExtra("LONG", dateNotification.getTime());

            PendingIntent sender = PendingIntent.getBroadcast(
                    getApplicationContext(),Integer.parseInt(id),
                    intent, 0);



                AlarmManager am = null;
                am = (AlarmManager) getSystemService(ALARM_SERVICE);

                // am.setRepeating(AlarmManager.RTC_WAKEUP,
                // dateNotification.getTime(), AlarmManager.INTERVAL_DAY,
                // sender);

                am.set(AlarmManager.RTC_WAKEUP, dateNotification.getTime(),
                        sender);



        }

公共类TimeAlarm扩展了BroadcastReceiver {

Calendar cal=Calendar.getInstance();


@Override
public void onReceive(Context context, Intent intent) {
    NotificationManager nm=null;

     nm = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);


              PendingIntent contentIntent = PendingIntent.getActivity(context,(int) intent.getExtras().getInt("ID"),
                      intent, 0);


              Notification notif = new Notification(R.drawable.app_icon,
                    "Your Title",System.currentTimeMillis());

              notif.defaults |= Notification.DEFAULT_SOUND;
                notif.flags |= Notification.FLAG_AUTO_CANCEL;
              notif.setLatestEventInfo(context, "ToroRide Reservation",intent.getExtras().getString("NOTIFICATION"), contentIntent);
        notif.when=intent.getExtras().getLong("LONG");

              nm.notify(intent.getExtras().getInt("ID"), notif);


        //  context.startActivity(new Intent(context, ReservationListing.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    }        

}

  

还在清单中添加:

<receiver android:name="com.torride.broadcast.TimeAlarm"></receiver>