我正在尝试匹配存储在数据库中的数据。然后我想在该日期生成通知。我编写了程序但它没有生成通知。在MainActivity.java中我将值存储在数据库中。在Notification.java中,我使用Alarm Manager生成警报。在DisplayNotification.java中,我使用Notification Builder构建通知。我以文本的形式存储日期。 Plz帮助...
MainActivity.java
//I am using it for storing the date value in database
public class MainActivity extends Activity {
EditText etdate;
Button add;
ImageButton imageButton;
DateDB handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etdate=(EditText)findViewById(R.id.etdate);
imageButton=(ImageButton)findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar c = Calendar.getInstance();
int year, month, date;
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
date = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd=new DatePickerDialog (MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
etdate.setText(dayOfMonth+"-"+monthOfYear+1+"-"+year);
}
},year,month,date);
dpd.show();
}
});
//Add Contents to database
add=(Button)findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String getdate=etdate.getText().toString();
handler=new DateDB(getBaseContext());
handler.open();
long id=handler.insertData(getdate);
Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG).show();
handler.close();
//---PendingIntent from MainActivity to notification.java
Intent intent=new Intent(MainActivity.this,Notification.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent displayIntent = PendingIntent.getActivity(
getBaseContext(), 0,intent, 0);
}
});
}
Notifacation.java
In this activity I am trying to fetch the date from database and set alarm for the date if it is matching with the current date
public class Notification extends Activity {
DateDB handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
//Get current date and time
Calendar cal = Calendar.getInstance();
int dayc = cal.get(Calendar.DAY_OF_MONTH);
int yearc = cal.get(Calendar.YEAR);
int monthc = cal.get(Calendar.MONTH);
handler = new DateDB(getBaseContext());
handler.open();
Cursor c = handler.returnData();
StringBuffer buffer = new StringBuffer();
while (c.moveToNext()) {
String date[] = c.getString(0).split("-");
int year = Integer.parseInt(date[0]);
int month = Integer.parseInt(date[1]);
int day = Integer.parseInt(date[2]);
if (year == yearc && day == dayc && month == monthc)
//Function to call alarm
noti(c.getString(0));
}
handler.close();
}
//generate notification function
public void noti(String s)
{
String date[]=s.split("-");
int year=Integer.parseInt(date[0]);
int month=Integer.parseInt(date[1]);
int day=Integer.parseInt(date[2]);
//---use the AlarmManager to trigger an alarm---
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//---get current date and time---
Calendar calendar = Calendar.getInstance();
//---sets the time for the alarm to trigger---
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY,0);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND, 0);
//---PendingIntent to launch activity when the alarm triggers-
Intent intent=new Intent(Notification.this,DisplayNotification.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//---assign an ID of 1---
intent.putExtra("NotifID", 1);
PendingIntent displayIntent = PendingIntent.getActivity(
getBaseContext(), 0,intent, 0);
//---sets the alarm to trigger---
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), displayIntent);
}
DisplayNotification.java
In this activity I am creating notification using NotificationBuilder
public class DisplayNotification extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_notification);
//---get the notification ID for the notification;
// passed in by the MainActivity---
int notifID = getIntent().getExtras().getInt("NotifID");
//---PendingIntent to launch activity if the user selects
// the notification---
Intent i = new Intent(DisplayNotification.this,AlarmDetails.class);
i.putExtra("NotifID", notifID);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent detailsIntent =
PendingIntent.getActivity(this, 0, i, 0);
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
CharSequence from = "AlarmManager - Time's up!";
CharSequence message = "This is your alert, courtesy of the AlarmManager";
NotificationCompat.Builder builder=new NotificationCompat.Builder (DisplayNotification.this);
builder.setContentTitle(from);
builder.setSmallIcon(R.drawable.cal);
builder.setContentText(message);
builder.setTicker("Alert");
builder.setContentIntent(detailsIntent);
builder.setAutoCancel(true);
android.app.Notification noti=builder.build();
//---100ms delay, vibrate for 250ms, pause for 100 ms and
// then vibrate for 500ms---
// noti.vibrate = new long[] { 100, 250, 100, 500};
try{ nm.notify(1234, noti);}
catch (Exception ex){}
//---destroy the activity---
finish();
}