我需要创建多个状态栏通知.当我拉下状态栏时,应该将多个通知图标显示为列表.每个通知图标都应显示下一页显示的单独数据.我怎么能这样做?
我的代码:
public class SimpleNotification extends Activity {
private NotificationManager mNotificationManager;
private int SIMPLE_NOTFICATION_ID;
String str="Hai";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
final Notification notifyDetails = new Notification(R.drawable.android,"New Alert, Click Me!",System.currentTimeMillis());
Button start = (Button)findViewById(R.id.notifyButton);
Button cancel = (Button)findViewById(R.id.cancelButton);
start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Context context = getApplicationContext();
CharSequence contentTitle = "Notification Details...";
CharSequence contentText = "Browse Android Official Site by clicking me"; …Run Code Online (Sandbox Code Playgroud) 我有一个相当简单的应用程序,它接受用户的输入,然后将其设置为通知.用户可以创建他/她喜欢的任意数量的通知.我希望用户单击该通知并进入一个名为的新活动ResultActivity.ResultActivity反过来putExtras从通知意图中读取并将其显示给用户.下面的代码允许我做几乎我想要的一切,除非按下通知,我收到putExtra最后创建的通知.
Intent notificationIntent = new Intent(ctx, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, i,notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = ctx.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(res,R.drawable.ic_launcher))
.setTicker("Remember to " + text.getText())
.setWhen(System.currentTimeMillis()).setAutoCancel(true)
.setContentTitle(text.getText());
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
String pass = text.getText().toString();
resultIntent.putExtra("title", pass);
resultIntent.putExtra("uid", i);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the …Run Code Online (Sandbox Code Playgroud) 我开发了一个应用程序,用户可以在其中创建事件并为该事件设置通知.所以我想添加多个通知.我使用以下代码.
final Notification notifyDetails = new Notification(R.drawable.icon, "Myapp",calendar.getTimeInMillis());
Context context = getApplicationContext();
Intent notifyIntent = new Intent(context, ViewDoughnut.class);
PendingIntent pendingIntent = PendingIntent.getActivity(ViewCal.this, 0, notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);
notifyDetails.flags = Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
Run Code Online (Sandbox Code Playgroud)
当我使用上面的代码添加事件并创建通知时,它工作正常.但是如果我添加另一个事件,没有创建新通知,旧的通知就会更新.我想再添加一个通知.怎么做?此外,如果用户删除与其对应的事件,我想删除任何特定通知.怎么可能?
public static void showNotification(Context ctx, int value1, String title, String message, int value2){
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(ctx, ActivityMain.class);
int not_id = Utils.randInt(1111, 9999);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationIntent.putExtra("key_1", value1);
notificationIntent.putExtra("key_2", value2);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx.getApplicationContext());
stackBuilder.addNextIntent(notificationIntent);
PendingIntent notifPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
notificationManager.notify(not_id, createNotification(ctx, notifPendingIntent, title, message));
}
Run Code Online (Sandbox Code Playgroud)
这是我发送和显示notification. 我从服务发送这个。正如你所看到的,我发送了 2 个额外的值,目的是(key_1和key_2);当我单击通知时,我应该打开活动并查看按键的value1和。value2
问题是:如果在我打开其中任何一个之前收到另一个通知,value1并且value2该通知被覆盖。例如:
第一个通知发送foo并且bar
第二个通知发送faz并且baz
第三次通知发送fubar和fobaz …