检测何时恢复信元接收

时间:2013-12-22 10:51:59

标签: android android-broadcast

由于退出保险范围,开启飞机模式或出于任何其他原因,接收可能会丢失。我对这个原因不感兴趣,但我只是想知道接待恢复的时间。

是否有可以注册的意图来启动广播接收器?

使用android.net.conn.CONNECTIVITY_CHANGE的连接更改接收器似乎不能用于此目的。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您可以使用PhoneStateListener。您可以使用PHONE_STATE的接收器执行此操作。

在清单中:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

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

BroadcastReceiver:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

class PhoneStateReceiver extends BroadcastReceiver {
    TelephonyManager telephonyManager;

    public void onReceive(Context context, Intent intent) {
        ServiceStateListener phoneListener = new ServiceStateListener();
        telephonyManager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_SERVICE_STATE);
    }

    public void onDestroy() {
        telephonyManager.listen(null, PhoneStateListener.LISTEN_NONE);
    }

}

PhoneStateListener:

import android.telephony.PhoneStateListener;

class ServiceStateListener extends PhoneStateListener {
    public void onServiceStateChanged (ServiceState serviceState) {
        boolean connected = (serviceState.getState() == ServiceState.STATE_IN_SERVICE);
        // Do work based on "connected"
    }
}