之前我用了button.setonclicklistener来获取一个点击事件,但是在new notificationcompat.builder是会报一个没有定义的错误。这种点击事件的方式就不会报那种错误了。
1 public class MainActivity extends Activity implements View.OnClickListener { 2 3 4 private Button sendnotice; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 11 sendnotice = (Button) findViewById(id.send_notice); 12 sendnotice.setOnClickListener(this); 13 } 14 15 @Override 16 public void onClick(View v) { 17 switch (v.getId()) { 18 case R.id.send_notice: 19 NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 20 Notification notification = new NotificationCompat.Builder(this) 21 .setContentTitle("锋少来通知了") //设置标题 22 .setContentText("去哪里玩啊") //内容 23 .setWhen(System.currentTimeMillis()) //时间 24 .setSmallIcon(R.drawable.ic_launcher) //小图片 25 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)) //大图片 26 .build(); 27 manager.notify(1,notification); 28 break; 29 30 } 31 } 32 }