Android中的消息通知Toast和Notification

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

    <Button
        android:id="@+id/showToast"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/_toast" />
 <Button
        android:id="@+id/showLongToast"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示一个较长的Toast" />

 <Button
     android: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" />
时间: 2024-08-02 06:41:14

Android中的消息通知Toast和Notification的相关文章

Android中的消息通知(NotificationManager和Notification)

下面来谈谈notification,这个notification一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这个快讯.已添加的Notification.Builder,使其更容易构建通知.notification是一种让你的应用程序在没有开启情况下或在后台运行警示用户.它是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径.

Android中的消息机制

在分析Android消息机制之前.我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListener { private TextView stateText; private Button btn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); s

android 实现自定义状态栏通知(Status Notification)

在android项目的开发中,有时为了实现和用户更好的交互,在通知栏这一小小的旮旯里,我们通常需要将内容丰富起来,这个时候我们就需要去实现自定义的通知栏,例如下面360或者网易的样式: 首先我们要了解的是 自定义布局文件支持的控件类型:Notification的自定义布局是RemoteViews,因此,它仅支持FrameLayout.LinearLayout.RelativeLayout三种布局控件,同时支持AnalogClock.Chronometer.Button.ImageButton.I

浅析Android中的消息机制(转)

原博客地址:http://blog.csdn.net/liuhe688/article/details/6407225 在分析Android消息机制之前,我们先来看一段代码: 1 public class MainActivity extends Activity implements View.OnClickListener { 2 private TextView stateText; 3 private Button btn; 4 5 @Override 6 public void onC

浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.

在分析Android消息机制之前,我们先来看一段代码: [html] view plaincopyprint? public class MainActivity extends Activity implements View.OnClickListener { private TextView stateText; private Button btn; @Override public void onCreate(Bundle savedInstanceState) { super.onC

浅析Android中的消息机制(转)

在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListener { private TextView stateText; private Button btn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); s

深入解析Android中Handler消息机制

Android提供了Handler 和 Looper 来满足线程间的通信.Handler先进先出原则.Looper类用来管理特定线程内对象之间的消息交换(MessageExchange).Handler消息机制可以说是Android系统中最重要部分之一,所以,本篇博客我们就来深入解析Android中Handler消息机制. Handler的简单使用 为什么系统不允许子线程更新UI 因为的UI控件不是线程安全的. 如果在多线程中并发访问可能会导致UI控件处于不可预期的状态,那为什么不对UI控件的访

Android 中的消息模型(Message,MessageQueue,handle,looper)

Android 中的消息模型(Message,MessageQueue,handle,looper,) Android 中的消息通讯 1.Android 中线程的应用机制? 1)Android 中所有的耗时操作应在工作线程执行. 2)Android 中所有的UI操作应该在主线程(UI线程)执行. FAQ? 1)主线程执行执行耗时操作好吗? 不好,这样会阻塞UI操作. 2)工作执行完耗时操作,假如有数据要传递给主线程,那如何实现? 2.Android 中多线程应用时的消息模型? 使用Android

Android中对消息机制(Handler)的再次解读

今天遇到一些关于在子线程中操作Handler的问题,感觉又要研究源代码了,但是关于Handler的话,我之前研究过,可以参考这篇文章:http://blog.csdn.net/jiangwei0910410003/article/details/17021809.但是这篇文章没有说的那么深入了,所以这次就更深入的解读一下. 摘要 Android中的应用程序都是通过消息驱动的,系统为每一个应用程序维护一个消息队列(MessageQueue),应用程序的主线程不断的从这个消息队列中获取消息(Loop