Notification通知栏设置及其点击响应
来源:互联网 发布:科勒淘宝商城旗舰店 编辑:程序博客网 时间:2024/06/02 04:48
转载请注明出处:http://blog.csdn.net/mr_leixiansheng/article/details/53611805
步骤:
1、新建好点击后跳转的类和布局(不需要跳转可跳过此步骤)
2、注册(养成良好习惯,每个活动都需要注册)
3、设置 NotificationManager获取哪个服务
4、设置Notification.Builder(通知栏各属性)
5、设置“意图”
6、创建Notification对象并获得Notification.Builder对象的属性
7、通过NotificationManager对象的.notify显示通知栏
代码如下:
package com.example.administrator.notification;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;/** * Created by Administrator on 2016/12/13. */public class NotificationIntent extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notification_layout); }}
<?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:id="@+id/text" android:text="this is a notification Intent" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>2、注册
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.administrator.notification"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".NotificationIntent"></activity> </application></manifest>3、Notification实现
package com.example.administrator.notification;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;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 { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //设置获取哪个服务 NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification.Builder builder = new Notification.Builder(MainActivity.this); builder.setSmallIcon(R.mipmap.ic_launcher); //设置图标 builder.setContentTitle("标题"); //设置标题 builder.setContentText("这是内容,点击我可以跳转"); //消息内容 builder.setWhen(System.currentTimeMillis()); //发送时间 builder.setDefaults(Notification.DEFAULT_ALL); //设置默认的提示音,振动方式,灯光 builder.setAutoCancel(true); //打开程序后图标消失 //跳转活动 Intent intent =new Intent (MainActivity.this,NotificationIntent.class); PendingIntent pi = PendingIntent.getActivities(MainActivity.this, 0, new Intent[]{intent}, PendingIntent.FLAG_CANCEL_CURRENT); builder.setContentIntent(pi); //创建通知栏对象,显示通知信息 Notification notification = builder.build(); manager.notify(1, notification); } }); }}
1 0
- Notification通知栏设置及其点击响应
- Android 通知栏Notification点击跳转无效
- Notification(Notification的通知栏常驻、Notification的各种样式、Notification点击无效)
- 通知栏通知Notification
- Notification 通知栏通知
- Notification通知栏通知
- Notification通知,点击通知后跳转
- Android -- Notification,如何向通知栏发送通知,点击通知开启相应的Activity
- Notification -- 一点击通知栏的通知,该通知就消失
- 认识Notification通知栏通知
- Notification(通知栏通知)
- Notification通知点击无效,无法跳转问题
- 通知的点击跳转(Notification)
- Android 通知栏 Notification
- Notification(通知栏)
- Android---Notification通知栏
- Android 通知栏Notification
- Android 通知栏Notification
- 细化热敏打印机打印bitmap图片参数
- test
- android-MediaRecorder.OutputFormat
- python将logging模块封装成单独模块并实现动态切换Level
- EasyUI使用tree生成树形结构加载两次的问题
- Notification通知栏设置及其点击响应
- 平台大小端 , 结构体大小-面试题
- IE的layout属性详解
- 最好的8个 Java RESTful 框架
- PL/SQL工具报:无效的窗口句柄
- IO流学习总结
- AFNetworking3.1.0源码分析(七)详解AFHTTPRequestSerializer 之AFStreamingMultipartFormData上
- MySQL 使用可视化工具导出与导入数据
- SequoiaDB版本在线升级介绍说明