Android笔记(二十八)通知的使用
2015-09-10 11:41
441 查看
一、通知的用法
当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现。相比于广播***和服务,在活动里创建通知的场景还是比较少的,因为一般只有当程序进入到后台的时候我们才需要使用通知。获得NotificationManager 的实例
创建一个 Notification 对象
设定通知的布局
调用 NotificationManager 的 notify()方法
二、具体实例——通过点击按钮来发出一条通知
建立布局<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/send_notice" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send notice" /> </LinearLayout>
MainActivity
public class MainActivity extends ActionBarActivity { private Button button; private NotificationManager manager; private Notification.Builder builder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.send_notice); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); builder = new Notification.Builder(MainActivity.this); Intent intent = new Intent(MainActivity.this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity( MainActivity.this, 0, intent, 0); builder.setContentIntent(contentIntent); builder.setTicker("这是一个通知"); builder.setContentTitle("通知"); builder.setContentText("hello"); builder.setDefaults(Notification.DEFAULT_ALL); builder.setSmallIcon(R.drawable.ic_launcher); Notification notification = builder.build(); manager.notify(1, notification); } }); } }
相关文章推荐
- 基于Android Studio 的NDK之 “Hello,World!”
- android-仿图片地震波SeismicWaveView
- Android动画(Animation)
- Ubuntu和Mac中Android源码查看工具
- Android初学-----环形进度条 带渐变
- 【问题解决】Android Stdio报错:Error occurred during initialization of VM Could not reser
- Android最佳性能实践(一)——合理管理内存
- Android几种FrameWork(Manager)的用法
- 升级android sdk到5.0,创建项目错误解决方法
- android之wifi移植全过程(一)
- Android中Bitmap,byte[],Drawable相互转化
- Android ---js与java的相互调用
- Android不规则布局并触发点击事件
- Android开发中遇到的小问题
- js调android的代码
- Android使用ORM思想封装数据库
- 使用volley实现android的三级缓存
- [Android Pro] android 混淆文件project.properties和proguard-project.txt
- 解读Android之Service(2)Bound Service
- Android动画-FrameAnimation