Android UI控件详解-Notification(通知)
2014-04-08 16:54
288 查看
xml布局
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"
android:orientation="vertical" >
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="发送通知" />
</LinearLayout>activity_second.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:background="#0000FF"
android:orientation="vertical" >
</LinearLayout>源代码
MainActivity
package com.example.notification;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.View;
/**
*
* @author TXF
*
* Notification(通知)
*
* 手机屏幕的最上面显示通知,点击通知内容会跳转到另一个界面
*/
public class MainActivity extends Activity {
// 通知管理器
private NotificationManager mnm;
// 通知
private Notification mn;
// 意图
private Intent mintent;
// 模糊意图
private PendingIntent mpIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mnm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 点击通知时转移内容
mintent = new Intent(this, SecondActivity.class);
// 主要是设置点击通知时显示内容的类
mpIntent = PendingIntent.getActivity(this, 0, mintent, 0);
// 初始化Notifaction对象
mn = new Notification();
}
@SuppressWarnings("deprecation")
public void click(View view) {
switch (view.getId()) {
case R.id.btn1:
// 设置通知在状态栏显示的图标
mn.icon = R.drawable.ic_launcher;
// 当我们点击发送通知的时候弹出的内容
mn.tickerText = "通知";
// 通知的时候默认发出的声音
mn.defaults = Notification.DEFAULT_SOUND;
// 设置通知显示的参数 第一个参数是上下文,第二个参数是通知的标题,第三个参数是通知内容,第四个参数是模糊意图
mn.setLatestEventInfo(this, "消息", "你有一条新的消息", mpIntent);
// 执行这个通知,第一个参数为通知的Id,第二个为通知对像
mnm.notify(0, mn);
break;
}
}
}
SecondActivity
package com.example.notification;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
效果图
![](https://oscdn.geek-share.com/Uploads/Images/Content/201911/15/18af7dc38b69a98874b47ff18de5db92)
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"
android:orientation="vertical" >
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="发送通知" />
</LinearLayout>activity_second.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:background="#0000FF"
android:orientation="vertical" >
</LinearLayout>源代码
MainActivity
package com.example.notification;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.View;
/**
*
* @author TXF
*
* Notification(通知)
*
* 手机屏幕的最上面显示通知,点击通知内容会跳转到另一个界面
*/
public class MainActivity extends Activity {
// 通知管理器
private NotificationManager mnm;
// 通知
private Notification mn;
// 意图
private Intent mintent;
// 模糊意图
private PendingIntent mpIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mnm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 点击通知时转移内容
mintent = new Intent(this, SecondActivity.class);
// 主要是设置点击通知时显示内容的类
mpIntent = PendingIntent.getActivity(this, 0, mintent, 0);
// 初始化Notifaction对象
mn = new Notification();
}
@SuppressWarnings("deprecation")
public void click(View view) {
switch (view.getId()) {
case R.id.btn1:
// 设置通知在状态栏显示的图标
mn.icon = R.drawable.ic_launcher;
// 当我们点击发送通知的时候弹出的内容
mn.tickerText = "通知";
// 通知的时候默认发出的声音
mn.defaults = Notification.DEFAULT_SOUND;
// 设置通知显示的参数 第一个参数是上下文,第二个参数是通知的标题,第三个参数是通知内容,第四个参数是模糊意图
mn.setLatestEventInfo(this, "消息", "你有一条新的消息", mpIntent);
// 执行这个通知,第一个参数为通知的Id,第二个为通知对像
mnm.notify(0, mn);
break;
}
}
}
SecondActivity
package com.example.notification;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
效果图
相关文章推荐
- Android第四讲——常用的基本控件(三)Toast、Dialog、PopupWindows、Notification(通知)、ViewPager
- Android--通知之Notification详解
- Android Notification通知详解
- Android UI控件详解-Spinner(下拉列表)
- Android UI系统控件进阶(一)之列表控件ListView详解
- Android UI系统控件进阶(三)—自动完成文本控件AutoCompleteTextView详解及属性
- Android学习指南之十九:Android Notification(通知)详解
- Android中的Notification通知详解
- Android UI详解之Toast、Notification、SearchView
- 【Android】状态栏通知Notification、NotificationManager详解
- Android中Notification通知用法详解
- Android基础—Notification(状态栏通知)详解
- Android中通知的使用-----Notification详解
- Android UI控件详解-EditText(编辑框)
- Android Notification通知详解
- 【Android】状态栏通知Notification、NotificationManager详解
- 【Android】状态栏通知Notification、NotificationManager详解
- Android UI控件详解-Menu(菜单)
- Android UI控件详解-ImageSwitcher(图片切换器)
- Android Notification通知详解