清单XML:
<uses-permission android:name="android.permission.INTERNET" />
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LeadActivity"
android:label="@string/title_activity_lead"
android:theme="@style/AppTheme" />
</application>
这是我的phonestatelistener代码:
private class CallStateListener extends PhoneStateListener {
LeadActivity la = new LeadActivity();
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// Is called whenever there is a change in call state
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
phone_number=incomingNumber;
Log.d("Hub","CallStateListener Ringing IncomingNumber:"+ incomingNumber);
la.setContactNo(phone_number);
checkIt = 1;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
checkIt = 0;
break;
case TelephonyManager.CALL_STATE_IDLE:
if (checkIt == 1) {
Log.d("Hub","Call Disconnected:"+ incomingNumber);
Intent i = new Intent(getBaseContext(),LeadActivity.class);
// la.setContactNo(phone_number);
getBaseContext().startActivity(i); // service that sends the SMS
}
break;
}
}
}
这是我的主要活动代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Hub","Main Activity Started");
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)!= PackageManager.PERMISSION_GRANTED)
{
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_PHONE_STATE))
{
Log.d("ConvergeHub","Already permitted");
}
else
{
Log.d("ConvergeHub","Need permission");
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_PHONE_STATE},MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}
}
if(MY_PERMISSIONS_REQUEST_READ_CONTACTS==PackageManager.PERMISSION_GRANTED)
{
Log.d("ConvergeHub","PERMISSION_GRANTED");
}
else
{
Log.d("ConvergeHub","PERMISSION_DENIED");
}
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(new MyAppWebViewClient());
mWebView.loadUrl("https://xxxx.xxxxxx.com/");
}
日志像这样打印
07-01 18:58:06.962 6876-6876 / com.xxx.xxx D /集线器:CallStateListener振铃传入号码:
07-01 18:58:16.921 6876-6876 / com.xxx.xxx D / Hub:已接电话:
找到类似的问题here,但没有适当或无法接受的解决方案。
我还添加了运行时权限的代码。启动应用程序时会弹出一个窗口,我可以让该应用程序管理电话。但是结果和以前一样。
谁能建议我可能要去哪里错了?我在这里想念什么吗?
最佳答案
您是否检查了运行时权限?
Requesting Permissions at Runtime
关于java - 无法在Android棉花糖中将传入调用者号码打印到logcat,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38147208/