View(视图)——消息
一.Toast:由makeText 构建并返回 Toast
1.context
2.消息内容
3.显示的时间长短:①Toast.LENGTH_SHORT ②Toast.LENGTH_LONG
二.Notification
1.状态栏消息
可以跳转到Activity上
2.用法
(1)得到NotificationManager
①(NotificationManager)getSystemService(NOTIFICATION_SERVICE)
②NotificationManager是系统内置的Service
(2)构建Notification
new Notification.Builder( ) 构建器:
①setContentTitle 设置内容标题
②setContentText 设置内容文本
③setSmallIcon 设置图标
④setTicker 设置状态栏提示
⑤setDefaults 设置提示方式:1.Notification.DEFAULT_SOUND 声音 2.其他需要授权
⑥setAutoCancel 设置点击跳转后自动消失: 默认true
⑦setContentIntent 设置内容意图:1.创建Intent 2.生成PendingIntent pi=PendingIntent.getActivity(context, 0 请求码,intent 意图,0 标记);
⑧build()创建并返回Notification
(3)由NotificationManager发送消息
notify(id,消息)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp2.TestActivity11" 11 android:orientation="vertical"> 12 <Button 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:text="发送状态栏消息" 16 android:onClick="bt_OnClick"/> 17 18 19 </LinearLayout>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.hanqi.testapp2; 2 3 import android.app.Notification; 4 import android.app.NotificationManager; 5 import android.app.PendingIntent; 6 import android.content.Intent; 7 import android.os.Bundle; 8 import android.support.v7.app.AppCompatActivity; 9 import android.view.View; 10 import android.widget.AdapterView; 11 import android.widget.ArrayAdapter; 12 import android.widget.AutoCompleteTextView; 13 import android.widget.Spinner; 14 import android.widget.Toast; 15 16 public class TestActivity11 extends AppCompatActivity { 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_test11); 21 //发消息 22 public void bt_OnClick(View v) 23 { 24 //1.得到状态栏消息管理器 25 NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); 26 27 28 //准备PendingIntent 29 //1)创建Intent 30 Intent intent=new Intent(this,TestActivity1.class); 31 32 //2)生成 33 PendingIntent pi=PendingIntent.getActivity(this,0,intent,0); 34 35 //2.构建状态栏消息 36 Notification nt=new Notification.Builder(this) 37 .setContentTitle("天气预报") 38 .setContentText("明天晴,气温30度") 39 .setSmallIcon(R.drawable.ff1) 40 .setTicker("新的天气预报") 41 .setDefaults(Notification.DEFAULT_SOUND) 42 .setAutoCancel(true) 43 .setContentIntent(pi) 44 .build(); 45 46 //3.由管理器发送消息 47 nm.notify(0,nt); 48 } 49 }
点击天气预报后