-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBroadcast-send-and-receiv.java
51 lines (40 loc) · 1.44 KB
/
Broadcast-send-and-receiv.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
package android.example.brodcast1;
import androidx.appcompat.app.AppCompatActivity;
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.TextView;
public class MainActivity extends AppCompatActivity {
TextView Ausgabe_Hello_World;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Ausgabe_Hello_World = findViewById(R.id.textView_hello_world);
}
public void onClick_Senden(View view) {
Intent intent = new Intent("ch.nicolassauter.BROTCAST_BEISPIEL");
intent.putExtra("Nachricht", "Von mir gesendete Nachricht");
sendBroadcast(intent);
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
Ausgabe_Hello_World.setText(intent.getStringExtra("Nachricht"));
}
};
@Override
protected void onStart() {
super.onStart();
IntentFilter intentFilter = new IntentFilter("ch.nicolassauter.BROTCAST_BEISPIEL");
registerReceiver(broadcastReceiver, intentFilter);
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(broadcastReceiver);
}
}