不会触发Android Nougat PhoneStateListener [英] Android Nougat PhoneStateListener is not triggered

查看:390
本文介绍了不会触发Android Nougat PhoneStateListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android(目标25)中,我有一个后台服务,在onCreate函数中,我初始化了一个电话状态侦听器。在Nougat之前的Android版本上,它可以正常工作,但在Nougat中,即使已授予权限,它也无法正常工作。

In Android (target 25) I have a background service and in onCreate function I have initialized a phone state listener. It works fine on Android versions that are before Nougat but in Nougat it doesn't work, even though the permissions are granted.

public class Service extends IntentService
{
    class PhoneListener extends PhoneStateListener
    {
       String TAG = getClass().getName();
       @Override
       public void onCallStateChanged(int state, String incomingNumber) 
       {
           super.onCallStateChanged(state, incomingNumber);
           switch (state)
           {
               case TelephonyManager.CALL_STATE_IDLE:
                Log.d(TAG,"IDLE" );
               break;
               case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.d(TAG,"OFFHOOK");
               break;
               case TelephonyManager.CALL_STATE_RINGING:
                Log.d(TAG,"RINGING");
               break;
           }
       }
   }

   public Service ()
   {
       super("ChatService");
   }
   public Service(String name)
   {
       super(name);
   }

   @Override
   public void onCreate()
   {
       super.onCreate();
       TelephonyManager tm = (TelephonyManager)getApplicationContext().getSystemService(TELEPHONY_SERVICE);
       PhoneListener listener = new PhoneListener();
       tm.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);
   }
}

我不知道问题是什么,它看起来电话管理器尚未注册,因此不会触发onCallStateChanged。
我的猜测之一是在Android M上引入的打ze功能,但是,即使未在工作中发现手机,此代码在Android 6上也能正常工作

I don't know what is the problem, it looks like the telephony manager is not registered and therefore the onCallStateChanged is not triggered. One of my guesses is the Doze feature that was introduced on Android M, but still.. this code works fine on Android 6 even when the phone is not found "in work"

推荐答案

因此,对于那些遇到相同问题的人,我找到了解决方案。
您不需要为 PhoneState 注册 BroadcastReceiver

So for those who encountered the same problem I found a solution. You don't need to register a BroadcastReceiver for PhoneState.

因此,我没有注册电话管理器以在服务的 onCreate 方法中进行监听,而是在 onStartCommand ,它开始工作。

So instead of registere the telephony manager to listen inside the onCreate method (of the service), I set it in onStartCommand and it started to work.

,但是请注意,在任何情况下,都会触发 onStartCommand ,它将注册电话管理器,因此

but notice that in any case that onStartCommand will be triggered it will register the telephony manager, so be sure to register it only once.

对于我来说,由于在服务类内部,我有内部类 PhoneListener ,因此我创建了一个类成员并将其初始化为null,然后在onStartCommand中检查它是否为null,然后创建并注册电话管理器,但是您可以使用singelton方式。

In my case, since inside the service class I have the Inner class PhoneListener I created a class member and initialized it to null, and In the onStartCommand I have checked if it's null and then created and registered the telephony manager, but you can use the singelton way.

是我的代码也可以在Android Nougat中使用:

So this is my code that worked also in Android Nougat:

public class Service extends IntentService
{
    class PhoneListener extends PhoneStateListener
    {
       String TAG = getClass().getName();
       @Override
       public void onCallStateChanged(int state, String incomingNumber) 
       {
           super.onCallStateChanged(state, incomingNumber);
           switch (state)
           {
               case TelephonyManager.CALL_STATE_IDLE:
                  Log.d(TAG,"IDLE" );
               break;
               case TelephonyManager.CALL_STATE_OFFHOOK:
                   Log.d(TAG,"OFFHOOK");
               break;
               case TelephonyManager.CALL_STATE_RINGING:
                   Log.d(TAG,"RINGING");
               break;
           }
     }
     PhoneListener phoneListener = null;

     public Service ()
     {
       super("ChatService");
     }

     @Override
     public void onCreate()
     {
        super.onCreate();
        // do what you need here
     }

     @Override
     public int onStartCommand (Intent intent, int flags, int startId)
     {
        if (phoneListener == null)
        {
          TelephonyManager tm = (TelephonyManager)getApplicationContext().getSystemService(TELEPHONY_SERVICE);
          phoneListener = new PhoneListener();
          tm.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);
        }
        // do what you need to do here

        return START_STICKY; // you can set it as you want
     } 
 }

这篇关于不会触发Android Nougat PhoneStateListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