Android studio的广播(静态注册)
1.发送广播
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/common"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="commonAction"
android:text="发送一条广播"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.297" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//这里被注释掉的部分是另一种监听实现方法
// Button button = findViewById(R.id.common);
// button.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// Log.d("TAG", "onClick: ");
// Intent intent = new Intent("meet");
// intent.setPackage(getPackageName());
// sendBroadcast(intent);
// }
// });
// }
}
//xml中通过绑定标签完成监听
public void commonAction(View view) {
Intent intent = new Intent();
intent.setAction("meet");
intent.setPackage("com.example.myapplication");
sendBroadcast(intent);
}
}
奉上监听事件实现方法的链接:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication" >
<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/Theme.MyApplication" >
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="meet"/>
</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>
MyReceiver.java
public class MyReceiver extends BroadcastReceiver {
private static final String ACTION="meet";
private static final String TAG=MyReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
// Log.d(TAG, "onReceive: ");
Toast.makeText(context, "广播接受到了", Toast.LENGTH_SHORT).show();
}
}
一定要注意里面一条语句:intent.setPackage("com.example.myapplication");
这个很重要,我在自学的时候发现很多视频都是只有三局,没有这句,他们可以运行,我捣鼓了一晚上,不知道为什么。大家都是人,为什么我的计算机难道配置不行。就很无语。书上有一句话:如果intent不设置package属性,将无法启动BroadcastReceiver.(问题解决了我才看到)
截图: