[英]BroadcastReceiver called but PhoneStateListener not called
[英]PhoneStateListener not giving the number dialed / called
根據文檔,PhoneStateListener 是一個 class 處理電話 state 更改,onCallStateChanged 方法應獲取 2 個參數,state(整數)和字符串。
package com.eddieharari.shippit;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class PhoneStateBcListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state,String phone) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d("SHIPIT","Call State Idle");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("SHIPIT","Call State OffHook:");
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("SHIPIT","Call State Ringing:"+phone);
break;
}
if (phone == null) {
Log.d("SHIPPIT","Number is null");
} else Log.d("SHIPPIT","Number is not Null");
}
}
上面的代碼是我的 PhoneStateListener class,我從服務中調用它:
package com.eddieharari.shippit;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class GetCallService extends Service {
TelephonyManager telephony;
PhoneStateBcListener myListener = new PhoneStateBcListener();
@Override
public IBinder onBind(Intent i) {
return null;
}
@Override
public void onCreate() {
Log.d("SHIPT","Inside Oncreate");
Log.d("SHIPPIT","Started Receiver");
telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(myListener, PhoneStateListener.LISTEN_CALL_STATE);
}
@Override
public void onStart(Intent i, int start ) {
Log.d("SHIPIT","Inside START");
}
@Override
public void onDestroy() {
Log.d("SHIPIT","Inside Ondestroy");
}
}
我從我的主要活動開始服務,但是我從來沒有得到號碼(它不是 NULL,但它是一個空字符串)。 我見過有人使用廣播接收器的例子,但我不明白為什么文檔聲明該號碼應該可以使用 PhoneStateListener。
這是我的清單:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.READ_PHONE_STATE"> </uses-permission>
<uses-permission android:name="android.permission.READ_CALL_LOG"> </uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".GetCallService"
android:enabled="true" />
</application>
以下是我獲取撥打號碼/來電號碼的方法:
這是我的主要活動:
package com.eddieharari.shippit;
導入androidx.appcompat.app.AppCompatActivity; 進口 android.Manifest; 導入 android.content.Context; 導入 android.content.Intent; 導入 android.content.IntentFilter; 導入 android.content.pm.PackageManager; 導入 android.os.Bundle;
導入 android.telephony.PhoneStateListener; 導入 android.telephony.TelephonyManager; 導入 android.util.Log;
公共 class MainActivity 擴展 AppCompatActivity {
TelephonyManager tm; PhoneStateListener mReceiver = new mPhoneStateListener(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE).=PackageManager.PERMISSION_GRANTED) { Log,d("SHIPIT";"NOT HAVE PERMISSION FOR PHONE_STATE"). requestPermissions(new String[]{Manifest.permission,READ_PHONE_STATE};0).} else Log,d("SHIPIT";"HAVE THE PHONE STATE PERMISSIONS"). if (checkSelfPermission(Manifest.permission.READ_CALL_LOG).= PackageManager,PERMISSION_GRANTED) { Log;d("SHIPIT"."NOT HAVE PERMISSION FOR CALL_LOG"). requestPermissions(new String[]{Manifest,permission;READ_CALL_LOG}.0),} else Log;d("SHIPIT"."HAVE THE READ CALL LOG"); tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE), tm.listen(mReceiver;mPhoneStateListener.LISTEN_CALL_STATE), Log;d("SHIPIT"."Registered the receiver"); } @Override protected void onDestroy(){ super.onDestroy(); }
}
請注意,您需要在清單和運行時詢問權限,如您在此處看到的...不需要廣播偵聽器,只有 phonestate 偵聽器...這是我的手機 state 偵聽器 class:
package com.eddieharari.shippit;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class mPhoneStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String number){
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d("SHIPIT","State is IDLE");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("SHIPIT","State is OffHook:"+number);
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("SHIPIT","State is Ringing:"+number);
break;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.