09_消息通知Toast和Notification
1. Toast
学习创建长短不一的Toast提示,并自定义Toast在屏幕上的位置以及Toast的外观。
1 package com.example.toastdemo;
2
3 import android.app.Activity;
4 import android.app.ActionBar;
5 import android.app.Fragment;
6 import android.os.Bundle;
7 import android.view.Gravity;
8 import android.view.LayoutInflater;
9 import android.view.Menu;
10 import android.view.MenuItem;
11 import android.view.View;
12 import android.view.ViewGroup;
13 import android.widget.Button;
14 import android.widget.ImageView;
15 import android.widget.Toast;
16 import android.os.Build;
17
18 public class MainActivity extends Activity {
19
20 private Button btnShowToastShort, btnShowToastLong, btnShowToastImage;
21
22 @Override
23 protected void onCreate(Bundle savedInstanceState) {
24 super.onCreate(savedInstanceState);
25 setContentView(R.layout.activity_main);
26
27 btnShowToastShort = (Button) findViewById(R.id.btnShowToast);
28 btnShowToastLong = (Button) findViewById(R.id.btnShowToastLong);
29 btnShowToastImage = (Button) findViewById(R.id.btnShowToastImage);
30
31 btnShowToastShort.setOnClickListener(new View.OnClickListener() {
32
33 @Override
34 public void onClick(View v) {
35 // 可以改变Toast显示的位置;
36
37 Toast shortToast = Toast.makeText(MainActivity.this,
38 "显示一个简短的Toast", Toast.LENGTH_SHORT);
39 shortToast.setGravity(Gravity.CENTER, 0, 0);
40 shortToast.show();
41 }
42 });
43
44 btnShowToastLong.setOnClickListener(new View.OnClickListener() {
45
46 @Override
47 public void onClick(View v) {
48 Toast.makeText(MainActivity.this, "显示一个较长的Toast",
49 Toast.LENGTH_LONG).show();
50 }
51 });
52
53 btnShowToastImage.setOnClickListener(new View.OnClickListener() {
54
55 // 显示图片之后,就不会显示文字。
56 // 若想显示其它,需要定义Layout
57 @Override
58 public void onClick(View v) {
59 Toast imageToast = Toast.makeText(MainActivity.this,
60 "显示一个图片的Toast", Toast.LENGTH_LONG);
61 ImageView imageView = new ImageView(MainActivity.this);
62 imageView.setImageResource(R.drawable.ic_launcher);
63 imageToast.setView(imageView);
64 imageToast.show();
65 }
66 });
67
68 }
69
70 }
MainActivity
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:id="@+id/container"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:orientation="vertical"
7 tools:context="com.example.toastdemo.MainActivity"
8 tools:ignore="MergeRootFrame" >
9
10 <Button
11 android:id="@+id/btnShowToast"
12 android:layout_width="fill_parent"
13 android:layout_height="wrap_content"
14 android:text="显示一个简短的Toast" />
15
16 <Button
17 android:id="@+id/btnShowToastLong"
18 android:layout_width="fill_parent"
19 android:layout_height="wrap_content"
20 android:text="显示一个较长的Toast" />
21
22 <Button
23 android:id="@+id/btnShowToastImage"
24 android:layout_width="fill_parent"
25 android:layout_height="wrap_content"
26 android:text="显示一个图片的Toast" />
27
28 </LinearLayout>
activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.example.toastdemo"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk
8 android:minSdkVersion="14"
9 android:targetSdkVersion="19" />
10
11 <application
12 android:allowBackup="true"
13 android:icon="@drawable/ic_launcher"
14 android:label="@string/app_name"
15 android:theme="@style/AppTheme" >
16 <activity
17 android:name="com.example.toastdemo.MainActivity"
18 android:label="@string/app_name" >
19 <intent-filter>
20 <action android:name="android.intent.action.MAIN" />
21
22 <category android:name="android.intent.category.LAUNCHER" />
23 </intent-filter>
24 </activity>
25 </application>
26
27 </manifest>
AndroidManifest.xml
2. Notification
学习创建Notification对象,为其指定标题、内容和图标,以及Notification的更新方法。
1 package com.example.notificationdemo;
2
3 import android.app.Activity;
4 import android.app.Notification;
5 import android.app.NotificationManager;
6 import android.content.Context;
7 import android.os.Bundle;
8 import android.support.v4.app.NotificationCompat;
9 import android.support.v4.app.NotificationCompat.Builder;
10 import android.view.View;
11 import android.widget.Button;
12
13 public class MainActivity extends Activity {
14
15 public static final int NOTIFICATION_ID = 1234;
16 private Button btn1;
17 private int conter = 0;
18
19 @Override
20 protected void onCreate(Bundle savedInstanceState) {
21 super.onCreate(savedInstanceState);
22 setContentView(R.layout.activity_main);
23
24 btn1 = (Button) findViewById(R.id.but1);
25 btn1.setOnClickListener(new View.OnClickListener() {
26
27 @Override
28 public void onClick(View v) {
29 conter++;
30 Builder builder = new NotificationCompat.Builder(
31 MainActivity.this);
32 builder.setSmallIcon(R.drawable.ic_launcher);
33 builder.setContentTitle("你已经创建" + conter + "个新消息了!");
34 builder.setContentText("Notification~~~");
35
36 Notification notification = builder.build();
37 NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
38 manager.notify(NOTIFICATION_ID, notification);
39
40 }
41 });
42
43 }
44
45 }
MainActivity
1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:id="@+id/container"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 tools:context="com.example.notificationdemo.MainActivity"
7 tools:ignore="MergeRootFrame" >
8
9 <Button
10 android:id="@+id/but1"
11 android:layout_width="fill_parent"
12 android:layout_height="wrap_content"
13 android:text="创建一个提示" />
14
15 </FrameLayout>
activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.example.notificationdemo"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk
8 android:minSdkVersion="14"
9 android:targetSdkVersion="19" />
10
11 <application
12 android:allowBackup="true"
13 android:icon="@drawable/ic_launcher"
14 android:label="@string/app_name"
15 android:theme="@style/AppTheme" >
16 <activity
17 android:name="com.example.notificationdemo.MainActivity"
18 android:label="@string/app_name" >
19 <intent-filter>
20 <action android:name="android.intent.action.MAIN" />
21
22 <category android:name="android.intent.category.LAUNCHER" />
23 </intent-filter>
24 </activity>
25 </application>
26
27 </manifest>
AndroidManifest.xml
09_消息通知Toast和Notification的更多相关文章
- 桌面消息通知:HTML5 Notification
先上一段完整代码 //注册权限 Notification.requestPermission(function (status) { // 这将使我们能在 Chrome/Safari 中使用 Noti ...
- Android消息通知(notification)和PendingIntent传值
通知栏的自定义布局:转:http://blog.csdn.net/vipzjyno1/article/details/25248021 拓展 实现自定义的通知栏效果: 这里要用到RemoteViews ...
- Android消息通知-Notification
Android中常用的消息提醒,一种是Toast弹出提醒内容,一种是AlterDialog弹出框来提醒用户,还有一种就是消息通知的,用Android经常收到各种通知就是Notifation.Notif ...
- Android 通知机制 Toast和Notification
Android常用的反馈系统状态信息的方式主要有三种 Toast提醒 通知栏提醒 对话框提醒 三种提醒分别适用于页面的提示.系统交互事件的通知和非常重要的提醒: 一.Toast Toast toast ...
- Android中的消息通知(NotificationManager和Notification)
下面来谈谈notification,这个notification一般用在电话,短 信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这 ...
- Android Notification 消息通知 相关资料.md
目录 Android Notification 消息通知 相关资料 Android 5.0 Lollipop (API 21)无法正常显示通知图标,只能看到一个白色方块或灰色方块的问题 解决方案 参考 ...
- UWP消息通知
在Windows 10通常是使用Toast通知方式进行的消息通知,但是在应用通知是不需要通知带有音效的,但是又不能在系统通知中心留下记录,那么需要监听ToastNotification实例的Dismi ...
- 使用 Windows10 自定义交互消息通知
消息通知是最常用的应用功能之一了,但是由于平台的差异,IOS Android 以及 Windows 都有其特殊性,Android开发者在国内常常都是使用三方的一些推送服务,或者是使用自建的服务器为应用 ...
- 【wpf】在win10系统上弹出toast和notification
原文:[wpf]在win10系统上弹出toast和notification 老规矩,先看效果 右下角的notification: 操作中心的notification: 整体效果: 前提条件 1.需要在 ...
随机推荐
- eyou升级弹窗、云插件库、接口配置、功能开关【按需显示插件】
分享一个实用三方插件,如插件描述所言,很多时候我们不希望客户乱搞. hbh.cool/find/146.html
- PASS模型-第一周个人报告
PASS模型-第一周个人报告 博客班级 https://edu.cnblogs.com/campus/zjcsxy/SE2020 作业要求 https://edu.cnblogs.com/campus ...
- GDB调试基础使用方法
尽管目前使用的VS code可以使用插件一键构建和运行程序,但GDB作为调试利器,还是值得花时间去学习的. 概述 GDB(GNU Debugger) 是一个由GNU开源组织发布的.UNIX/LINUX ...
- 【Luogu】 P5482 [JLOI2011]不等式组 题解
本来以为有多难,结果发现是道树状数组水题... 显然,对于每一个添加的不等式,有3种情况: \(a<0\) .此时可转换为 $x < {{a} \over {c-b}} $ . 但是,我们 ...
- Java_静态代理与Lambda
静态代理 要点: 公共接口 真实角色 代理角色 public class StaticProxy { public static void main(String[] args) { You you ...
- 毕业一年后接私活赚了10w,还拿了几家大厂offer!
原本计划这周接着写一篇技术文章的,但是没想到忙到天天凌晨回家,几乎没有为下一篇文章做准备的时间(通常写一篇文章需要至少 30 个小时,需要搭进日常下班及周末的休息时间).这周如果写的话精力和时间都达不 ...
- MySql中指定符号分割并分行展示
1.涉及到的函数三个: 1.1 REPLACE('value','str1','str2') 用法规则:使用str2替换掉value中的所有的str1; SELECT REPLACE('我来了','来 ...
- PHP无限级评论回复功能实现
protected function commentList($aid,$pid = 0,&$result=array()){ $arr = ArticleComment::relation( ...
- 关于“Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.”
Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the c ...
- UNP第11章——名字与地址转换
1.域名系统 程序中只使用主机名和服务名的好处是,如果IP或端口变化,只需要改变映射关系,不需要重新编译程序. 1.1 资源记录 DNS的条目为资源记录,有用的项如下: A IPv4地址 AAAA I ...