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

55 篇文章 0 订阅
48 篇文章 0 订阅




关键代码:

简单的通知的实现。

@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














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百世修行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值