Toast简要说明:(前面已经用过好多次了)

  1. Toast是一种非持久的(在屏幕上面留一会儿就消失了),提供给用户简洁提示信息的视图。
  2. 它不阻断用户的操作,一般用于显示一些不重要的信息。(比方说设置音量的那个提示)
  3. Toast类可以用于创建和显示toast信息,toast一般翻译为吐司。

常用方法:(有set方法,也有get方法)

  1. Toast.makeText(context, text, duration);  //返回Toast对象
  2. toast.setDuration(duration);  //设置持续时间
  3. toast.setGravity(gravity, xOffset, yOffset);  //设置toast位置,后面的位置偏移参照物是gravity(center/center_horizontal/center_vertical/Bottomd等)
  4. toast.setText(s);  //设置提示内容
  5. toast.show(); //显示

代码演示一下:

1. 改变位置

后面两个是偏移量,是根据前面一个gravity参数来指定的。

2. toast显示图片

MainActivity.java

public class MainActivity extends Activity implements OnClickListener
{

    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.btn1);
        btn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v)
    {
        Toast toast = Toast.makeText(this, "带有图片的toast", Toast.LENGTH_LONG);
        LinearLayout toast_layout = (LinearLayout) toast.getView();
        ImageView iv = new ImageView(this);
        iv.setImageResource(R.drawable.ic_launcher);
        toast_layout.addView(iv); //再当前布局中加入一个iamgeview
        //toast_layout.addView(iv,0);则是指定线性布局中的索引
        toast.show();
    }
}

(布局文件就只有一个Button控件)

点击按钮,则出现下列情况。

3. 完全自定义的toast

先要去定义一个布局文件toastlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/ic_launcher"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="center"
        android:text="这个是自定义布局的toast"/>

</LinearLayout>

activity_main.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.toastytest.MainActivity" >

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示toast"
        />
</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity implements OnClickListener
{

    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.btn1);
        btn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v)
    {
        //为控件指定布局
        LayoutInflater inflater = LayoutInflater.from(this);
        View toast_view = inflater.inflate(R.layout.toastlayout, null);

        Toast toast = new Toast(this);
        toast.setView(toast_view);
        toast.show();
    }
}

点击效果如下:

(toast的用法,一般就是简单的提示作用)


Notification简要说明:

  1. 在设备的状态栏(通知栏)中显示
  2. 显示一条持久化信息
  3. 用到Notification和NotificationManager类

在这里,必须要分清3种情况:

  1. 其他应用程序在使用时,突然在通知栏中显示一条Notification
  2. 下拉状态栏,Notification显示一些详细信息
  3. 点击Notification,有相应的应用程序或者activity被激活

下面用代码演示一下:

新建一个活动NotificationView.java  (一会儿点击“下拉状态栏”里面的notification时,就跳转这个activity)

public class NotificationView extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notification);

        //点击notification后,在状态栏取消notification
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.cancel(getIntent().getExtras().getInt("notificationID"));
    }
}

其指定布局为 notification.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="这里是notification的详细信息" />

</LinearLayout>

当然还要注册activity—AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.notificationdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.VIBRATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".NotificationView"
            android:label="Details of notification"
            >
            <intent-filter >
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

    </application>

</manifest>

MainActivity.java

public class MainActivity extends Activity implements OnClickListener
{

    private int notificationID = 1; //用于被激活的activity去销毁notification

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v)
    {
        displayNotification();
    }

    private void displayNotification()
    {
        Intent i = new Intent(this,NotificationView.class);
        i.putExtra("notificationID", notificationID);

        //PendingIntent 可以让应用程序在后面某个时刻执行一个动作,而不考虑应用程序是否在运行
        //PendingIntent android.app.PendingIntent.getActivity(Context context,
        //                         int requestCode, Intent intent, int flags) flags是活动启动时的标志
        //点击了下拉的notification时,启动一个指定活动
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,0);

        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        //没有下拉时显示的notification
        Notification notif = new Notification(
                R.drawable.ic_launcher,
                "还有没下拉时的信息",
                System.currentTimeMillis());//发送时间

        //下拉时,显示Notification的信息
        notif.setLatestEventInfo(this, "下拉时", "详细信息是", pendingIntent);
        notif.vibrate = new long[]{100,250,100,500}; //发出震动
        nm.notify(notificationID,notif);
    }

}

主布局里面就一个button

<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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示一条通知" />

</RelativeLayout>

赶快来看看效果:

点击按钮、

下拉状态栏:

点击该下拉状态栏中的notification

(立刻就跳转到了新的activity)

小结:

  1. 突然显示了一条notification提示:Notification notif = new Notification( R.drawable.ic_launcher, "还有没下拉时的信息", System.currentTimeMillis());
  2. 下拉显示,需要用到NotificationManager:
    1. NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

      //下拉时,显示Notification的信息

      notif.setLatestEventInfo(this, "下拉时", "详细信息是", pendingIntent);

      notif.vibrate = new long[]{100,250,100,500}; //发出震动

      nm.notify(notificationID,notif);

  3. 点击下拉的状态栏里面的notification时,跳转:

Intent i = new Intent(this,NotificationView.class);
                         i.putExtra("notificationID", notificationID);
                         PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,0);

补充:PendingIntent 可以让应用程序在后面某个时刻执行一个动作,而不考虑应用程序是否在运行

实际上显示和取消通知栏都是NotificationManager在起作用:

notify(id, notification);

cancel(id);

构造Notification的时候,如果有初始化,那么就是最开始没有下拉时显示的状况了。

通知栏可以设置,提示音,指示灯,以及震动效果.

