Broadcast Receiver
通过LoaclBroadcastManager发送广播并接收广播
点击broadactivity的click,跳转到broad2activity
点击broad2activity的clickme,发送广播
broadactivity接收广播,将abc改成123
broadactivity
broad2activity
点击clickme后返回到broadactivity
代码
broadactivity_xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<?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=".broad.broadActivity">
<Button
android:id="@+id/bcbt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="40dp"
android:textSize="30sp"
android:text="click"/>
<TextView
android:id="@+id/bctv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abc"
android:textSize="60sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bcbt1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintVertical_bias="0.3"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
|
broadactivity_java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package com.example.test.broad;
import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.test.R;
public class broadActivity extends AppCompatActivity {
private Button bcbt1;
private TextView bctv1;
private Mybroad mybroad;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_broad);
bcbt1 = findViewById(R.id.bcbt1);
bctv1 = findViewById(R.id.bctv1);
bcbt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(broadActivity.this , broad2Activity.class);
startActivity(intent);
}
});
mybroad = new Mybroad();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("sss");
LocalBroadcastManager.getInstance(broadActivity.this).registerReceiver(mybroad,intentFilter);
}
private class Mybroad extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//接收到广播要处理的事
switch(intent.getAction()){
case "sss":
bctv1.setText("123");
break;
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
LocalBroadcastManager.getInstance(broadActivity.this).unregisterReceiver(mybroad);
}
}
|
broad2activity_xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?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=".broad.broad2Activity">
<Button
android:id="@+id/bc2bt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="30dp"
android:textSize="30sp"
android:text="click me"/>
</androidx.constraintlayout.widget.ConstraintLayout>
|
broad2activity_java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package com.example.test.broad;
import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.test.R;
public class broad2Activity extends AppCompatActivity {
private Button bc2bt1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_broad2);
bc2bt1 = findViewById(R.id.bc2bt1);
bc2bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("sss");
LocalBroadcastManager.getInstance(broad2Activity.this).sendBroadcast(intent);
}
});
}
}
|
一些系统操作对应的action