[英]PhoneStateListener not giving the number dialed / called
According to documentation, the PhoneStateListener is a class that handles the phone state change, the onCallStateChanged method should get 2 parameters, state (integer) and String -> phone number.根据文档,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");
}
}
The above code is my PhoneStateListener class, and I call it from a service:上面的代码是我的 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");
}
}
I start the service from my main activity however I never get the number (it's not NULL but it is an empty string).我从我的主要活动开始服务,但是我从来没有得到号码(它不是 NULL,但它是一个空字符串)。 I have seen examples of people using Broadcast receiver but I don't understand why the documentation states that the number should be available using PhoneStateListener.
我见过有人使用广播接收器的例子,但我不明白为什么文档声明该号码应该可以使用 PhoneStateListener。
Here is my manifest:这是我的清单:
<?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>
Here is what worked for me to get the dialed number / incoming number:以下是我获取拨打号码/来电号码的方法:
Here is my main activity:这是我的主要活动:
package com.eddieharari.shippit; package com.eddieharari.shippit;
import androidx.appcompat.app.AppCompatActivity;导入androidx.appcompat.app.AppCompatActivity; import android.Manifest;
进口 android.Manifest; import android.content.Context;
导入 android.content.Context; import android.content.Intent;
导入 android.content.Intent; import android.content.IntentFilter;
导入 android.content.IntentFilter; import android.content.pm.PackageManager;
导入 android.content.pm.PackageManager; import android.os.Bundle;
导入 android.os.Bundle;
import android.telephony.PhoneStateListener;导入 android.telephony.PhoneStateListener; import android.telephony.TelephonyManager;
导入 android.telephony.TelephonyManager; import android.util.Log;
导入 android.util.Log;
public class MainActivity extends AppCompatActivity {公共 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(); }
} }
Note that you need to ask permissions on the manifest and also on run time as you can see here... There is no need for a broadcast listener, only phonestate listener... here is my phone state listener class:请注意,您需要在清单和运行时询问权限,如您在此处看到的...不需要广播侦听器,只有 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.