自定义Notification,Notificaton的使用,

如图:一个是自定义的,一个是调用系统的



自定义Notification:

	public void locationNotification(Class<?> cls, int requestCode, String tickerText,
			CharSequence contentTitle, CharSequence contentText) {
		NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification();
		// 设置statusbar显示的icon
		notification.icon = R.drawable.icon;
		// 设置statusbar显示的文字信息
		// myNoti.tickerText= new_msg ;
		notification.flags = Notification.FLAG_AUTO_CANCEL;
		// 设置notification发生时同时发出默认声音
		notification.defaults = Notification.DEFAULT_SOUND;
		RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.location_notification);
		Bitmap bitmap = null;
		// 此处是关键地方,可以从网络或是sdcard上获取图片,转成bitmap就可以
		bitmap = drawableToBitmap(this.getResources().getDrawable(R.drawable.icon));
		contentView.setImageViewBitmap(R.id.notification_icon, bitmap);
		contentView.setTextViewText(R.id.app_name, contentTitle);
		contentView.setTextViewText(R.id.notification_content, contentText);

		final Calendar mCalendar = Calendar.getInstance();
		int mHour = mCalendar.get(Calendar.HOUR_OF_DAY);
		int mMinuts = mCalendar.get(Calendar.MINUTE);
		contentView.setTextViewText(R.id.notification_time, mHour + ":" + mMinuts);// 当前时间

		notification.contentView = contentView;
		Intent intent = new Intent(this, cls);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);
		notification.contentIntent = contentIntent;

		// 显示Notification
		Random random = new Random(new Date().getTime());
		manager.notify(random.nextInt(1000000), notification);
	}
布局代码:

<?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="wrap_content"
    android:background="#EE171717"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/notification_icon"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:layout_gravity="center_vertical|left"
        android:layout_margin="5dp"
        android:src="@drawable/icon" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:layout_marginLeft="5dp"
        android:gravity="center_vertical"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/app_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="notification 图片改变 "
            android:textColor="#FFFFFFFF"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/notification_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=".... "
            android:textSize="14sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/notification_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="5dp"
        android:text="11:26" />

</LinearLayout>

使用系统自带的:

private void myNotification(Class<?> cls, String tickerText, CharSequence contentTitle,
			CharSequence contentText) {
		NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification();
		Intent intent = new Intent(this, cls);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		PendingIntent pandingIntent = PendingIntent.getActivity(this, 1, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);
		notification.flags = Notification.FLAG_AUTO_CANCEL;
		notification.defaults = Notification.DEFAULT_SOUND;
		notification.icon = R.drawable.ic_launcher;// 图标
		notification.tickerText = tickerText;
		notification.setLatestEventInfo(this, contentTitle, contentText, pandingIntent);
		manager.notify(1, notification);
	}

项目下载: http://download.csdn.net/detail/u010411826/8293525

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值