|
32 | 32 |
|
33 | 33 | package com.smartdevicelink.util; |
34 | 34 |
|
| 35 | +import android.annotation.SuppressLint; |
| 36 | +import android.content.BroadcastReceiver; |
35 | 37 | import android.content.ComponentName; |
36 | 38 | import android.content.Context; |
37 | 39 | import android.content.Intent; |
@@ -392,4 +394,27 @@ public static void saveVehicleType(Context context, VehicleType vehicleType, Str |
392 | 394 | return null; |
393 | 395 | } |
394 | 396 | } |
| 397 | + |
| 398 | + /** |
| 399 | + * A helper method to handle adding flags to registering a run time broadcast receiver. |
| 400 | + * |
| 401 | + * @param context a context that will be used to register the receiver with |
| 402 | + * @param receiver the receiver that will be registered |
| 403 | + * @param filter the filter that will be use to filter intents sent to the broadcast receiver |
| 404 | + * @param flags any flags that should be used to register the receiver. In most cases this |
| 405 | + * will be {@link Context#RECEIVER_NOT_EXPORTED} or |
| 406 | + * {@link Context#RECEIVER_EXPORTED} |
| 407 | + * @see Context#registerReceiver(BroadcastReceiver, IntentFilter) |
| 408 | + * @see Context#registerReceiver(BroadcastReceiver, IntentFilter, int) |
| 409 | + */ |
| 410 | + @SuppressLint("UnspecifiedRegisterReceiverFlag") |
| 411 | + public static void registerReceiver(Context context, BroadcastReceiver receiver, IntentFilter filter, int flags) { |
| 412 | + if (context != null && receiver != null && filter != null) { |
| 413 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 414 | + context.registerReceiver(receiver, filter, flags); |
| 415 | + } else { |
| 416 | + context.registerReceiver(receiver, filter); |
| 417 | + } |
| 418 | + } |
| 419 | + } |
395 | 420 | } |
0 commit comments