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.需要在 ...
随机推荐
- Luogu P3324 [SDOI2015]星际战争
二分+最大流 首先考虑二分答案 然后可以发现对于已知时间,判断是否可以将所有机器人摧毁可以用网络流 建立源点和汇点,源点向每一个激光武器连一条容量为$time*b[i]$的边,表示该激光武器在$tim ...
- [POJ 2821]TN's Kindom III(任意长度循环卷积的Bluestein算法)
[POJ 2821]TN's Kindom III(任意长度循环卷积的Bluestein算法) 题面 给出两个长度为\(n\)的序列\(B,C\),已知\(A\)和\(B\)的循环卷积为\(C\),求 ...
- vim实现CTRL+S为保存快捷键
用vim正撸代码撸的飞起,突然Xshell就掉线了,真是太蛋疼了. 于是开始怀念起vs下撸代码时随时随地ctrl+s保存的快捷键,百度了一下,网上的vim实现ctrl+s保存的快捷键设置都有问题,自己 ...
- 百度地图省市php获取
$api = 'http://api.map.baidu.com/shangquan/forward/?qt=sub_area_list&ext=1&level=3&areac ...
- 百度开源插件echarts介绍及如何使用
前言 如果你想要用较少的代码实现比较酷炫的数据统计表,echarts是值得你考虑的一种实现方式.官网提供了很多实例供参考:http://echarts.baidu.com/examples.html. ...
- VBA_Copy数据及数据格式_DoLoop删除空行
Sub copyreport() Application.ScreenUpdating = False Application.Calculation = xlCalculationManual Ap ...
- java字符串大小写转换的两种方法
转载自:飞扬青春sina blogjava字符串大小写转换的两种方法 import java.io..* public class convertToPrintString { pu ...
- 四、c++总结------linux多线程服务端编程
- transformer多头注意力的不同框架实现(tensorflow+pytorch)
多头注意力可以用以下一张图描述: 1.使用pytorch自带的库的实现 torch.nn.MultiheadAttention(embed_dim, num_heads, dropout=0.0, b ...
- git操作之二:git restore
在上篇博客中留了一个问题,那就是git restore命令是做什么的,下面重点分析. 一.概述 git restore命令是撤销的意思,也就是把文件从缓存区撤销,回到未被追踪的状态. 该命令有git ...