25,//Notification,控制顶部状态栏的通知,不会自动消失,需要手动清除,
//在notificationxml.xml文件中添加7个按钮
View Code
1 public class SubNotificationActivity extends Activity { 2 private static int NOTIFICATIONS_ID = R.layout.activity_notification; 3 private NotificationManager mNotificationManager; 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_notification); 8 Button button; 9 mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 10 button = (Button)findViewById(R.id.sun_1); 11 button.setOnClickListener(new OnClickListener() { 12 13 @Override 14 public void onClick(View v) { 15 //显示图片加字符 16 setWeather("晴空万里","天气预报","晴空万里",R.drawable.sun); 17 } 18 }); 19 button = (Button)findViewById(R.id.cloudy_1); 20 button.setOnClickListener(new OnClickListener() { 21 22 @Override 23 public void onClick(View v) { 24 //显示图片加字符 25 setWeather("阴云密布","天气预报","阴云密布",R.drawable.cloudy); 26 } 27 }); 28 button = (Button)findViewById(R.id.rain_1); 29 button.setOnClickListener(new OnClickListener() { 30 31 @Override 32 public void onClick(View v) { 33 //显示图片加字符 34 setWeather("大雨连绵","天气预报","大雨连绵",R.drawable.rain); 35 } 36 }); 37 button = (Button)findViewById(R.id.defaultSound); 38 button.setOnClickListener(new OnClickListener() { 39 40 @Override 41 public void onClick(View v) { 42 //默认的铃声 43 setDefault(Notification.DEFAULT_SOUND); 44 } 45 }); 46 button = (Button)findViewById(R.id.defaultVibrate); 47 button.setOnClickListener(new OnClickListener() { 48 49 @Override 50 public void onClick(View v) { 51 //默认的震动 52 setDefault(Notification.DEFAULT_VIBRATE); 53 } 54 }); 55 button = (Button)findViewById(R.id.defaultAll); 56 button.setOnClickListener(new OnClickListener() { 57 58 @Override 59 public void onClick(View v) { 60 //默认的铃声和震动 61 setDefault(Notification.DEFAULT_ALL); 62 } 63 }); 64 button = (Button)findViewById(R.id.clear); 65 button.setOnClickListener(new OnClickListener() { 66 67 @Override 68 public void onClick(View v) { 69 //清除通知栏中的内容 70 mNotificationManager.cancel(NOTIFICATIONS_ID); 71 } 72 }); 73 } 74 private void setWeather(String tickerText,String title,String content,int drawable) { 75 Notification notification = new Notification(drawable,tickerText, 76 System.currentTimeMillis()); 77 PendingIntent contentIntent = PendingIntent.getActivity(this, 78 0, new Intent(this,ToastActivity.class), 0); 79 notification.setLatestEventInfo(this, title, content, contentIntent); 80 mNotificationManager.notify(NOTIFICATIONS_ID,notification); 81 } 82 private void setDefault(int defaults){ 83 PendingIntent contentIntent = PendingIntent.getActivity(this, 84 0, new Intent(this,ToastActivity.class), 0); 85 String title = "天气预报"; 86 String content = "晴空万里"; 87 final Notification notification = new Notification(R.drawable.sun,content, 88 System.currentTimeMillis()); 89 notification.setLatestEventInfo(this, title, content, contentIntent); 90 notification.defaults = defaults; 91 mNotificationManager.notify(NOTIFICATIONS_ID,notification); 92 } 93 }
注:这个是从别处学来的,验证通过了的,谢谢原作者