android学习记录(七)

android学习记录(七),第1张

概述动态注册监听时间变化.MainActivity.ktpackagecom.example.broadcasttestimportandroid.content.BroadcastReceiverimportandroid.content.Contextimportandroid.content.Intentimportandroid.content.IntentFilterimportandroidx.appcompat.app.AppCompatActi 动态注册监听时间变化.MainActivity.kt
package com.example.broadcasttestimport androID.content.broadcastReceiverimport androID.content.Contextimport androID.content.Intentimport androID.content.IntentFilterimport androIDx.appcompat.app.AppCompatActivityimport androID.os.Bundleimport androID.os.ResultReceiverimport androID.Widget.Toastclass MainActivity : AppCompatActivity() {    lateinit var  timeChangeReceiver: TimeChangeReceiver    overrIDe fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentVIEw(R.layout.activity_main)        val intentFilter=IntentFilter()        intentFilter.addAction("androID.intent.action.TIME_TICK")        timeChangeReceiver=TimeChangeReceiver()        registerReceiver(timeChangeReceiver,intentFilter)    }    overrIDe fun onDestroy() {        super.onDestroy()        unregisterReceiver(timeChangeReceiver)    }    inner  class TimeChangeReceiver : broadcastReceiver(){        overrIDe fun onReceive(context: Context?, intent: Intent?) {            Toast.makeText(context,"Time has changed" ,Toast.LENGTH_SHORT).show()        }    }}
静态注册实现开机启动BootCompIEteRecIEver.kt
package com.example.broadcasttestimport androID.content.broadcastReceiverimport androID.content.Contextimport androID.content.Intentimport androID.Widget.Toastclass BootCompIEteReceiver : broadcastReceiver() {    overrIDe fun onReceive(context: Context, intent: Intent) {        // This method is called when the broadcastReceiver is receiving an Intent broadcast.        Toast.makeText(context,"Boot Complete",Toast.LENGTH_SHORT).show()    }}
AndroIDManifest.xml
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"    package="com.example.broadcasttest">    <uses-permission androID:name="androID.permission.RECEIVE_BOOT_COMPLETED"/>    <application        androID:allowBackup="true"        androID:icon="@mipmap/ic_launcher"        androID:label="@string/app_name"        androID:roundIcon="@mipmap/ic_launcher_round"        androID:supportsRtl="true"        androID:theme="@style/theme.broadcastTest">        <receiver            androID:name=".BootCompIEteReceiver"            androID:enabled="true"            androID:exported="true">            <intent-filter>                <action androID:name="androID.intent.action.BOOT_COMPLETED"/>            </intent-filter>        </receiver>        <receiver            androID:name=".MybroadcastReceiver"            androID:enabled="true"            androID:exported="true">            <intent-filter>                <action androID:name="com.example.broadcasttest.MY_broADCAST" />            </intent-filter>        </receiver>        <activity androID:name=".MainActivity">            <intent-filter>                <action androID:name="androID.intent.action.MAIN" />                <category androID:name="androID.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>
发送标准广播先写一个接收广播的 MybroadcastReceiver
package com.example.broadcasttestimport androID.content.broadcastReceiverimport androID.content.Contextimport androID.content.Intentimport androID.Widget.Toastclass MybroadcastReceiver : broadcastReceiver() {    overrIDe fun onReceive(context: Context, intent: Intent) {        // This method is called when the broadcastReceiver is receiving an Intent broadcast.        Toast.makeText(context,"received in MybroadcastReceiver",Toast.LENGTH_SHORT).show()    }}

在AndroIDManifest.xml中添加相关配置.

  <intent-filter>                <action androID:name="com.example.broadcasttest.MY_broADCAST" />            </intent-filter>

用来接收广播.
接着在主界面处添加一个按钮用来触发广播.

<button        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:ID="@+ID/button"        androID:text="Send broadcast"/>

接着在MainActivity中添加相应的响应来发送广播.

package com.example.broadcasttestimport androID.content.broadcastReceiverimport androID.content.Contextimport androID.content.Intentimport androID.content.IntentFilterimport androIDx.appcompat.app.AppCompatActivityimport androID.os.Bundleimport androID.os.ResultReceiverimport androID.Widget.buttonimport androID.Widget.Toastclass MainActivity : AppCompatActivity() {    lateinit var  timeChangeReceiver: TimeChangeReceiver    lateinit var  button : button    overrIDe fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentVIEw(R.layout.activity_main)        val intentFilter=IntentFilter()        intentFilter.addAction("androID.intent.action.TIME_TICK")        timeChangeReceiver=TimeChangeReceiver()        registerReceiver(timeChangeReceiver,intentFilter)        button=findVIEwByID(R.ID.button)        button.setonClickListener {            val intent =Intent("com.example.broadcasttest.MY_broADCAST")            intent.setPackage(packagename)            sendbroadcast(intent)        }    }    overrIDe fun onDestroy() {        super.onDestroy()        unregisterReceiver(timeChangeReceiver)    }    inner  class TimeChangeReceiver : broadcastReceiver(){        overrIDe fun onReceive(context: Context?, intent: Intent?) {            Toast.makeText(context,"Time has changed" ,Toast.LENGTH_SHORT).show()        }    }}
发送有序广播.

另建一个Receiver

AnotherbroadcastReceiver
package com.example.broadcasttestimport androID.content.broadcastReceiverimport androID.content.Contextimport androID.content.Intentimport androID.Widget.Toastclass AnotherbroadcastReceiver : broadcastReceiver() {    overrIDe fun onReceive(context: Context, intent: Intent) {        // This method is called when the broadcastReceiver is receiving an Intent broadcast.        Toast.makeText(context,"recevIEd in AnotherbroadcastReceiver",Toast.LENGTH_SHORT).show()    }}

将广播发送方式换成有序广播.

button.setonClickListener {            val intent =Intent("com.example.broadcasttest.MY_broADCAST")            intent.setPackage(packagename)            sendOrderedbroadcast(intent,null)//第二个参数和权限有关.        }

接着为了显示先后关系可以在AndroIDManifest.xml中修改优先级.

<intent-filter androID:priority="100">                <action androID:name="com.example.broadcasttest.MY_broADCAST" />            </intent-filter>
总结

以上是内存溢出为你收集整理的android学习记录(七)全部内容,希望文章能够帮你解决android学习记录(七)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1034391.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存