如何在特定时间设置通知

时间:2014-01-03 07:37:07

标签: android service notifications sqlite

我一直在搜索通知直到早上。我在内容提供商处保存日期和时间。 当我保留alaram经理的时间和时间。

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.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
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();
    }
};

TimePickerDialog.OnTimeSetListener time = new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        final Intent chartNotifier = new Intent(Addevent.this,
                MyReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                Addevent.this, 234324243, chartNotifier,
                PendingIntent.FLAG_UPDATE_CURRENT);
        final Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, hourOfDay);
        c.set(Calendar.MINUTE, minute);
        long when = c.getTimeInMillis();

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, when, pendingIntent);

    } // DailyNotification dn = new DailyNotification(Addevent.this,
        // hourOfDay, minute);
    // dn.registerAlarm(Addevent.this, hourOfDay, minute);
    /*
     * Intent myIntent = new Intent(Addevent.this, MyReceiver.class);
     * pendingIntent = PendingIntent.getBroadcast(Addevent.this, 0,
     * myIntent,0);
     * 
     * AlarmManager alarmManager =
     * (AlarmManager)getSystemService(ALARM_SERVICE);
     * alarmManager.set(AlarmManager.RTC,c.getTimeInMillis(),
     * pendingIntent);
     * 
     * setCurrentDateOnView();
     */

};
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();

    /*
     * Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
     * PendingIntent pendingIntent =
     * PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
     * AlarmManager alarmManager =
     * (AlarmManager)getSystemService(Context.ALARM_SERVICE);
     * alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
     * pendingIntent);
     */

}

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());

    EditText name = (EditText) findViewById(R.id.txtName);
    String name1 = name.getText().toString();

    EditText eventname = (EditText) findViewById(R.id.txtGrade);
    String eventname1 = name.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

}
}

请检查我没有收到通知。如果用户提供日期和时间,请在特定日期和时间收到通知。

1 个答案:

答案 0 :(得分:0)

private void setAlarm(Date targetCal, boolean isEditMode) {

        Intent myIntent = new Intent(AddEventActivity.this, MyReceiver.class);
        myIntent.putExtra("MODE", isEditMode);
        myIntent.putExtra("time", targetCal.getTime());
        myIntent.putExtra("title", mTitle);
        pendingIntent = PendingIntent.getBroadcast(AddEventActivity.this, 0, myIntent, 0);

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