即使我在Android中点击它,通知也不会消失

时间:2016-01-28 18:10:44

标签: android android-notifications android-broadcastreceiver

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

这是我的活动课程:

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());
    }
}

它显示noti罚款。但即使我点击它也永远不会消失。它仍然像截图一样。

Here is the documentation for defaultdict

我的代码出了什么问题?

0 个答案:

没有答案