Android studio 静态广播发送和接收
基础代码
1.创建自定义广播类MyBroadCastReceiver,重写 onReceive(Context context, Intent intent) {//在这里收到广播} 方法,在
2. 在清单文件AndroidManifest.xml中注册静态广播类
<!--receiver广播接收器,name="包名",广播过滤器intent-filter,action触发的广播名称,-->
<!--可以是系统的广播,需要注册权限,可以是自定义的-->
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.COUSTBROAD" />
</intent-filter>
</receiver>
- 在MainActivity类中
Intent intent = new Intent(“com.example.COUSTBROAD”);这里指定了广播的接收者
intent.setPackage(getPackageName());//android8.0以上版本需要指定包名
sendBroadcast(intent);发送广播。
例子
<!--AndroidManifest.xml-->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity2"
android:exported="false" />
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.COUSTBROAD" />
</intent-filter>
<intent-filter>
<action android:name="my_to_my" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<!--activity_main.xml-->
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
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:id="@+id/main"
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:layout_marginLeft="30dp"
android:layout_marginTop="50dp"
android:text="静态广播,传输对象"
android:textColor="#ff00"
android:textSize="30dp"
android:id="@+id/txt_view"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="150dp"
android:layout_height="60dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="starsend"
android:textSize="20dp"
android:id="@+id/starsend_bt"/>
</LinearLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
//自定义MyBroadcastReceiver类,广播接收器
package com.example.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
//广播接收者onReceive
private static final String TAG = MyBroadcastReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.e("wks","广播接收者");
if (intent.getAction().equals("com.example.COUSTBROAD")){
if (intent.hasExtra("key")){
String value = intent.getStringExtra("key");
Log.v("wks","key:"+value);
Toast.makeText(context, value, Toast.LENGTH_SHORT).show();
Intent broadIntent = new Intent("my_to_my");
broadIntent.setPackage(context.getPackageName());
context.sendBroadcast(broadIntent);
}
}if (intent.getAction().equals("my_to_my")){
Log.e("wks","收到广播后自己再发送一次广播然后再跳转页面");
//用广播的方法跳转页面
Intent toMain2Intent = new Intent(context,MainActivity2.class);
//需要设置程序入口权限
toMain2Intent.setAction("android.intent.action.MAIN");
toMain2Intent.addCategory("android.intent.category.LAUNCHER");
toMain2Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//启动指定的页面
context.startActivity(toMain2Intent);
}
}
}
//MainActivity
package com.example.myapplication;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_send;
private TextView txtView;
private MyBroadcastReceiver localReciver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
//自定义初始化方法
public void init() {
btn_send = (Button) findViewById(R.id.starsend_bt);
btn_send.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//看网上很多人用switch方法获取按钮的点击id,新版本api已经弃用,所以使用if方法
if (v.getId()==R.id.starsend_bt){
Intent intent = new Intent("com.example.COUSTBROAD");
Log.d("wks","广播已发送");
Toast.makeText(this, "广播已发送", Toast.LENGTH_SHORT).show();
intent.setPackage(getPackageName());//android8.0以上版本需要指定包名
intent.putExtra("key","我是洞幺,收到请回答");
//发送静态广播
sendBroadcast(intent);
}
}
}
附图片