Android UI控件详解-Notification(通知)

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);
	}
}
效果图


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值