|
| 1 | +package com.example.snippets; |
| 2 | + |
| 3 | +import android.content.BroadcastReceiver; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.Intent; |
| 6 | +import android.content.IntentFilter; |
| 7 | + |
| 8 | +import androidx.core.content.ContextCompat; |
| 9 | + |
| 10 | +import java.util.Objects; |
| 11 | + |
| 12 | +import javax.inject.Inject; |
| 13 | + |
| 14 | +import dagger.hilt.android.qualifiers.ApplicationContext; |
| 15 | + |
| 16 | +// Warning for reader: This file has the Java code snippets for completeness of the corresponding |
| 17 | +// documentation page, but these snippets are not part of the actual sample. Refer to the Kotlin |
| 18 | +// code for the actual sample. |
| 19 | +public class BroadcastReceiverJavaSnippets { |
| 20 | + |
| 21 | + // [START android_broadcast_receiver_2_class_java] |
| 22 | + public static class MyBroadcastReceiver extends BroadcastReceiver { |
| 23 | + |
| 24 | + @Inject |
| 25 | + DataRepository dataRepository; |
| 26 | + |
| 27 | + @Override |
| 28 | + public void onReceive(Context context, Intent intent) { |
| 29 | + if (Objects.equals(intent.getAction(), "com.example.snippets.ACTION_UPDATE_DATA")) { |
| 30 | + String data = intent.getStringExtra("com.example.snippets.DATA"); |
| 31 | + // Do something with the data, for example send it to a data repository: |
| 32 | + if (data != null) { dataRepository.updateData(data); } |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + // [END android_broadcast_receiver_2_class_java] |
| 37 | + |
| 38 | + /** @noinspection ConstantValue, unused */ |
| 39 | + public static class BroadcastReceiverViewModel { |
| 40 | + Context context; |
| 41 | + |
| 42 | + public BroadcastReceiverViewModel(@ApplicationContext Context context) { |
| 43 | + this.context = context; |
| 44 | + } |
| 45 | + |
| 46 | + // [START android_broadcast_receiver_3_definition_java] |
| 47 | + MyBroadcastReceiver myBroadcastReceiver = new MyBroadcastReceiver(); |
| 48 | + // [END android_broadcast_receiver_3_definition_java] |
| 49 | + |
| 50 | + public void registerBroadcastReceiver() { |
| 51 | + // [START android_broadcast_receiver_4_intent_filter_java] |
| 52 | + IntentFilter filter = new IntentFilter("com.example.snippets.ACTION_UPDATE_DATA"); |
| 53 | + // [END android_broadcast_receiver_4_intent_filter_java] |
| 54 | + // [START android_broadcast_receiver_5_exported_java] |
| 55 | + boolean listenToBroadcastsFromOtherApps = false; |
| 56 | + int receiverFlags = listenToBroadcastsFromOtherApps |
| 57 | + ? ContextCompat.RECEIVER_EXPORTED |
| 58 | + : ContextCompat.RECEIVER_NOT_EXPORTED; |
| 59 | + // [END android_broadcast_receiver_5_exported_java] |
| 60 | + |
| 61 | + // [START android_broadcast_receiver_6_register_java] |
| 62 | + ContextCompat.registerReceiver(context, myBroadcastReceiver, filter, receiverFlags); |
| 63 | + // [END android_broadcast_receiver_6_register_java] |
| 64 | + |
| 65 | + // [START android_broadcast_receiver_12_register_with_permission_java] |
| 66 | + ContextCompat.registerReceiver( |
| 67 | + context, myBroadcastReceiver, filter, |
| 68 | + android.Manifest.permission.ACCESS_COARSE_LOCATION, |
| 69 | + null, // scheduler that defines thread, null means run on main thread |
| 70 | + receiverFlags |
| 71 | + ); |
| 72 | + // [END android_broadcast_receiver_12_register_with_permission_java] |
| 73 | + } |
| 74 | + |
| 75 | + public void unregisterBroadcastReceiver() { |
| 76 | + context.unregisterReceiver(myBroadcastReceiver); |
| 77 | + } |
| 78 | + |
| 79 | + public void sendBroadcast(String newData, boolean usePermission) { |
| 80 | + if(usePermission) { |
| 81 | + // [START android_broadcast_receiver_8_send_java] |
| 82 | + Intent intent = new Intent("com.example.snippets.ACTION_UPDATE_DATA"); |
| 83 | + intent.putExtra("com.example.snippets.DATA", newData); |
| 84 | + intent.setPackage("com.example.snippets"); |
| 85 | + context.sendBroadcast(intent); |
| 86 | + // [END android_broadcast_receiver_8_send_java] |
| 87 | + } else { |
| 88 | + Intent intent = new Intent("com.example.snippets.ACTION_UPDATE_DATA"); |
| 89 | + intent.putExtra("com.example.snippets.DATA", newData); |
| 90 | + intent.setPackage("com.example.snippets"); |
| 91 | + // [START android_broadcast_receiver_send_9_with_permission_java] |
| 92 | + context.sendBroadcast(intent, android.Manifest.permission.ACCESS_COARSE_LOCATION); |
| 93 | + // [END android_broadcast_receiver_9_send_with_permission_java] |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
0 commit comments