0
내 목표는 휴대 전화가 절전 모드에 있더라도 수신 전화를 항상 감지하는 것입니다. 그러나 롤리팝에 이상한 행동이 발생했습니다.휴대 전화가 절전 모드에있을 때 수신 통화 용 브로드 캐스트 수신기가 롤리팝에서 작동하지 않는 이유
인한 문제 :
전화 모드로 대기에 방송 수신기의 onReceive() 메소드가 호출되지 않습니다 (> 10 분 동안 사용되지 않는다).
이것은 Kitkat, Marshmallow 및 Nougat에서 완벽하게 작동합니다. 어쩌면 누군가가 똑같은 문제를 겪었을 것입니까?
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<receiver android:name=".PhoneCallReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
수신기 :
public class PhoneCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
String nr = Utils.fixNumber(intent.getExtras().getString("android.intent.extra.PHONE_NUMBER"));
prefs.edit().putString("LastActiveNr", nr).apply();
} else {
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
int state = 0;
if (TelephonyManager.EXTRA_STATE_IDLE.equals(stateStr)) {
state = TelephonyManager.CALL_STATE_IDLE;
prefs.edit().putInt("state", state).apply();
} else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(stateStr)) {
state = TelephonyManager.CALL_STATE_OFFHOOK;
prefs.edit().putInt("state", state).apply();
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
state = TelephonyManager.CALL_STATE_RINGING;
prefs.edit().putInt("state", state).apply();
String nr = Utils.fixNumber(intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER));
prefs.edit().putString("LastActiveNr", nr).apply();
}
onCallStateChanged(context);
}
}
어쩌면 당신은 서비스 내부에서 onCallStateChanged (컨텍스트) 메소드를 호출하는 방법을 알고 있습니까? 구현 된 수신기 인터페이스가있는 서비스가 있습니다. 그리고 난 onCallStateChanged (컨텍스트) 서비스 클래스에서 메서드를 재정의했습니다. 어떻게하면 방송 수신기에서이 방법을 시작할 수 있습니까? –
이것은 다른 주제에 대한 질문이며 귀하가 무엇을 요구하고 있는지 잘 모르겠습니다. 어쩌면 코드로 다른 주제를 열면 도와 드리겠습니다. 일반적으로 여러분은'BroadcastReceiver' 안에'Listener'를 등록해야합니다. 그러면'mListener.onPhoneStateChange (isRinging);이라고 부를 수 있습니다. – Kristopher
그걸 알아 냈는데, 그 인터페이스를 상속받은 인스턴스 인 mListener를 등록해야했습니다. –