跳至主要内容

新建顺序广播

MainActivity.kt---->

package com.example.fredric.orderedbroadcast

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button

class MainActivity : AppCompatActivity() {

    var sendBroadCastBtn: Button? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        sendBroadCastBtn = findViewById<Button>(R.id.send_broadcast_btn)
        sendBroadCastBtn?.setOnClickListener({
            var intent = Intent("com.example.broadcasttest.MY_BROADCAST")
            sendOrderedBroadcast(intent,null)
        })
    }
}



MyBroadcastReceiver.kt --->

package com.example.fredric.orderedbroadcast

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.widget.Toast

class 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()
        abortBroadcast()
    }
}



AnotherBroadcastReceiver.kt --->

package com.example.fredric.orderedbroadcast

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.widget.Toast

class AnotherBroadcastReceiver : 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 another broadcast receiver!!",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.fredric.orderedbroadcast">

    <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/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".MyBroadcastReceiver"
            android:enabled="true"
            android:exported="true">

            <intent-filter android:priority="100">
                <action android:name="com.example.broadcasttest.MY_BROADCAST"></action>
            </intent-filter>
        </receiver>


        <receiver
            android:name=".AnotherBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.broadcasttest.MY_BROADCAST"></action>
            </intent-filter>
        </receiver>
    </application>

</manifest>


activity_main.xml --->

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

    <Button
        android:id="@+id/send_broadcast_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="Send Broadcast" />
</LinearLayout>

评论

此博客中的热门博文

自定义BroadcastReceiver

MainActivity.kt package com.example.fredric.mybroadcast import android.content.Intent import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.widget.Button class MainActivity : AppCompatActivity() { var sendBroadCastBtn : Button? = null override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout. activity_main ) sendBroadCastBtn = findViewById <Button> (R.id. send_broadcast_btn ) sendBroadCastBtn ?.setOnClickListener( { _ -> var intent = Intent( "com.example.broadcasttest.MY_BROADCAST" ) sendBroadcast(intent) } ) } } MyBroadcastReceiver.kt package com.example.fredric.mybroadcast import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.widget.Toast class MyBroadcastReceiver : BroadcastReceiver() { override fun onReceive(context...

Android使用LitePal操作SQLite数据库(Kotlin)

MainActivity.kt----> package com.example.fredric.litepaltest import android.content.ContentValues import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.util.Log import android.widget.Button import com.example.fredric.litepaltest.bean.Book import org.litepal.LitePal class MainActivity : AppCompatActivity() {     override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)         var createDatabase = findViewById<Button>(R.id.create_database)         createDatabase.setOnClickListener{             LitePal.getDatabase()         }         var addData = findViewById<Button>(R.id.add_data)         addData.setOnClickListener{           ...

LocalBroadcast本地广播创建

MainActivity.kt ---> package com.example.fredric.localbroadcastreceiver import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.IntentFilter import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.support.v4.content.LocalBroadcastManager import android.widget.Button import android.widget.Toast class MainActivity : AppCompatActivity() {     var intentFilter: IntentFilter? = null     var localReceiver: LocalReceiver? = null     var localBroadcastManager: LocalBroadcastManager? = null     var sendLocalBroadcastBtn: Button? = null     override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)         localBroadcastManager = LocalBroadcastManager.getInstance(this)   ...