24 接受自定义的广播

1  发送广播的代码:  点击按钮发送广播


activity略

package com.itheima.sender;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 发送广播事件 (消息)
* @param view
*/
public void click(View view){
Intent intent = new Intent();
//自定义一个广播动作。
intent.setAction("com.itheima.sender.jiuminga");
//大吼一声 把消息发出去了。 无序广播
sendBroadcast(intent);
//发送有序广播
//sendOrderedBroadcast(intent, receiverPermission);
}
}

2  接受广播的应用

配置项目清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheima.police"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:theme="@android:style/Theme.Translucent"
            android:name="com.itheima.police.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="com.itheima.police.PoliceReceiver">
            <intent-filter >
                <action android:name="com.itheima.sender.jiuminga"/>
            </intent-filter>
        </receiver>
    </application>


</manifest>

activity略

代码

package com.itheima.police;


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


public class PoliceReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "我是警察,我收到了消息", 1).show();
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Kotlin语法实现自定义广播的发送和接收逻辑的示例代码和布局文件: 1. 布局文件 在您的布局文件中添加以下代码,它包括一个按钮和一个文本视图,用于发送广播和显示接收到的广播消息。 ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <Button android:id="@+id/send_broadcast_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send Broadcast" /> <TextView android:id="@+id/broadcast_message_textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="Broadcast message will be displayed here" /> </LinearLayout> ``` 2. 发送广播的代码 ```kotlin val intent = Intent("com.example.mybroadcast") intent.putExtra("message", "Hello from sender!") sendBroadcast(intent) ``` 这段代码创建了一个名为“com.example.mybroadcast”的Intent对象,并将一个消息字符串作为额外数据添加到Intent中。然后使用sendBroadcast()方法将广播发送给所有已注册接收器。 3. 接收广播的代码 ```kotlin class MyBroadcastReceiver : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { val message = intent?.getStringExtra("message") Toast.makeText(context, "Broadcast received: $message", Toast.LENGTH_SHORT).show() } } ``` 这段代码创建了一个名为“MyBroadcastReceiver”的广播接收器类,并实现了onReceive()方法以处理接收到的广播。在这个例子中,它从接收到的Intent中提取了消息字符串,并使用Toast显示了一条消息。 4. 注册广播接收器 ```kotlin val filter = IntentFilter("com.example.mybroadcast") val receiver = MyBroadcastReceiver() registerReceiver(receiver, filter) ``` 这段代码创建了一个名为“com.example.mybroadcast”的IntentFilter对象,以指定接收器应接收哪个广播。然后创建一个MyBroadcastReceiver对象,并使用registerReceiver()方法将其注册为接收器。 5. 取消注册广播接收器 ```kotlin unregisterReceiver(receiver) ``` 这段代码使用unregisterReceiver()方法取消注册接收器,以便在不再需要它时释放资源。 完整的示例代码如下: ```kotlin class MainActivity : AppCompatActivity() { private lateinit var sendBroadcastButton: Button private lateinit var broadcastMessageTextView: TextView private lateinit var receiver: MyBroadcastReceiver override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) sendBroadcastButton = findViewById(R.id.send_broadcast_button) broadcastMessageTextView = findViewById(R.id.broadcast_message_textview) sendBroadcastButton.setOnClickListener { val intent = Intent("com.example.mybroadcast") intent.putExtra("message", "Hello from sender!") sendBroadcast(intent) } val filter = IntentFilter("com.example.mybroadcast") receiver = MyBroadcastReceiver() registerReceiver(receiver, filter) } override fun onDestroy() { super.onDestroy() unregisterReceiver(receiver) } class MyBroadcastReceiver : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { val message = intent?.getStringExtra("message") Toast.makeText(context, "Broadcast received: $message", Toast.LENGTH_SHORT).show() } } } ``` 请注意,为了确保在不再需要时释放资源,我们在onDestroy()方法中取消注册广播接收器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值