Notification.Builder 实现基本通知和自定义view通知

简介: 关键代码: 简单的通知的实现。 @SuppressLint("NewApi") private void showNotification() { Notification.Builder builder = new Builder(this); // new Notification.Builder(this) builder.setContentTitl




关键代码:

简单的通知的实现。

@SuppressLint("NewApi")
	private void showNotification() {
		Notification.Builder builder = new Builder(this);
		// new Notification.Builder(this)
		builder.setContentTitle("New mail from " + "sender.toString()")
				.setContentText("subject").setSmallIcon(R.drawable.ic_launcher)
				.build();
		// .setLargeIcon(aBitmap)

		Intent resultIntent = new Intent(this, MyOtherActivity.class);
		Bundle bundle = new Bundle();
		bundle.putInt("tag", 100);
		resultIntent.putExtras(bundle);
		// Because clicking the notification opens a new ("special") activity,
		// there's
		// no need to create an artificial back stack.
		PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
				resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

		builder.setContentIntent(resultPendingIntent);

		// Sets an ID for the notification
		int mNotificationId = 100;
		// Gets an instance of the NotificationManager service
		NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		// Builds the notification and issues it.
		mNotifyMgr.notify(mNotificationId, builder.build());
	}

自定义view的通知:


@SuppressLint("NewApi")
	private void initNotification() {
		Notification.Builder builder = new Builder(this);
		// new Notification.Builder(this)
		builder.setContentTitle("New mail from " + "sender.toString()")
				.setContentText("subject").setSmallIcon(R.drawable.ic_launcher)
				.build();
		// .setLargeIcon(aBitmap)

		Intent resultIntent = new Intent(this, MyOtherActivity.class);
		Bundle bundle = new Bundle();
		bundle.putInt("tag", 100);
		resultIntent.putExtras(bundle);
		// Because clicking the notification opens a new ("special") activity,
		// there's
		// no need to create an artificial back stack.
		PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
				resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

		// 封装自定义的布局
		RemoteViews mRemoteViews = new RemoteViews(this.getPackageName(),
				R.layout.notification_bar);

		// 如果是动态设置值的时候,需要这些操作
		mRemoteViews
				.setImageViewResource(R.id.img_user, R.drawable.ic_launcher);
		mRemoteViews.setTextViewText(R.id.tv, "XXX");
		mRemoteViews.setProgressBar(R.id.progressBar, 100, 50, false);

		// 点击ImageView时跳转
		mRemoteViews
				.setOnClickPendingIntent(R.id.img_user, resultPendingIntent);

		// builder.setContentIntent(resultPendingIntent);
		builder.setContent(mRemoteViews);

		// Sets an ID for the notification
		int mNotificationId = 100;
		// Gets an instance of the NotificationManager service
		NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		// Builds the notification and issues it.
		mNotifyMgr.notify(mNotificationId, builder.build());
	}

<RelativeLayout 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" >

    <ImageView
        android:id="@+id/img_user"
        android:layout_width="80dip"
        android:layout_height="80dip"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignTop="@+id/img_user"
        android:layout_marginLeft="10dip"
        android:layout_marginTop="10dip"
        android:layout_toLeftOf="@+id/btn"
        android:layout_toRightOf="@+id/img_user"
        android:text="自定义通知栏样式"
        android:textSize="15sp" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignTop="@+id/img_user"
        android:text="查看详情" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/img_user"
        android:layout_margin="10dip"
        android:layout_toRightOf="@+id/img_user"/>

</RelativeLayout>



通知跳转的 activity、

package com.example.mynotificationintentput;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class MyOtherActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		int tagId = bundle.getInt("tag");
		NotificationManager nm = (NotificationManager) this
				.getSystemService(Context.NOTIFICATION_SERVICE);
		
		Log.e("OtherActivity", "tag = tagId = "+tagId);
		nm.cancel(tagId);
		
	}
	
//	@Override
//	protected void onNewIntent(Intent intent) {
//		// TODO Auto-generated method stub
//		super.onNewIntent(intent);
//		
//		 setIntent(intent);
//	}
}


下载地址:http://download.csdn.net/detail/flyingsir_zw/9527940














目录
相关文章
|
API Android开发
Notification(状态栏通知)详解
Android中用于在状态栏显示通知信息的控件:Notification,相信大部分学Android都对他都很熟悉,而网上很多关于Notification的使用教程都是基于2.x的,而现在普遍的Android设备基本都在4.x以上,甚至是5.0以上的都有;他们各自的Notification都是不一样的!而本节给大家讲解的是基于4.x以上的Notification。
173 0
|
数据可视化 C# 开发工具
C#或Winform中的消息通知之系统本地通知(local toast notification)
C#应用通过 Microsoft.Toolkit.Uwp.Notifications NuGet包可以很方便的发送本地通知,适用于所有类型的应用(WPF、UWP、WinForms、控制台)
749 0
C#或Winform中的消息通知之系统本地通知(local toast notification)
|
Android开发
通知(Notification)
创建通知之前需要对android版本进行一个判断 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 1 activity_main.xml代码里仅有一个Button用于响应通知,代码不再展示
218 0
通知(Notification)