Notification(通知栏)

来源:互联网 发布:什么是双色球密码算法 编辑:程序博客网 时间:2024/05/16 19:09
public class MainActivity extends ActionBarActivity implements OnClickListener{NotificationManager manager;//通知控制类int notification_ID;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.bt1).setOnClickListener(this);findViewById(R.id.bt2).setOnClickListener(this);manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch(v.getId()){case R.id.bt1://发送通知按钮sendNotification();break;case R.id.bt2:manager.cancel(notification_ID);break;}}//构造notification并发送到通知栏private void sendNotification() {// TODO Auto-generated method stub//创建点击后的意图Intent intent = new Intent(this,MainActivity.class);PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, 0);Builder builder = new Notification.Builder(this);//创建一个Builder类builder.setSmallIcon(R.drawable.ic_launcher);//设置图标builder.setTicker("hello");//手机状态栏的提示;builder.setWhen(System.currentTimeMillis());//设置时间builder.setContentTitle("通知栏通知");//设置标题builder.setContentText("我来自NotificationDemo");//设置通知内容builder.setContentIntent(pintent);//点击后的意图//builder.setDefaults(Notification.DEFAULT_SOUND);//设置提示声音//builder.setDefaults(Notification.DEFAULT_LIGHTS);//设置指示灯//builder.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动builder.setDefaults(Notification.DEFAULT_ALL);//设置震动Notification notification = builder.build();//4.1以上//builder.getNotification();manager.notify(notification_ID, notification); }}

0 0