Notification的使用

1.普通的通知

package mobile.android.notification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;

public class Main extends Activity
{
	private NotificationManager mNotificationManager;

	public void onClick_ShowCommonNotification(View view)
	{
		Notification.Builder builder = new Notification.Builder(this)
				.setSmallIcon(R.drawable.smile).setContentTitle("笑脸通知")
				.setContentText("图标显示在左侧!");
		mNotificationManager.notify(1, builder.build());
	}

	public void onClick_ShowBigIconNotification(View view)
	{
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.smile);
		Notification.Builder builder = new Notification.Builder(this)
				.setSmallIcon(R.drawable.why).setLargeIcon(bitmap)
				.setContentTitle("大图标笑脸").setContentText("大图标放在左侧,小图标放在右下角!");

		mNotificationManager.notify(2, builder.build());

	}

	public void onClick_ShowBigPictureNotification(View view)
	{
		// 必须先清除大图Notification,否则再次显示时抛出异常
		mNotificationManager.cancel(3);
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.big);

		Notification notification = new Notification.BigPictureStyle(
				new Notification.Builder(this).setContentTitle("Big Picture")
						.setContentText("大图").setSmallIcon(R.drawable.wrath))
				.bigPicture(bitmap).build();

		mNotificationManager.notify(3, notification);

	}

	public void onClick_ShowInfoNotification(View view)
	{
		Notification.Builder builder1 = new Notification.Builder(this)
				.setSmallIcon(R.drawable.why).setContentTitle("Info")
				.setContentText("文本信息显示在右下角!").setContentInfo("信息");

		mNotificationManager.notify(4, builder1.build());

		// setContentInfo优先,有setContentInfo,setNumber不起作用
		Notification.Builder builder2 = new Notification.Builder(this)
				.setSmallIcon(R.drawable.wrath).setContentTitle("Number")
				.setContentText("数字显示在右下角!").setNumber(25);

		mNotificationManager.notify(5, builder2.build());

	}

	public void onClick_ShowProgressNotification(View view)
	{
		Notification.Builder builder1 = new Notification.Builder(this)
				.setSmallIcon(R.drawable.smile).setContentTitle("Determinate Progress")
				.setContentText("显示进度的进度条!").setProgress(100, 25, false);
		mNotificationManager.notify(6, builder1.build());
		Notification.Builder builder2 = new Notification.Builder(this)
		.setSmallIcon(R.drawable.why).setContentTitle("Indeterminate Progress")
		.setContentText("未显示进度的进度条!").setProgress(100, 25, true);
		mNotificationManager.notify(7, builder2.build());
	}
    public void onClick_ClearAllNotification(View view)
    {
    	mNotificationManager.cancelAll();
    }
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	}
}
2.自定义Notification

布局文件Notification.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:id="@+id/textview" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="自定义内容"
		android:textColor="#F00" android:textSize="20sp" android:gravity="center" />
	<ImageView android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:src="@drawable/smile"
		android:layout_gravity="center" />

		
</LinearLayout>
 
Java代码
package mobile.android.custom.notification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;

public class Main extends Activity
{
	private NotificationManager mNotificationManager;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
	}

	public void onClick_ShowNotification(View view)
	{
		
		RemoteViews remoteViews = new RemoteViews(getPackageName(),
				R.layout.notification);
		remoteViews.setTextViewText(R.id.textview, "更新自定义内容");

		Notification.Builder builder = new Notification.Builder(this)
				.setSmallIcon(R.drawable.smile).setContent(remoteViews);

		mNotificationManager.notify(1, builder.build());

	}
}

3.结合Pendingintent


    RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.layout_notification);

        Intent intent=new Intent(this,ExpandableListViewActivity.class);
        PendingIntent pendingIntent=PendingIntent.getActivity(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        Notification notification=new Notification.Builder(this).setContentTitle("通知")
                .setContentText("1021589")
                .setSmallIcon(R.drawable.wait)
                .setDefaults(Notification.DEFAULT_ALL)
                .setContent(remoteViews)
                .setContentIntent(pendingIntent)
                .build();
        manager.notify(1,notification);


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值