上面的代码,构建Notification对象的方法其实已经过时了,现在用的比较多的是Builder对象

Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher); //设置图标
builder.setTicker("没有下拉时显示的一个消息");

builder.setWhen(System.currentTimeMills()); //设置时间
builder.setContentTitle("下拉时标题");
builder.setContentText("下拉时详细信息");

PendingIntent pIntent = PendingIntent.getActivity(this,0,new Intent("com.example.NotificationView"),0);
builder.setContentIntent(pIntent); //点击后跳转
builder.setDefaults(Notification.DEFAULT_LIGHTS); //设置指示灯

Notification notif = builder.build(); //4.1及以上版本
//builder.getNotification();  //4.1以下版本

其中:

builder.setDefaults();

可以传入:

Notification.DEFAULT_SOUND;

Notification.DEFAULT_LIGHTS;

Notification.DEFAULT_VIBRATE;

或者设置一条:(相当于以上三条)

Notification.DEFUALT_ALL;


通知(Toast+Notification)的更多相关文章

  1. 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo

    [源码下载] 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo 作者:webabcd 介绍重新想象 Wind ...

  2. 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解

    [源码下载] 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Toa ...

  3. Android消息通知(notification)和PendingIntent传值

    通知栏的自定义布局:转:http://blog.csdn.net/vipzjyno1/article/details/25248021 拓展 实现自定义的通知栏效果: 这里要用到RemoteViews ...

  4. New Windows 10 SDK - Toast Notification

    概述 Toast Notification 在 UWP App 中有很重要的作用,能够很大程度上增强 App 和用户之间的沟通,比如运营推广活动.版本更新.提醒类任务提示等等.Toast Notifi ...

  5. 【转】通知 Toast详细用法(显示view)

    原文网址:http://www.pocketdigi.com/20100904/87.html 今天学习Android通知 Toast的用法,Toast在手机屏幕上向用户显示一条信息,一段时间后信息会 ...

  6. HTML5开启浏览器桌面通知 Web Notification

    说明: 1.Chrome要求必须https才可以开启浏览器通知 2.显示图片在本服务器,不支持跨越 3.自定义声音Chrome不播放,Firefox正常播放 代码如下: <!-- /** * @ ...

  7. iOS 远程通知(Remote Notification)和本地通知(Local Notification)

    ios通知分为远程通知和本地通知,远程通知需要连接网络,本地通知是不需要的,不管用户是打开应用还是关闭应用,我们的通知都会发出,并被客户端收到 我们使用远程通知主要是随时更新最新的数据给用户,使用本地 ...

  8. 介绍一个比较酷东西:HTML5 桌面通知(Notification API)

    Notification API 是 HTML5 新增的桌面通知 API,用于向用户显示通知信息.该通知是脱离浏览器的,即使用户没有停留在当前标签页,甚至最小化了浏览器,该通知信息也一样会置顶显示出来 ...

  9. redis键空间通知(keyspace notification)

    一.需求 在redis中,设置好key和生存时间之后,希望key过期被删除时能够及时的发送一个通知告诉我key,以便我做后续的一些操作. 二.环境 系统:windows10 php:7.1 redis ...

随机推荐

  1. SQL SERVER ->> CXPacket等待类型

    最近做了一个项目,把整个数据仓库平台下所有的表和索引都改成页级别的数据压缩.昨天发现测试环境下的某个workload跑得比平时慢.最后我们定位了到这个workload做的事情中可能造成性能下降的地方, ...

  2. URAL 1069 Prufer Code 优先队列

    记录每个节点的出度,叶子节点出度为0,每删掉一个叶子,度数-1,如果一个节点的出度变成0,那么它变成新的叶子. 先把所有叶子放到优先队列中. 从左往右遍历给定序列,对于root[i],每次取出叶子中编 ...

  3. 树形结构部门的 sqlserver 排序

    树形结构部门的 sqlserver 排序 因为要实现部门排序功能,而且要考虑部门的层级,直接用 sql 排序是不行的,所以写个 sql function 来支持. 首先部门表:company CREA ...

  4. SQL Service Database BACKUP & RESTORE

    1. 完整恢复模式下的数据库备份 USE master; ALTER DATABASE AdventureWorks2012 SET RECOVERY FULL; GO -- Back up the ...

  5. Git服务器搭建全过程分步详解【转】

    转自:http://developer.51cto.com/art/201507/483448.htm GitHub是一个免费托管开源代码的Git服务器,如果我们不想公开项目的源代码,又不想付费使用, ...

  6. 你猜……你再猜

    『男』:你喜欢我吗? 『女』:你猜. 『男』:喜欢. 『女』:你再猜. 『男』:--

  7. poj -2010 Moo University - Financial Aid (优先队列)

    http://poj.org/problem?id=2010 "Moo U"大学有一种非常严格的入学考试(CSAT) ,每头小牛都会有一个得分.然而,"Moo U&quo ...

  8. HeadFirst Jsp 04 (请求和响应作为servlet)

    servlet 的存在就是为了客服服务, servlet的任务是得到一个客户的请求, 再发回一个响应. 由上图可知, web 容器会在启动后就加载所有的servlet类, 并为之创建实例和初始化 注意 ...

  9. JAVA设计模式之【工厂方法模式】

    看例子 1.TV产品接口,负责播放 public interface TV // TV接口 { public void play(); } 2.TV工厂接口,负责生产产品 public interfa ...

  10. Git基础(三)

    本章 就开始和大家一起学习第三块内容:远程仓储的使用操作.要参与任何一个 Git 项目的协作,必须要了解该如何管理远程仓库.远程仓库是指托管在网络上的项目仓库,可能会有好多个,其中有些你只能读,另外有 ...