android - Broadcastreceiver获取ServiceState信息

标签 android broadcastreceiver listener telephony connectivity

有谁知道在android中获取电话服务状态(IN_SERVICE、OUT_OF_SERVICE、EMERGENCY_ONLY、POWER_OFF)的方法。

我希望有一个广播接收器来识别更改,但我找不到任何东西。我知道有一个监听器,但我不确定如何在我的应用程序中使用它,因为它使用 WakefulIntentService(由 thecommonsguy 提供)作为服务运行。

有了电池电量(即 BATTERY_LOW、BATTERY_OKAY)之类的东西,这很容易,但我无法为电话服务更改制定类似的东西。

最佳答案

注册一个接收器
public static final String ACTION_SERVICE_STATE_CHANGED = "android.intent.action.SERVICE_STATE";

当你对你的接收器有兴趣时,只需使用下面来自 android 源的小技巧

public void onReceive(Context context, Intent intent) {
    int state = intent.getExtras().getInt("state");
    if(state == ServiceState.STATE_IN_SERVICE)
    {
        //Do whatever you want
    }
    }

检查服务状态类的来源 http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/telephony/ServiceState.java#ServiceState.setFromNotifierBundle%28android.os.Bundle%29

关于android - Broadcastreceiver获取ServiceState信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4846271/

相关文章:

c# - Shared Xamarin.Forms - 命名空间中不存在类型或命名空间名称 'App'

android - 如何在使用 AsyncTask 下载图像时显示默认图像?

android - 如何从 BroadcastReceiver 启动一项 Activity ?

android - 从 BroadcastReceiver 关闭正在运行的应用程序

Android - 在可绘制对象中获取形状颜色/实体

android - 带有 ionic 的 Cordova Google-analytics-plugin 在 Android 中不起作用

android - 哪一个更可取 : Rx-Subject or Android BroadcastReceiver

javascript - Dojo:删除所有元素监听器或具有单例监听器

java - 用于弥补 Java 中基于多态参数的运行时方法查找不足的模式?

database - 如何通过网络访问Oracle数据库?