如果内存不足,系统将杀死BroadcastReceiver

时间:2020-08-18 16:56:24

标签: android-studio broadcastreceiver

我正在创建一个监听电话事件的应用程序。但是,当系统内存不足时,它将破坏我的BroadcastReceiver。如何避免这种情况。如果不是,请告诉我如何再次启动BroadcastReceiver。感谢您的帮助

public class PhoneStateReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    try {
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                if(incomingNumber != null) {

                    Intent serviceWriteCallData = new Intent(context, WriteIncomCallService.class);
                    serviceWriteCallData.putExtra("phoneNumber", incomingNumber);
                    Log.d("TAG", "onReceiveNumber: " + incomingNumber);

                    WriteIncomCallService.enqueueWork(context, serviceWriteCallData);
                }
        }

        if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))) {
        }

        if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
            context.stopService(new Intent(context, AudioManagerService.class));
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}}

清单中的代码

<receiver android:name=".PhoneStateReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
            <action android:name="android.intent.action.PHONE_STATE"/>
        </intent-filter>
    </receiver>

0 个答案:

没有答案