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" />
本性的苏醒,往往在遭遇真实之后。
原文地址:https://www.cnblogs.com/chance88/p/4677431.html