0

我在使用 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> 

我找到了一种使用警报管理器将通知设置到特定时间的方法,我将此代码添加到我的代码中,以便它每天中午重复,但它似乎不起作用。

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
4

1 回答 1

-1

你想调查一下AlarmManager。把它想象成你的 android 应用程序的 unix cron。简而言之,您计划Intent在将来触发。然后你写一个BroadcastReceiver来处理意图,并在这里做任何你想做的事情。

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

于 2013-01-25T18:04:04.787 回答