`

Android学习之Notification通知

阅读更多

NotificationActivity.java:

package com.demo.notification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;

import com.demo.broadcast.R;

/**  
 * @ClassName: NotificationActivity  
 * @Description: 通知测试demo
 * @author chenzheng
 * @date 2014-9-5 下午2:54:14  
 */
public class NotificationActivity extends Activity implements OnClickListener{
	
	private Button btn_show_intent_act, btn_show_custom;
	/** Notification构造器 */
	private NotificationCompat.Builder mBuilder;
	/** Notification的ID */
	private int notifyId = 100;
	/** Notification管理 */
	private NotificationManager mNotificationManager;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notification);
		
		initService();
		btn_show_intent_act = (Button) findViewById(R.id.btn_show_intent_act);
		btn_show_intent_act.setOnClickListener(this);
		btn_show_custom = (Button) findViewById(R.id.btn_show_custom);
		btn_show_custom.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_show_intent_act:
			showIntentActivityNotify();
			break;
		case R.id.btn_show_custom:
			showCustomNotify();
			break;
		default:
			break;
		}
	}
	
	/** 显示通知栏点击跳转到指定Activity */
	public void showIntentActivityNotify(){
		mBuilder = new NotificationCompat.Builder(this);
		mBuilder.setAutoCancel(true)//点击后让通知将消失  
				.setContentTitle("测试标题")
				.setContentText("点击跳转")
				.setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL))
				.setTicker("点我")
				.setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示
				.setPriority(Notification.PRIORITY_DEFAULT)//设置该通知优先级
				.setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
				.setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合:
				//Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission
				.setSmallIcon(R.drawable.ic_launcher);
		//点击的意图ACTION是跳转到Intent
		Intent resultIntent = new Intent(this, NotificationActivity.class);
		resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		mBuilder.setContentIntent(pendingIntent);
		mNotificationManager.notify(notifyId, mBuilder.build());
		
	}
	
	/** 显示自定义通知*/
	public void showCustomNotify(){
		//先设定RemoteViews
		RemoteViews view_custom = new RemoteViews(getPackageName(), R.layout.view_custom);
		//设置对应IMAGEVIEW的ID的资源图片
		view_custom.setImageViewResource(R.id.custom_icon, R.drawable.ic_launcher);
		view_custom.setTextViewText(R.id.tv_custom_title, "淘宝放价");
		view_custom.setTextViewText(R.id.tv_custom_content, "欢乐中秋,淘宝大放价,千万豪礼等你来拿。");
		mBuilder = new Builder(this);
		mBuilder.setContent(view_custom)
				.setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL))
				.setWhen(System.currentTimeMillis())// 通知产生的时间,会在通知信息里显示
				.setTicker("有新消息")
				.setPriority(Notification.PRIORITY_DEFAULT)// 设置该通知优先级
				.setOngoing(false)//不是正在进行的   true为正在进行  效果和.flag一样
				.setSmallIcon(R.drawable.ic_launcher);
		Notification notify = mBuilder.build();
		notify.contentView = view_custom;
		mNotificationManager.notify(notifyId, notify);
	}
	
	/**
	 * 初始化要用到的系统服务
	 */
	private void initService() {
		mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	}
	
	/**
	 * @获取默认的pendingIntent,为了防止2.3及以下版本报错
	 * @flags属性:  
	 * 在顶部常驻:Notification.FLAG_ONGOING_EVENT  
	 * 点击去除: Notification.FLAG_AUTO_CANCEL 
	 */
	public PendingIntent getDefalutIntent(int flags){
		PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, new Intent(), flags);
		return pendingIntent;
	}
}

 

 

notification.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_show_intent_act"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示通知,点击跳转到指定Activity" />
    
    <Button
        android:id="@+id/btn_show_custom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示自定义通知" />

</LinearLayout>

 

 

view_custom.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/custom_icon"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:layout_alignParentLeft="true"
        android:layout_margin="5dip"
        android:padding="5dip"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/custom_icon"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/tv_custom_title"
                style="@style/NotificationTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:text="title"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/tv_custom_time"
                style="@style/NotificationTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:text="16.49"
                android:textSize="12sp" />
        </RelativeLayout>

        <TextView
            android:id="@+id/tv_custom_content"
            style="@style/NotificationContent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dip"
            android:text="content"
            android:textSize="12sp" />
    </LinearLayout>

</RelativeLayout>

 

添加允许震动权限:

 

 

<uses-permission android:name="android.permission.VIBRATE" />

 

 

 

  • 大小: 17 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics