为Android应用程序创建定时通知

时间:2013-01-25 17:56:23

标签: android notifications

我使用eclipse创建android应用程序相对较新。我的问题是我在创建定时通知时遇到了麻烦。目前我创建了一个功能,该功能使用按钮在http://www.vogella.com/articles/AndroidNotifications/article.html

的帮助下创建通知

现在我想在代码中使用定时器而不是使用按钮。 请帮忙!这是我的按钮激活通知代码。

     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> 

创建了以下result.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" >

    <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> 

我发现了一个使用Alarm Manager Set notification to specific time的方法,我将这个代码添加到我的中心,以便它每天中午重复,但它似乎不起作用。

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

1 个答案:

答案 0 :(得分:-1)

你想看看AlarmManager。把它想象成你的Android应用程序的unix cron。简而言之,您计划在将来开火Intent。然后你写一个BroadcastReceiver来处理意图,并在这里做你想做的事。

当然,魔鬼的细节。密切注意诸如将任何耗时的工作从广播接收器卸载到线程,并确保您事先获得唤醒锁定。这些是高级主题,无法在SO答案中正确涵盖。