mercredi 22 juillet 2015

Generating notification by matching date from Sqlite database

I am trying to match the data which is stored in database. Then I want to generate notification on that date. I wrote the program but it is not generating notification .In MainActivity.java I am storing the value in database. In Notification.java I am generating alarm using Alarm Manager. In DisplayNotification.java I am building notification using Notification Builder. I am storing the date in the form of text. Plz help...

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

Aucun commentaire:

Enregistrer un commentaire