安卓开发笔记——Notification通知栏
2015-07-14 13:19
393 查看
当用户有没有接到的电话的时候,Android顶部状态栏里就会出现一个小图标。提示用户有没有处理的快讯,当拖动状态栏时,可以查看这些快讯。Android给我们提供了NotificationManager来管理这个状态栏。可以很轻松的完成。
很基础的东西,直接看注释就可以了,随手粘贴。
看下效果图:
很基础的东西,直接看注释就可以了,随手粘贴。
看下效果图:
package com.example.notificationdemo; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private Button send; private Button cancel; private int flag=888;// 用来标志当前是哪个Notification private NotificationManager mNotificationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initListener(); // 取得通知系统服务 mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); } private void initListener() { this.send.setOnClickListener(this); this.cancel.setOnClickListener(this); } private void initView() { this.send = (Button) findViewById(R.id.send); this.cancel = (Button) findViewById(R.id.cancel); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.send: // 发送通知 sendNotification(); break; case R.id.cancel: // 取消通知 mNotificationManager.cancel(flag); break; } } private void sendNotification() { // 构建通知显示类 Notification.Builder builder = new Notification.Builder(this); // 设置通知样式内容 builder.setSmallIcon(R.drawable.ic_launcher);// 设置顶部通知栏小图标 builder.setContentTitle("我是通知标题栏");// 设置具体标题 builder.setContentText("我是通知具体内容");// 设置具体内容 builder.setWhen(System.currentTimeMillis());// 设置时间 // 设置提示灯,震动,声音(可以一起设置DEFAULT_ALL,需要对应权限) builder.setDefaults(Notification.DEFAULT_LIGHTS); builder.setDefaults(Notification.DEFAULT_SOUND); builder.setDefaults(Notification.DEFAULT_VIBRATE); Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); // 设置点击跳转Intent builder.setContentIntent(pendingIntent); // 设置通知 Notification notification; if (android.os.Build.VERSION.SDK_INT >= 16) { notification = builder.build();// 系统4.1以上 Toast.makeText(this, "当前系统版本4.1以上", Toast.LENGTH_LONG).show(); } else { notification = builder.getNotification();// 系统4.1以下 Toast.makeText(this, "当前系统版本4.1以下", Toast.LENGTH_LONG).show(); } // 发送通知 mNotificationManager.notify(flag, notification); } }
相关文章推荐
- 二分图及其匹配——匈牙利算法
- 奔跑中的2015——我想静静
- 潜伏Uber刷单QQ群:教程卖至千元 大学生旷课刷单日赚三四百
- 数的读法
- shell学习小结
- Oracle 字符集的查看和修改
- 如何使用EF来进行ASP.net的开发
- Latex beamer书签乱码解决方法
- Latex beamer书签乱码解决方法
- Latex beamer书签乱码解决方法
- Latex beamer书签乱码解决方法
- Latex beamer书签乱码解决方法
- Latex beamer书签乱码解决方法
- Latex beamer书签乱码解决方法
- Latex beamer书签乱码解决方法
- Latex beamer书签乱码解决方法
- Latex beamer书签乱码解决方法
- Latex beamer书签乱码解决方法
- Latex beamer书签乱码解决方法
- Latex beamer书签乱码解决方法