之前我用了button.setonclicklistener来获取一个点击事件,但是在new notificationcompat.builder是会报一个没有定义的错误。这种点击事件的方式就不会报那种错误了。
public class MainActivity extends Activity implements View.OnClickListener { private Button sendnotice; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); sendnotice = (Button) findViewById(id.send_notice);
sendnotice.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.send_notice:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("锋少来通知了") //设置标题
.setContentText("去哪里玩啊") //内容
.setWhen(System.currentTimeMillis()) //时间
.setSmallIcon(R.drawable.ic_launcher) //小图片
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)) //大图片
.build();
manager.notify(,notification);
break; }
}
}