Android Kotlin 与屏幕共享数据 on/off intentfilter
Android Kotlin Sharing data with screen on/off intentfilter
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
val filter = IntentFilter()
filter.addAction(Intent.ACTION_SCREEN_ON)
filter.addAction(Intent.ACTION_SCREEN_OFF)
// Here I want to send some data to MyBroadcastReceiver like .putExtra but how with IntentFilter???
registerReceiver(MyBroadcastReceiver(), filter)
return START_STICKY
}
我需要使用 IntentFilter 发送一个变量,就像我们在屏幕 On/Off 发生时使用 intent.putExtra 所做的那样。
我曾尝试单独发送数据,但由于广播对象被杀死,它的所有变量都被重置。
class MyBroadcastReceiver : BroadcastReceiver() {
private var isRemoteOn: Boolean = false
private var pinNo = "-1"
override fun onReceive(context: Context, intent: Intent) {
if(intent.action.equals("is_remote_on")) {
var status = intent.extras?.get("status").toString()
isRemoteOn = if (status.equals("on")) true else false
Log.d("RemoteOnOff", isRemoteOn.toString())
}
else if(intent.action.equals("setPin")) {
Log.d("PinNo", pinNo.toString());
pinNo = intent.extras?.get("pinNo").toString();
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF) or intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.d("pinNo", pinNo.toString()) // This always -1 when Screen get on/off no matter what I set here
Log.d("isRemoteOn", isRemoteOn.toString()) // This always false when Screen get on/off even I have set it to true }
}
}
这是我尝试设置 isRemoteOn 和 pinNo 的方法
val broadcastIntent = Intent()
broadcastIntent.action = "setPin"
broadcastIntent.putExtra("pinNo", "5")
broadcastIntent.setClass(this, MyBroadcastReceiver::class.java)
val broadcastIntent2 = Intent()
broadcastIntent2.action = "is_remote_on"
broadcastIntent2.putExtra("status", "on")
broadcastIntent2.setClass(this, MyBroadcastReceiver::class.java)
Intent.ACTION_SCREEN_ON
和Intent.ACTION_SCREEN_OFF
将由Android系统发送,当BroadcastReceiver
触发回调[=13=时,您无法将信息添加到意图中].关于问题,我建议您将一些关于 pinNo 和 isRemoteOn 的信息保存到 SharedPreferences
。在 onReceive
里面你通过 SharedPreferences
得到它
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
val filter = IntentFilter()
filter.addAction(Intent.ACTION_SCREEN_ON)
filter.addAction(Intent.ACTION_SCREEN_OFF)
// Here I want to send some data to MyBroadcastReceiver like .putExtra but how with IntentFilter???
registerReceiver(MyBroadcastReceiver(), filter)
return START_STICKY
}
我需要使用 IntentFilter 发送一个变量,就像我们在屏幕 On/Off 发生时使用 intent.putExtra 所做的那样。 我曾尝试单独发送数据,但由于广播对象被杀死,它的所有变量都被重置。
class MyBroadcastReceiver : BroadcastReceiver() {
private var isRemoteOn: Boolean = false
private var pinNo = "-1"
override fun onReceive(context: Context, intent: Intent) {
if(intent.action.equals("is_remote_on")) {
var status = intent.extras?.get("status").toString()
isRemoteOn = if (status.equals("on")) true else false
Log.d("RemoteOnOff", isRemoteOn.toString())
}
else if(intent.action.equals("setPin")) {
Log.d("PinNo", pinNo.toString());
pinNo = intent.extras?.get("pinNo").toString();
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF) or intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.d("pinNo", pinNo.toString()) // This always -1 when Screen get on/off no matter what I set here
Log.d("isRemoteOn", isRemoteOn.toString()) // This always false when Screen get on/off even I have set it to true }
}
}
这是我尝试设置 isRemoteOn 和 pinNo 的方法
val broadcastIntent = Intent()
broadcastIntent.action = "setPin"
broadcastIntent.putExtra("pinNo", "5")
broadcastIntent.setClass(this, MyBroadcastReceiver::class.java)
val broadcastIntent2 = Intent()
broadcastIntent2.action = "is_remote_on"
broadcastIntent2.putExtra("status", "on")
broadcastIntent2.setClass(this, MyBroadcastReceiver::class.java)
Intent.ACTION_SCREEN_ON
和Intent.ACTION_SCREEN_OFF
将由Android系统发送,当BroadcastReceiver
触发回调[=13=时,您无法将信息添加到意图中].关于问题,我建议您将一些关于 pinNo 和 isRemoteOn 的信息保存到 SharedPreferences
。在 onReceive
里面你通过 SharedPreferences