0

我是 Android 的绝对初学者。现在我正在学习如何将通知与广播接收器一起使用。要向用户显示通知,它工作正常。但问题是我点击后通知永远不会消失。我按时间间隔向用户显示通知。

这是我的活动课:

public class MainActivity extends AppCompatActivity {
    private DrawerLayout mDrawer;
    private NavigationView navigationView;
    private ImageButton openSidebarBtn,calendarBtn;
    private int CALENDAR_DIALOG_ID;
    private int year, month, day;
    private Fragment fragment;
    private String fTag;
    private RemainderReceiver remainderReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        remainderReceiver = new RemainderReceiver();
        remainderReceiver.startRemainder(this.getApplicationContext());

      .
      . 
      .
}

这是我的接收器类显示通知:

public class RemainderReceiver extends BroadcastReceiver {
    NotificationCompat.Builder notification;
    DatabaseHelper dbHelper;
    Context context;
    @Override
    public void onReceive(Context pContext, Intent intent) {
        context = pContext;
        dbHelper = new DatabaseHelper(context.getApplicationContext());
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TODO_REMAINDER");

        wl.acquire();
        ArrayList<Task> items = dbHelper.getToDoTasks();
        int taskCount = 0;
        if(items.size()>0)
        {
            for(int i=0;i<items.size();i++)
            {
                String dateStr = items.get(i).getDate();
                if(dateStr==null || dateStr.isEmpty())
                {
                    taskCount++;
                }
                else{
                    if(dateStr==CommonHelper.getCurrentDateStrInFormat("yyyy-mm-dd"))
                    {
                        taskCount++;
                    }
                }
            }
        }
        showNotification(String.valueOf(taskCount));
        wl.release();
    }

    public void startRemainder(Context context)
    {
        AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context,RemainderReceiver.class);

        PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
        long intervalMilliSec =12960000;//6 hours
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), intervalMilliSec, pi);
    }

    public void showNotification(String taskCount)
    {
        notification = new NotificationCompat.Builder(context);
        notification.setAutoCancel(true);
        notification.setSmallIcon(R.drawable.todo_app_icon);
        notification.setWhen(System.currentTimeMillis());
        notification.setTicker("You have "+taskCount+" to-do task(s) for today");
        notification.setContentTitle("To-do tasks");
        notification.setContentText("Please click here to view your to-do tasks");

        Intent i = new Intent(context.getApplicationContext(),MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(),0,i,PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setContentIntent(pendingIntent);

        NotificationManager nm = (NotificationManager)context.getApplicationContext().getSystemService(context.getApplicationContext().NOTIFICATION_SERVICE);
        nm.notify(1,notification.build());
    }
}

它显示不是很好。但即使我点击它,它也永远不会消失。它仍然像在屏幕截图中一样。

在此处输入图像描述

我的代码有什么问题?

4

0 回答 0