话不多说,直接看效果图和代码!
一、效果图
1、未发送广播之前,两个APP的主界面图; 2、发送之后,文本框内容改变。
二、代码
1、创建第一个APP (1)MainActivity中代码如下:
package com.example.study;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EatReceiver eatReceiver;
@SuppressLint("StaticFieldLeak")
static TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//文本框
textView = findViewById(R.id.textview);
}
public static class EatReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
textView.setText("芜湖");
}
}
}
注意这里的广播接收器前面的修饰符必须是:public static。
(2)静态注册“广播接收器”
<receiver android:name="com.example.study.MainActivity$EatReceiver"
tools:ignore="Instantiatable"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.WuHu"/>
</intent-filter>
</receiver>
这里需要注意的是: (1)接收器的名字格式是:“包名.类名$内部类名”; (2)android:exported=“true”:可以接收其他应用程序发送的广播;
(3)或者动态注册”广播接收器“
package com.example.study;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EatReceiver eatReceiver;
@SuppressLint("StaticFieldLeak")
static TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//动态注册广播接收器
IntentFilter intentFilter = new IntentFilter("android.intent.action.WuHu");
EatReceiver eatReceiver = new EatReceiver();
registerReceiver(eatReceiver,intentFilter);
//文本框
textView = findViewById(R.id.textview);
}
public static class EatReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
textView.setText("芜湖");
}
}
}
2、创建第二个APP (1)MainActivity中代码如下:
package com.example.study2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("android.intent.action.WuHu");
// ComponentName componentName = new ComponentName("com.example.study","com.example.study.MainActivity$EatReceiver");
// intent.setComponent(componentName);
intent.setPackage("com.example.study");
sendBroadcast(intent);
}
});
}
}
这里需要注意的是: 给intent指定广播接收器的位置(两种方式): (1)intent.setPackage():设置广播接收器所在的包名; (2)new ComponentName(“包名”,“包名.类名”) ?intent.setComponent(); ?设置广播接收器的包名和类名。
|