Notification的应用

今天碰到Notification 捣鼓了好久,写个笔记免得以后又要用。

先传两张效果图

刚开始做的是老版本的不能像酷狗一样把通知栏内容缩小,后来换成4.1后支持的功能,可以缩放。

这是xml文件:

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageview2"
            android:layout_width="wrap_content"
            android:layout_height="1dp"
            android:src="@drawable/backgro" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/imageview1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/pic" />

            <TextView
                android:id="@+id/textview1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
三个全局变量()
        private Notification notification;
	private NotificationManager mNotificationManager;
	private static final int CUSTOM_VIEW_ID = 1;
在oncreate()方法中

                //设置内容
                RemoteViews contentView = new RemoteViews(getPackageName(),
				R.layout.notification);
		contentView.setImageViewResource(R.id.imageview1, R.drawable.pic);
		contentView.setTextViewText(R.id.textview1, "通知內容 ");
                //使用Builder构造notification
                NotificationCompat.Builder builder = new NotificationCompat.Builder(
                this).setContent(contentView).setContentTitle("通知栏")
                .setContentText("点我进入").setSmallIcon(R.drawable.ic_launcher);
                notification = builder.build();
                //判断当前版本,只有16开始才可以用缩放
                if (Build.VERSION.SDK_INT >= 16) {
                   notification.bigContentView = bigContentView;//使用bigContentView进行缩放
                 } else {
                    notification.contentView = contentView;
                 }
        //发送通知
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(
                MainActivity.this, CUSTOM_VIEW_ID, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        notification.contentIntent = contentIntent;

        String ns = Context.NOTIFICATION_SERVICE;
        mNotificationManager = (NotificationManager) getSystemService(ns);
        mNotificationManager.notify(CUSTOM_VIEW_ID, notification);

在发送通知之前还可以做一些其他的处理比如

// 自定义提示音

		// SD卡内资源
		 String pathString = "/storage/sdcard1/1317.mp3";
		 File file = new File(pathString);
		 notification.sound = Uri.fromFile(file);

		// raw内资源
		notification.sound = Uri.parse("android.resource://" + getPackageName()
				+ "/" + R.raw.mm);

        // 使用系统通知音
        // notification.defaults=Notification.DEFAULT_SOUND;

//LED提示灯  
                notification.flags |= Notification.FLAG_SHOW_LIGHTS;
		notification.ledARGB = Color.BLUE;//根据设备不同可执行的颜色也不同
		notification.ledOnMS = 2000;//开启时间
		notification.ledOffMS = 2000;//关闭时间
		notification.defaults |= Notification.DEFAULT_LIGHTS;
//震动
              notification.defaults |= Notification.DEFAULT_VIBRATE; // 震动

// 声音一直响到用户相应,就是通知会一直响起,直到你触碰通知栏的时间就会停止(包括震动、提示音、LED闪烁)
        // notification.flags |= Notification.FLAG_INSISTENT;

最后再加入两个权限

LED闪烁<uses-permission android:name="android.permission.FLASHLIGHT" />
震动 <uses-permission android:name="android.permission.VIBRATE" />

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值