Я пытаюсь написать простое приложение, которое ответило бы на повторный вызов ienter code, если при звонке подключено указанное устройство c bluetooth. Все работает. Я могу определить состояние звонка с помощью моего приемника, а также я могу обнаружить соединение с устройством BT. Но большую часть времени полученные намерения не достаточно быстры, чтобы вовремя инициировать действие. Иногда это работает, но в основном не срабатывает в нужное время.
. В следующем вопросе я прочитал, что добавление флага «FLAG_RECEIVER_FOREGROUND» сделает приемник быстрее обрабатывать события. Но я не мог понять, как мне установить этот флаг. Broadcast Receiver занимает слишком много времени для приема в onReceive () после выключения / включения режима полета
ниже указан код моего приемника:
> import android.bluetooth.BluetoothDevice; import
> android.content.BroadcastReceiver; import android.content.Context;
> import android.content.Intent; import
> android.content.SharedPreferences; import
> android.telecom.TelecomManager; import
> android.telephony.TelephonyManager; import android.util.Log;
>
>
> import static android.content.Context.MODE_PRIVATE;
>
> public class MyReceiver extends BroadcastReceiver {
>
> public static String TAG="MyReceiver";
> public Boolean ringing;
> public Boolean offhook=false;
>
> @Override
> public void onReceive(Context context, Intent intent) {
>
> if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
> String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
> Log.d(TAG, "PhoneStateReceiver**Call State=" + state);
> SharedPreferences.Editor editor = context.getSharedPreferences("MyPrefsFile", MODE_PRIVATE).edit();
> if (state.equals("RINGING")) {
> ringing = true;
> offhook = false;
> Log.d(TAG, "RING: " +String.valueOf(ringing));
> editor.putBoolean("ringing",ringing);
> editor.putBoolean("offhook",offhook);
> } else if (state.equals("IDLE")) {
> ringing = false;
> offhook = false;
> editor.putBoolean("ringing",ringing);
> editor.putBoolean("offhook",offhook);
> Log.d(TAG, "RING: " +String.valueOf(ringing));
> } else if (state.equals("OFFHOOK")) {
> ringing = false;
> offhook = true;
> editor.putBoolean("ringing",ringing);
> editor.putBoolean("offhook",offhook);
> Log.d(TAG, "RING: " +String.valueOf(ringing));
> }
> editor.apply();
> } else if (intent.getAction().equals("android.bluetooth.device.action.ACL_CONNECTED"))
> {
> SharedPreferences sharedPreferences = context.getSharedPreferences("MyPrefsFile", Context.MODE_PRIVATE);
> ringing= sharedPreferences.getBoolean("ringing", false);
> String action = intent.getAction();
> BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
> if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { Log.d(TAG, "RING: " +String.valueOf(ringing));
> if(ringing) {
> Log.d(TAG, "Dev connected: " + device.getName()+ " while ringing");
> if(device.getName().contains("earphone")) {
> TelecomManager tm = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
> Log.d(TAG, "answer now");
> if (tm == null) {
> Log.d(TAG, "tm null");
> // whether you want to handle this is up to you really
> throw new NullPointerException("tm == null");
> }
> tm.acceptRingingCall();
> }
> } else {
> Log.d(TAG, "Dev connected: " + device.getName());
> }
> }
> Log.d(TAG,"PhoneStateReceiver **BT Connected ");
> } else if (intent.getAction().equals("android.bluetooth.device.action.ACL_DISCONNECTED"))
> {
> SharedPreferences sharedPreferences = context.getSharedPreferences("MyPrefsFile", Context.MODE_PRIVATE);
> offhook= sharedPreferences.getBoolean("offhook", false);
> String action = intent.getAction();
> BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
> if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
> if(offhook) {
> Log.d(TAG, "Dev disconnected: " + device.getName()+ " while oncall");
> if(device.getName().contains("earphone")) {
> TelecomManager tm = (TelecomManager) context
> .getSystemService(Context.TELECOM_SERVICE);
> Log.d(TAG, "answer now");
> if (tm == null) {
> Log.d(TAG, "tm null");
> // whether you want to handle this is up to you really
> throw new NullPointerException("tm == null");
> }
>
> tm.endCall();
> }
> } else {
> Log.d(TAG, "Dev disconnected: " + device.getName());
> }
> }
> Log.d(TAG,"PhoneStateReceiver **BT DisConnected ");
> } else {
> Log.d(TAG,"PhoneStateReceiver **unexpected intent.action=" + intent.getAction());
> }
> }
>
>
>
> }