@cxm-2016
2016-12-15T09:19:20.000000Z
字数 4156
阅读 4485
Android
版本:1
作者:陈小默
声明:禁止商业,禁止转载
首先我们来演示一下最简单的通知发送方式
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)
首先,我们要先定义一个用于在通知栏展示的布局文件
<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被点击的时候打开浏览器。
假如我们向每点通知后更新其中的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")
}
}