2.13 Notification(通知)的使用
来源:互联网 发布:Mac 不能共享文件夹 编辑:程序博客网 时间:2024/05/19 00:35
一、布局文件
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/btn_show_normal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送通知" /> <Button android:id="@+id/btn_close_normal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关闭通知" android:layout_marginTop="50dp" /></RelativeLayout>
二、Java代码
1、新建一个Activity,命名为OtherActivity;
2、MianActivity添加如下代码:
package com.example.yuancan.test101902;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;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 Context mContext; private NotificationManager mNManager; private Notification notify1; Bitmap LargeBitmap = null; private static final int NOTIFYLID_1 = 1; private Button btn_show_normal; private Button btn_close_normal; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = MainActivity.this; LargeBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.avator_smaller); mNManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); bindView(); } private void bindView() { btn_show_normal = (Button)findViewById(R.id.btn_show_normal); btn_close_normal = (Button)findViewById(R.id.btn_close_normal); btn_show_normal.setOnClickListener(this); btn_close_normal.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_show_normal: //定义一个pendingIntent,点击Notification后启动一个Activity Intent it = new Intent(mContext, OtherActivity.class); PendingIntent pit = PendingIntent.getActivity(mContext, 0, it, 0); //设置图片,通知标题,发送时间,提示方式等 Notification.Builder mBuilder = new Notification.Builder(this); mBuilder.setContentTitle("您有一条新的通知") .setContentText("请不要忘记打卡") .setSubText("上下班请记得打卡") .setTicker("收到信息") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(LargeBitmap) .setDefaults(Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE) .setAutoCancel(true) .setContentIntent(pit); notify1 = mBuilder.build(); mNManager.notify(NOTIFYLID_1, notify1); break; case R.id.btn_close_normal: mNManager.cancel(NOTIFYLID_1); break; } }}
0 0
- 2.13 Notification(通知)的使用
- Notification通知的使用
- Notification通知栏的使用
- android通知(Notification)的使用
- Notification通知栏的使用
- android Notification(通知) 的使用
- Notification(通知)的简单使用
- Notification(通知)的使用简介
- NOtification(通知)的使用
- Notification 通知栏的使用
- Android通知Notification的使用
- notification(通知)的使用
- notification(通知)的使用
- ANDROID NOTIFICATION 通知的使用
- Android中通知的使用-----Notification详解
- Android中通知的使用-----Notification详解
- Android中通知的使用-----Notification详解
- Android中通知的使用-----Notification详解
- 解决VS2008编译的程序在某些机器上运行提示“由于应用程序配置不正确,应用程序未能启动”的问题
- 小程序的swiper不显示图片
- GPRS Multislot operation
- ScrollView、RecyclerView、ScrollView嵌套ListView性能优化方案
- 为什么要用存储过程
- 2.13 Notification(通知)的使用
- node.js与express4.X实战--快速构建一个简单的API接口(翻译)
- shader ide
- Python3中使用map()结果出错的解决方法
- 题型:涉及到i++的用法,变量作用域以及闭包的概念
- AR--AR 小应用究竟是如何实现的
- 自己动手合成喜欢的编程字体
- 别人眼中的程序员VS现实中的程序员
- virt viewer 3.0 交叉编译