0

I'm relatively new at using eclipse for creating android application. My problem is that I'm having trouble creating a timed notification. At the moment i created a function that uses a button to create a notification with the help from http://www.vogella.com/articles/AndroidNotifications/article.html

Now i would like to use a timer set in the code instead of using a button. Please help! Here is my code for button activation notification.

     import android.app.Activity;
     import android.app.Notification;
     import android.app.NotificationManager;
     import android.app.PendingIntent;
     import android.content.Intent;
     import android.os.Bundle;
     import android.view.View;

     public class MainActivity extends Activity {

          @Override
          public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
          }

          public void createNotification(View view) {
            // Prepare intent which is triggered if the
            // notification is selected
            Intent intent = new Intent(this, MainActivity.class);
            PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

            // Build notification
            // Actions are just fake
            Notification noti = new Notification.Builder(this)
                .setContentTitle("Medication")
                .setContentText("Subject").setSmallIcon(R.drawable.original)
                .setContentIntent(pIntent)
               .build();
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            // Hide the notification after its selected
            noti.flags |= Notification.FLAG_AUTO_CANCEL;

            notificationManager.notify(0, noti);

             Intent myIntent = new Intent(this , MainActivty.class);     
       AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
       PendingIntent pIntent2 = PendingIntent.getService(ThisApp.this, 0, myIntent, 0);

       Calendar calendar = Calendar.getInstance();
           calendar.set(Calendar.HOUR_OF_DAY, 12);
       calendar.set(Calendar.MINUTE, 00);
       calendar.set(Calendar.SECOND, 00);

      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pIntent2);  //set repeating every 24 hours

          }
        } 

main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:onClick="createNotification"
        android:text="Create Notification" >
    </Button>

</LinearLayout> 

Created the following result.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the result activity opened from the notification" >
    </TextView>

</LinearLayout> 

I found a method which uses Alarm Manager Set notification to specific time, i added this code to mine so that it repeats everyday at noon, but it doesn't seem to work.

Intent myIntent = new Intent(this , MainActivty.class);     
       AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
       PendingIntent pIntent2 = PendingIntent.getService(ThisApp.this, 0, myIntent, 0);

       Calendar calendar = Calendar.getInstance();
           calendar.set(Calendar.HOUR_OF_DAY, 12);
       calendar.set(Calendar.MINUTE, 00);
       calendar.set(Calendar.SECOND, 00);

      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pIntent2);  //set repeating every 24 hours
Community
  • 1
  • 1

1 Answers1

-1

you want to look into AlarmManager. think of it like unix cron for your android app. in a nutshell, you schedule an Intent to fire in the future. You then write a BroadcastReceiver to handle the intent, and do whatever you want here.

of course, the devil's in the details. pay close attention to things like offloading any time-consuming work from your broadcast receiver to a thread, and ensuring that you grab a wake lock before hand. these are advanced topics that can't properly be covered in a SO answer.

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134