基于listview中的时间项的通知

时间:2014-12-02 12:58:19

标签: android push-notification

我有一个对象的arraylist,其成员是日期/时间。 arraylist和object用于填充UI列表视图。

当object / listview的时间接近系统/移动时间时(无论是5分钟,10分钟还是可修改的,无关紧要),我想发送推送通知。

所以我熟悉Notification Builder,但我不确定在哪里放置逻辑以及应用程序将如何监控时间并在关闭时通知。

任何有关正确道路的想法和建议都非常感激

2 个答案:

答案 0 :(得分:0)

您可以在预定时间触发警报,然后在警报响起时使用IntentService捕获警报。

在主要活动中(假设名称为MainActivity,您可以创建这样的警报。

    AlarmManager mgr = (AlarmManager) MainActivity.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(MainActivity, NotifService.class);
    PendingIntent pi = PendingIntent.getService(MainActivity, 0, intent, 0);
    mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + PERIOD, pi);
    where the PERIOD defines after how much milliseconds you want the alarm to go off.

要以不同方式配置AlarmManager,您可以阅读更多相关信息。

NotifService可以在闹钟响起时捕获它。创建一个名为NotifService的类,它扩展了IntentService

    public class NotifService extends IntentService {

    public NotifService() {
      super("My service");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
         //write your code for sending Notification
         Intent intent = new Intent(); 
         intent.setAction("com.xxx.yyy.youractivitytobecalled");
         PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, intent, 0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder (getApplicationContext());
        builder.setContentTitle("Your Application Title");
        builder.setContentText("Notification Content");
        builder.setContentIntent(pendingIntent);
        builder.setTicker("Your content");
        builder.setSmallIcon(R.drawable.ic_launcher);
        builder.setOngoing(false);
        builder.setPriority(0);

        Notification notification = builder.build();
        NotificationManager notificationManger = (NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE);
        notificationManger.notify(1, notification);

    }
    }

在AndroidManifest.xml中注册NotifService

    <application
             fill content here />

    <service android:name=".NotifService" >
    </service>

    </application>

答案 1 :(得分:0)

为了将来参考,我创建了一个扩展BroardcastReceiver的类,并使用它来处理来自AlarmManager的警报 - 它在文档中的所有内容,但它有点令人困惑的是一切都在一起。

相关问题