Android中的消息通知Toast和Notification

1.弹出通知Toast

MainActivity.java

 1 package com.example.toast;
 2
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.view.Gravity;
 6 import android.view.Menu;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.ImageView;
10 import android.widget.Toast;
11
12 public class MainActivity extends Activity {
13     private Button showToast,showLongToast,showImageToast;
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_main);
18
19         showToast=(Button) findViewById(R.id.showToast);
20         showLongToast=(Button) findViewById(R.id.showLongToast);
21         showImageToast=(Button)findViewById(R.id.showImageToast);
22         showToast.setOnClickListener(new View.OnClickListener() {
23
24             @Override
25             public void onClick(View v) {
26                 // TODO 自动生成的方法存根
27                 Toast ashort=Toast.makeText(MainActivity.this, "显示一个简短的Toast", Toast.LENGTH_SHORT);
28                 ashort.setGravity(Gravity.CENTER, 100, -200);//设置偏移量
29                 ashort.show();
30             }
31         });
32         showLongToast.setOnClickListener(new View.OnClickListener() {
33
34             @Override
35             public void onClick(View v) {
36                 // TODO 自动生成的方法存根
37                 Toast.makeText(MainActivity.this, "显示一个较长的Toast", Toast.LENGTH_LONG).show();
38             }
39         });
40
41
42       showImageToast.setOnClickListener(new View.OnClickListener() {
43
44         @Override
45         public void onClick(View v) {
46             // TODO 自动生成的方法存根
47             Toast ImageToast=Toast.makeText(MainActivity.this, "显示一个带有图片的Toast", Toast.LENGTH_SHORT);
48             ImageView imageView=new ImageView(MainActivity.this);
49             imageView.setImageResource(R.drawable.ic_launcher);
50             ImageToast.setView(imageView);
51             ImageToast.show();
52         }
53     });
54     }
55
56     @Override
57     public boolean onCreateOptionsMenu(Menu menu) {
58         // Inflate the menu; this adds items to the action bar if it is present.
59         getMenuInflater().inflate(R.menu.main, menu);
60         return true;
61     }
62
63 }

activity_main.xml

<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"tools:context=".MainActivity"android:orientation="vertical"><Buttonandroid:id="@+id/showToast"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/_toast" /><Buttonandroid:id="@+id/showLongToast"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="显示一个较长的Toast" /><Buttonandroid:id="@+id/showImageToast"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="showImageToast" /></LinearLayout>

2.状态栏提示Notification

MainActivity.java

 1 package com.example.notification;
 2
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.app.Notification;
 6 import android.app.Notification.Builder;
 7 import android.app.NotificationManager;
 8 import android.content.Context;
 9 import android.support.v4.app.NotificationCompat;
10 import android.view.Menu;
11 import android.view.View;
12 import android.widget.Button;
13
14 public class MainActivity extends Activity {
15     private Button BNotification;
16     public static final int NOFIFICATION_ID=2000;
17     private int conter=1;
18
19
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_main);
24         BNotification=(Button) findViewById(R.id.button1);
25         BNotification.setOnClickListener(new View.OnClickListener() {
26
27             @Override
28             public void onClick(View v) {
29                 conter++;
30                 // TODO 自动生成的方法存根
31         android.support.v4.app.NotificationCompat.Builder builder=new NotificationCompat.Builder(MainActivity.this);
32         builder.setSmallIcon(R.drawable.ic_launcher);
33         builder.setContentText("哇!你有"+conter+"个新校息!");
34         builder.setContentTitle("Hello Notification!");
35         Notification notification=builder.build();
36         NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
37         manager.notify(NOFIFICATION_ID, notification);
38
39             }
40         });
41     }
42
43
44     @Override
45     public boolean onCreateOptionsMenu(Menu menu) {
46         // Inflate the menu; this adds items to the action bar if it is present.
47         getMenuInflater().inflate(R.menu.main, menu);
48         return true;
49     }
50
51 }

activity_main.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context=".MainActivity" >
 7
 8     <Button
 9         android:id="@+id/button1"
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content"
12         android:text="@string/_notification" />

转载于:https://www.cnblogs.com/chance88/p/4677431.html

