Android Notification通知栏使用
1 package com.example.mynotifycation; 2 3 import android.app.Activity; 4 import android.app.Notification; 5 import android.app.NotificationManager; 6 import android.app.PendingIntent; 7 import android.app.Notification.Builder; 8 import android.content.Context; 9 import android.content.Intent; 10 import android.os.Bundle; 11 import android.view.View; 12 import android.widget.Button; 13 /** 14 * Notifycation 使用总结 15 * @Describe: 16 * @package: com.example.mynotifycation 17 * @author shaobn 18 * @date 2015-9-14 下午3:32:30 19 */ 20 public class MainActivity extends Activity { 21 private Button button; 22 private NotificationManager notificationManager; 23 private Notification.Builder builder; 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 button = (Button) this.findViewById(R.id.button1); 29 notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 30 builder = new Notification.Builder(MainActivity.this); 31 button.setOnClickListener(new View.OnClickListener() { 32 33 @Override 34 public void onClick(View arg0) { 35 // TODO Auto-generated method stub 36 Intent intent = new Intent(MainActivity.this,MainActivity.class); 37 //PendingIntent是Intent的一种封装,并不是立刻执行intent,只有在响应pendingIntent后才执行intent 38 PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 1); 39 builder.setContentIntent(pIntent); 40 builder.setContentTitle("notify"); 41 builder.setContentText("hello world");
//必须要有设置图片,否则出不来 42 builder.setSmallIcon(R.drawable.ic_launcher); 43 builder.setTicker("通知来了"); 44 // builder.setDefaults(Notification.DEFAULT_VIBRATE); 45 //停100ms,震动250ms,再停1000ms,再震动500ms,注意需要在配置文件中声明震动权限 android.permission.VIBRATE 46 long [] ll = {100, 250, 1000, 500}; 47 builder.setVibrate(ll); 48 Notification notification = builder.build(); 49 notificationManager.notify(100, notification); 50 } 51 }); 52 } 53 }
吾宁做一叶扁舟,始航于湖边,遨游于海上,浪迹于江中。