使用RemoteViews自定义通知栏样式

使用RemoteViews自定义通知栏样式

标签(空格分隔): 菜鸟 android


一、普通通知栏

首先我们来演示一下最简单的通知发送方式

val Activity.notificationManager: NotificationManager
    get() = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

class MainActivity : AppCompatActivity() {
    private var viewHolder = MainViewHolder()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        viewHolder.bind(this)
        //send是一个button
        viewHolder.send?.setOnClickListener { view ->
            sendNotification()
        }
    }

    private fun sendNotification() {
        val manager = notificationManager
        val builder = NotificationCompat.Builder(this)

        builder.setContentTitle("title").setContentText("text").setSmallIcon(R.drawable.ic_launcher)
                .setDefaults(0)
                .setAutoCancel(true)
        val notification = builder.build()
        manager.notify(0, notification)
    }
}

处理点击事件

假如我们想要点击通知消息之后处理某些事件,比如打开浏览器之类的,这里我们选择演示点击通知后使用浏览器打开我的博客主页

        val intent = Intent()
        intent.action = "android.intent.action.VIEW"
        val uri = Uri.parse("http://blog.csdn.net/qq_32583189/")
        intent.data = uri
        val pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
        builder.setContentIntent(pIntent)

二、使用RemoteViews替换普通通知栏

首先,我们要先定义一个用于在通知栏展示的布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#56ac58"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:gravity="center"
        android:text="hello world" />
</LinearLayout>

以下为修改后的发送通知的代码(注意:所有类型的通知都必须设置小图标)

    private fun sendNotification() {
        val manager = notificationManager
        val builder = NotificationCompat.Builder(this)
        val view = RemoteViews(packageName, R.layout.notification_layout)

        builder.setSmallIcon(R.drawable.ic_launcher)
                .setDefaults(0)
                .setAutoCancel(true)
                .setContent(view)
        val notification = builder.build()
        manager.notify(0, notification)
    }

这是弹出的对话框的样子

处理点击事件

现在我们如何达到上述点击后打开浏览器的效果呢

private fun sendNotification() {
        val manager = notificationManager
        val builder = NotificationCompat.Builder(this)
        val view = RemoteViews(packageName, R.layout.notification_layout)

        builder.setSmallIcon(R.drawable.ic_launcher)
                .setDefaults(0)
                .setAutoCancel(true)
                .setContent(view)
        val intent = Intent()
        intent.action = "android.intent.action.VIEW"
        val uri = Uri.parse("http://blog.csdn.net/qq_32583189/")
        intent.data = uri
        val pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
        view.setOnClickPendingIntent(R.id.tv, pIntent)
        val notification = builder.build()
        manager.notify(0, notification)
    }

这里解释一下view.setOnClickPendingIntent(R.id.tv, pIntent)里面第一个参数的意思是该点击事件的接收控件,这里设置的是当TextView被点击的时候打开浏览器。

三、RemoteViews如何更新View

假如我们向每点通知后更新其中的UI应该怎么办?其实通知和我们的应用是两个不同的进程,也就是说通知和我们的进程的内存是不能共享的,而RemoteViews的作用就是用来跨进程发送View,所以我们不能通过直接操作通知栏的内存来修改View,只能通过RemoteViews提供的操作方法来更新

这是更新后的java代码,注意此时需要将配置文件中的启动模式设计为单例模式<activity android:launchMode="singleInstance">
下面代码所做的工作就是当点击TextView的使用重新显示当前Activity并调用onNewIntent方法,于是就产生了两种效果,一种是Activity已经退出,此时重新打开Activity并不会调用onNewIntent方法所以显示的字仍是Hello World,如果Activity没有退出,则会显示新字符串hello remoteViews

val Activity.notificationManager: NotificationManager
    get() = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

class MainActivity : AppCompatActivity() {
    private var viewHolder = MainViewHolder()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        viewHolder.bind(this)

        viewHolder.send?.setOnClickListener { view ->
            sendNotification("hello world")
        }
    }

    private fun sendNotification(message: String) {
        val manager = notificationManager
        val builder = NotificationCompat.Builder(this)
        val view = RemoteViews(packageName, R.layout.notification_layout)

        builder.setSmallIcon(R.drawable.ic_launcher)
                .setDefaults(0)
                .setAutoCancel(true)
                .setContent(view)

        val intent = Intent()
        intent.setClass(this, MainActivity::class.java)
        val pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
        view.setTextViewText(R.id.tv, message)
        view.setOnClickPendingIntent(R.id.tv, pIntent)
        val notification = builder.build()
        manager.notify(0, notification)
    }

    override fun onNewIntent(intent: Intent?) {
        sendNotification("hello remoteViews")
    }
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值