2016-02-14 3 views
2

Я пытаюсь проверить PhoneStateListener используя Broadcast Receiver. Но я не могу этого сделать. Вывод журнала внутри класса приемника также не печатается в Logcat.PhoneStateListener не работает с использованием широковещательного приемника

Вот файл манифеста

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.suraj.phonelistener"> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
      </activity> 
      <receiver android:name=".PhoneStateBroadcastReceiver" android:enabled="true"> 
       <intent-filter> 
        <action android:name="android.intent.action.PHONE_STATE"/> 
       </intent-filter> 
      </receiver> 
    </application> 
</manifest> 

А вот PhoneStateBroadcastReceiver.java файл

package com.example.suraj.phonelistener; 

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

public class PhoneStateBroadcastReceiver extends BroadcastReceiver { 

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

     Log.d("DEBUG","########"); 
     // above line is also not printing in Logcat 

     TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
     telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE); 
    } 

    public class CustomPhoneStateListener extends PhoneStateListener { 

     //private static final String TAG = "PhoneStateChanged"; 
     Context context; //Context to make Toast if required 
     public CustomPhoneStateListener(Context context) { 
      super(); 
      this.context = context; 
     } 

     @Override 
     public void onCallStateChanged(int state, String incomingNumber) { 
      super.onCallStateChanged(state, incomingNumber); 
      switch (state) { 
       case TelephonyManager.CALL_STATE_IDLE: 
        //when Idle i.e no call 
        Log.d("DEBUG","IDLE"); 
        break; 
       case TelephonyManager.CALL_STATE_OFFHOOK: 
        //when Off hook i.e in call 
        //Make intent and start your service here 
        Log.d("DEBUG","OFFHOOK"); 
       case TelephonyManager.CALL_STATE_RINGING: 
        //when Ringing 
        Log.d("DEBUG","RINGING"); 
        break; 
       default: 
        break; 
      } 
     } 
    } 
} 

Что должно быть проблема?

ответ

2

Возможно, это связано с тем, что ваш целевой уровень API для приложения - 23, то есть android M (6.0). пользовательские разрешения разные в 6.0 Вы можете изменить свой уровень API с 23 до любой более низкой версии или следовать правилам android M. Здесь вы найдете link в разрешении для Android M