Android 四大组件之一(broadcast receiver),移动端开发语言
import android.util.Log;
public class CustomReceiver extends BroadcastReceiver {
private static String TAG = CustomReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG,"CustomReceiver onReceive 广播接受者");
}
}
第三步:编写布局文件和 java 代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="静态注册广播,发送广播区域"
android:textSize="20dp"
android:textColor="@color/colorAccent"
android:layout_marginTop="100dp"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送广播"
android:onClick="sendAction2"/>
</LinearLayout>
并编写对应 java 代码
package com.wust.broadcast;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendAction2(View view){
/**
Description: 静态发送广播 的 接收者
@author: yiqi
@date: 2021/4/24 17:17
@param:[view]
@return:void
*/
Intent intent = new Intent();
//这里的 Action 必须与静态注册时保持一致
intent.setAction("com.wust.broadcast");
//这一句话是 android 8.0 以上的版本要求写的,因为谷歌鼓励动态注册广播接受者,所以这种方式作为了解,第一个参数是包名;第二个参数是广播接受者的类名
intent.setComponent(new ComponentName("com.wust.broadcast","com.wust.broadcast.CustomReceiver"));
sendBroadcast(intent);
}
}
动态注册接收广播
=========
注册步骤
第一步:创
建?CustomReceiver 类 继承?BroadcastReceiver
package com.wust.broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class dynamicReceiver extends BroadcastReceiver {
private static String TAG = dynamicReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG,"Dynamic Receiver 广播接受者");
}
}
第二步:java 代码 来 注册 刚刚的接受者即可(动态注册)
package com.wust.broadcast;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//动态注册
dynamicReceiver dynamicReceiver = new dynamicReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("dynamicReceiver"); //这个名字可以随便取 它是广播注册时 与 发送广播时 的 唯一标识,必须要保持一致
registerReceiver(dynamicReceiver,filter);
}
}
第三步:编写布局文件及 java 代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
评论