如何计算 Android 中收到的通知
How to count received Notifications in Android
我正在开发一个应用程序,它将计算通知栏中收到的通知。到现在为止,我已经完成了在按钮单击时创建多个通知并显示不同的通知。我想要的是它在单个通知时显示 setText,当它是多个通知时,它将在 setText 中显示 n 计数。
我努力在 Whosebug 和其他 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++;
}
以及如何区分通知中的已读和未读通知Activity?
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 n 这里是代码。
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;
}
}
您需要查看 Managing Notifications,因为它解释了如何更新通知。
以下是您可能想要的:
public 变量
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
我正在开发一个应用程序,它将计算通知栏中收到的通知。到现在为止,我已经完成了在按钮单击时创建多个通知并显示不同的通知。我想要的是它在单个通知时显示 setText,当它是多个通知时,它将在 setText 中显示 n 计数。
我努力在 Whosebug 和其他 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++;
}
以及如何区分通知中的已读和未读通知Activity?
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 n 这里是代码。
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;
}
}
您需要查看 Managing Notifications,因为它解释了如何更新通知。 以下是您可能想要的:
public 变量
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