Android中的消息通知Toast和Notification相关推荐

  1. Android中的消息推送

    转载于Android中的消息推送 前段时间做了一个应用,需要用到服务器端向Android或者是Iphone终端主动发送命令.随后客户端做出相应的反应.当时没有找到最佳的方案,一直搁置着.今天看到网上有 ...

  2. 浅析Android中的消息机制

    在分析Android消息机制之前,我们先来看一段代码: [java] view plaincopy public class MainActivity extends Activity impleme ...

  3. 重温Android中的消息机制

    引入: 提到Android中的消息机制,大家应该都不陌生,我们在开发中不可避免的要和它打交道.从我们开发的角度来看,Handler是Android消息机制的上层接口.我们在平时的开发中只需要和Hand ...

  4. Electron中的消息通知

    Electron中的消息通知是基于H5的Notification来实现的,比较常见的使用场景是用于监听网络变化然后进行消息提示. 1. 消息提示: var option = {title: '温馨提示 ...

  5. android gmail 不翻墙,Android延迟Gmail消息通知,可能只是为了省电

    原标题:Android延迟Gmail消息通知,可能只是为了省电 IT之家11月9日消息 据Android Authority消息,推送通知是我们智能手机的重要组成部分.没有这些推送,我们可能会错过重要 ...

  6. Android中的消息机制

    Android 中的消息机制其实就是指的是 Handler 消息机制以及附带的 Looper 和 MessageQueue 的工作流程. 1.Android 为什么提供Handler? 解决子线程不能 ...

  7. odoo中的消息通知

    odoo有着强大的消息记录.通知机制: 假如有的用户修改了内容, 就会记录并发布消息. 首先,先保证整体基本的消息记录功能,两个地方需要修改,模型和form视图 在odoo中集成消息通知, 只需要在对 ...

  8. Java中集成极光推送实现给Android提送消息通知(附代码下载)

    场景 Android中集成极光推送实现推送消息通知与根据别名指定推送附示例代码下载: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details ...

  9. android o preview 3,Android O Preview 之 通知渠道(Notification Channels)

    介绍 Android O 引入了 通知渠道(Notification Channels),以提供统一的系统来帮助用户管理通知,如果是针对 android O 为目标平台时,必须实现一个或者多个通知渠道 ...

  10. Android中的消息机制:Handler消息传递机制

    参考<疯狂android讲义>第2版3.5 P214 一.背景 出于性能优化考虑,Android的UI操作并不是线程安全的,这意味着如果有多个线程并发操作UI组件,可能导致线程安全问题.为 ...

最新文章

  1. 4月书讯 | 从引爆技术圈的 K8s 到图灵奖得主作品升级,从独角兽项目到人人都要学习的写作逻辑...
  2. 化工设备与反应器 第二章 直杆的拉伸与压缩
  3. excel平均值公式_投资组合Normal VaR的具体计算方法(Excel版)
  4. java工具类下载_java文件下载工具类
  5. java通用分页条件查询_通用分页查询
  6. ASP.NET操作Excel
  7. 类似mysqlhotcopy备份mysql myisam脚本
  8. 【kafka】 kafka 0.10报错IOException: Connection to 1 was disconnected before the response was read
  9. Eeic Meyer on CSS 之 背景半透明效果
  10. windows 配置squid反向代理服务器
  11. windows本地安装PLSQL
  12. httppost请求工具类
  13. Java中的正则表达式 regex
  14. 菱形图案c语言程序,C语言程序设计,做一个菱形图案
  15. 【NAACL 2021】RCI:在基于 Transformer 的表格问答中行和列语义捕获
  16. Laravel使用Dingo API+JWT实现认证机制 无痛刷新Token
  17. 【小5聊】小程序之体验版本wx.chooseImage选择图片上传可以,而线上正式版本选择图片一闪而过的原因
  18. 从服装关键点、信息检索、个性化推荐到智能试衣,电商AI技术如何进化?
  19. CL7016 – 用户能灵活添加自己应用的USB 接口音频Codec
  20. CA证书原理(转载)

热门文章

  1. Metasequoia 4 for Mac(3D建模)
  2. Sketch 80 for mac(矢量绘图设计软件)
  3. 如何在苹果Mac上删除APFS卷?
  4. Python3 列表
  5. SDRAM学习笔记(二)
  6. angularjs学习:事件
  7. CSS3 实现图片上浮动画
  8. 使用SCVMM2008 R2管理Hyper-V之3-使用模板部署虚拟机
  9. 不是我吹,这款神仙 IDEA 插件你真没用过!
  10. 读完《Effective Java》后我淦了 50 条开发技巧