状态栏中的BroadCast服务和通知

时间:2014-02-18 00:00:27

标签: android notifications

编辑:我将以不同的方式再次提出我的问题,因为我收到了任何回复。

我认为在Mainactivity中放置警报管理器(广播接收器由pendingIntent调用)会导致两个问题。首先,只要应用程序打开,它就会被调用然后显示通知。  其次,当应用程序关闭时,通知可能不会被调用,因为他们在主要活动中调用(或者因为他们是braodcast他们应该以任何方式运行!!!!) 如果是这样,问题是我还能把它放在哪里?

我将开发一个TODO列表作为我的应用程序中的一个功能。即使应用程序尚未运行,我也想通知用户,所以我使用Broad Cast服务,通过我的主要活动调用该服务,如下所示:

public void NOtifyMe( ArrayList<String> mArrayList)
{
        AlarmManager alarmMang = (AlarmManager) getSystemService(ALARM_SERVICE);

        // ---get current date and time---

        Calendar calender = Calendar.getInstance();
        calender.add(Calendar.SECOND, 20);

        Intent noteIntent = new Intent(MainActivity.this, BroadCast.class);
        noteIntent.putStringArrayListExtra("ArrayList", mArrayList);//.putExtra("Cursor",c/*"ID", id*/);
        PendingIntent pendI = PendingIntent.getBroadcast(MainActivity.this, 0,
                noteIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        // // SET ALARM

        alarmMang.set(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(),
                pendI);
        alarmMang.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),
                AlarmManager.INTERVAL_DAY, pendI);

这是我的Broad Cast服务代码:

public void onReceive(Context arg0, Intent arg1) {

    // TODO Auto-generated method stub
    ArrayList<String> array = arg1.getStringArrayListExtra("ArrayList");
    Intent intent = new Intent();
    intent.setClassName("com.Sali.todolist_final", "com.Sali.todolist_final.notifyService");
    //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putStringArrayListExtra("ArrayList", array);
    arg0.startService(intent);

}

正如您所看到的,我正在使用数组列表,因为如果有两个即将发生的任务,它没有显示两个通知。我试图通过调用函数来完成它,就像我在Cursor中记录一样。

这是notifyService中的代码:

    for (int i = 0; i < array.size(); i++) {

        id = array.get(i);// .getLong(inId.getColumnIndex("_id"));//
                            // Long.valueOf(inId);
        g = Integer.parseInt(id);
        idtopass = Long.valueOf(id);
        counter = 0;
        Intent mainIntent = new Intent(notifyService.this, TaskDetail.class);
        mainIntent.putExtra("id", idtopass);
        //mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mainIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pIntent = PendingIntent.getActivity(
                notifyService.this, g, mainIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder;
        NotificationManager notificationManager;

        builder = new NotificationCompat.Builder(notifyService.this)

        .setSmallIcon(R.drawable.ic_launcher).setContentTitle("TO_Do")
                .setContentText("You Have Something To Do." + id);
        builder.setAutoCancel(true);
        builder.setContentIntent(pIntent).setNumber(++counter);

        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        notificationManager.notify(g, builder.build());

    }

这里有两大问题: 第一:它没有显示没有app运行的通知 第二:当我设置正确的标志时,通过点击通知,它将再次出现。 第三个(不太重要的时间)我不喜欢两个有两个通知图标,以防有2个即将到来,但我喜欢通过打开窗口,用户可以看到两者。我知道这是因为我在for body中使用了Manager.notify,但在这种情况下我没办法。如果有人也有解决方案。 如果有人可以帮助我,我会表示赞赏。这真是个傻瓜。

0 个答案:

没有答案