我有这个应用程序,您可以按下按钮触发通知。问题是,如果你多按一次,通知会在你点击按钮后频繁出现。是否有任何简单的代码行来避免这种情况?是什么东西可以自动取消旧通知,所以始终只有一个通知?
预先感谢!
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void sendNotification(View view) {
switch(view.getId()){
case R.id.buttom1:
Notification1();
break;
case R.id.buttom2:
Notification2();
break;
}
}
private void Notification1() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setAutoCancel(true);
builder.setContentTitle("BasicNotification");
builder.setContentText("Test");
builder.setSmallIcon(R.drawable.icon1);
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
manager.notify((int) System.currentTimeMillis(), notification);
}
private void Notification2() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setAutoCancel(true);
builder.setContentTitle("BasicNotification");
builder.setContentText("Test");
builder.setSmallIcon(R.drawable.icon2);
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
manager.notify((int) System.currentTimeMillis(), notification);
}
}
答案 0 :(得分:0)
你试过这个吗?
Button btn = (Button) findViewById(R.id.button1);
btn.setEnabled(false);
更新
声明一个全局变量:
int clickcount=0;
将此信息放入执行Action onClick的方法:
if(clickcount==2)
{
buttom1.setEnabled(false);
}
答案 1 :(得分:0)
为每种通知类型定义一个全局变量:
int notofication1Id = 0;
当您在整数变量
中推送新通知存储ID时manager.Cancel(notification1Id);
int id = (int) System.currentTimeMillis();
manager.notify(id, notification);
notification1Id = id;