[android]_[Notication的使用]
来源:互联网 发布:社交网络sns 编辑:程序博客网 时间:2024/05/16 18:59
相信多数人都知道安卓上的通知,这原本是安卓系统的一大特点,早期iOS和wp还没有。
一、创建通知的步骤:
1.创建NotificationManager
2.创建Notification
3.调用NotificationManager的方法notify()产生通知
代码如下:
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);Notification notification = new Notification.Builder(this).setContentTitle("This is content title").setContentText("This is ticker text").setSmallIcon(R.mipmap.ic_launcher).build();manager.notify(1, notification);
二、通知的其它属性
1.点击通知框弹出窗口
2.弹出窗口后关闭通知
例子完整代码:
MainActivity:
package com.example.administrator.notification;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button sendNotice; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendNotice = (Button) findViewById(R.id.send_notice); sendNotice.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.send_notice: NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Intent intent = new Intent(this, NotificationActivity.class); PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); Notification notification = new Notification.Builder(this) .setContentTitle("This is content title") .setContentText("This is ticker text") .setContentIntent(pi) .setSmallIcon(R.mipmap.ic_launcher) .build(); manager.notify(1, notification); break; default: break; } }}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:id="@+id/send_notice" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send notice" /></RelativeLayout>
NotificationActivity:
package com.example.administrator.notification;import android.app.Activity;import android.app.NotificationManager;import android.os.Bundle;/** * Created by Administrator on 2016/2/21. */public class NotificationActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notification_layout); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.cancel(1); }}
notification_layout.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="24sp" android:text="This is notification layout" /></LinearLayout>
0 0
- [android]_[Notication的使用]
- Android Notication的使用
- 使用Notication在状态栏上显示通知
- [android]_[ListView的基本使用]
- [android]_[ViewGroup的基本使用]
- [android]_[handler的简单使用]
- [Android]_[RxJava的基本使用]
- IT人才如何使用HTML5 Notication API实现一个定时提醒工具
- 12_项目中关于android中Parcelable的使用
- Android特色开发_位置与传感器的使用
- [Android]_[注解库butterknife的基本使用]
- Android新组件RecyclerView的简单使用_项海涛
- Android 侧滑菜单DrawerLayout简单的使用_田少嵩
- android服务_使用服务注册特殊的广播接收者
- 909422229_对于Android即时通讯融云的使用总结
- Android基础_传感器的使用 Fragment入门(十二)
- Android基础知识_使用Service
- android _常见控件使用
- 小紫书 4-3 UVA 220 Othello
- 百思不得其姐关注模块的推荐关注中 右侧TableView刷新细节
- thinkPHP浓缩版笔记
- [Android]ToolBar使用心得
- c语言入门之项目3.0——求和的和
- [android]_[Notication的使用]
- 数据结构之模式匹配KMP算法的next数组求解
- thinkPHP框架搭建心得
- EXE文件脱壳解压#层序遍历搜索#源码
- 九度OJ 题目1172:哈夫曼树&题目1107:搬水果
- 《笨办法学Python》 第42课手记
- Using DevStack with neutron Networking
- Android夜间模式最佳实践
- iOS-UI控件精讲之【1】-UIView