如何计算Android中收到的通知

时间:2015-03-25 05:55:51

标签: android notifications

我正在开发一个应用程序,它会计算通知栏中收到的通知。直到现在我完成了按钮点击创建多个通知,它显示不同的通知。我想要的是它在单个通知时显示setText,当它是多个时它会在setText中显示它n。

我努力在Stackoverflow和其他Android网站上找到这个,但没找到我要找的东西。任何帮助都会非常支持。

如果有人无法理解我的问题,请告诉我。

以下是代码:

Button noti;
int counter = 0;
boolean ncompi = false;
//  private static int pendingNotificationsCount = 0;

NotificationManager nManager;
NotificationCompat.Builder ncomp;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.imageclass);

SharedPreferences sharedpreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putInt("count", count);
editor.putInt("counter", counter);
editor.commit();


SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
int i = sharedPreferences.getInt("count", 0);
int j = sharedPreferences.getInt("counter", 0);


  Button increaseButton = (Button) findViewById(R.id.btn1);
    increaseButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub


            nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                ncomp = new NotificationCompat.Builder(ImageClass.this);
                ncomp.setContentTitle("9999900000");
                if(counter == 0){
                    ncomp.setContentText(ss);
                    counter++;
                   // ncomp.setContentText(ss);
                    Intent resultIntent = new Intent(ImageClass.this, MainActivity.class);

                //    resultIntent.putExtra("msg", ss);
                    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 1, resultIntent, 0);

                    ncomp.setContentIntent(pi);
                    ncomp.setTicker("Notification Listener");
                    ncomp.setSmallIcon(R.drawable.ic_launcher);

                    ncomp.setAutoCancel(true);

                    nManager.notify(NOTIFY_ID, ncomp.build());
                }
                    else{
                         ncomp.setContentTitle("Notification");
                        ncomp.setContentText(counter + " Notifications");
                        Intent resultIntent = new Intent(ImageClass.this, SmsActivity.class);

                      //  resultIntent.putExtra("message", ss);
                        PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 1, resultIntent, 0);

                        ncomp.setContentIntent(pi);
                    ncomp.setTicker("Notification Listener");
                    ncomp.setSmallIcon(R.drawable.ic_launcher);
                    ncomp.setAutoCancel(true);
                    nManager.notify(NOTIFY_ID, ncomp.build());

                    counter++;
}

以及如何区分通知活动中的已读和未读通知?

  ArrayList<MessageData> arrayList = new ArrayList<MessageData>();

    CustomListAdapter adapter = new CustomListAdapter(this, arrayList);
    smsListView.setAdapter(adapter);

    MessageData data1 = new MessageData();
    data1.setMessage("");
    data1.setRead(false);

这里创建的CustomListAdapter是代码。

public class CustomListAdapter extends BaseAdapter {

private Context context;
private ArrayList<MessageData> dataList;

public CustomListAdapter(Context context, ArrayList<MessageData> arr){
      super();
        this.context = context;
        this.dataList = arr;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
     return dataList.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return dataList.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}


@Override
public View getView(int position, View view, ViewGroup arg2) {
    // TODO Auto-generated method stub
     if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.listitem_spinner, null);

        }

        TextView textView = (TextView) view.findViewById(R.id.txt);

        textView.setText("" + dataList.get(position).getMessage());

        // here you can get Read/Unread status for all message
        boolean isRead = dataList.get(position).isRead();

        if (isRead) {
            textView.setTextColor(Color.GRAY);
        } else {
            textView.setTextColor(Color.BLACK);
        }

        return view;
}

}

1 个答案:

答案 0 :(得分:1)

您需要查看Managing Notifications,因为它解释了如何更新通知。 这是你可能想要的:

公共变量

final int NOTIFY_ID=1; // any integer number
int count = 0;

点击事件:

    NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder ncomp = new NotificationCompat.Builder(MainActivity.this);
        ncomp.setContentTitle("My Notification");
        if (count == 0)
            ncomp.setContentText("Notification");
        else
            ncomp.setContentText(count + " Notifications");
        Intent resultIntent = new Intent(MainActivity.this, Message.class);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        ncomp.setContentIntent(resultPendingIntent);
        ncomp.setTicker("Notification Listener");
        ncomp.setSmallIcon(R.drawable.ic_launcher);
        ncomp.setAutoCancel(true);
        nManager.notify(NOTIFY_ID, ncomp.build());

        count++;

希望这有帮助!

编辑: 要存储计数,请参阅Shared Preferences