无法将来电号码打印到 android marshmallow 中的 logcat
Unable to print incoming caller number to logcat in android marshmallow
清单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;
}
}
}
这是我的主要 activity 代码:
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/Hub: CallStateListener Ringing IncomingNumber:
07-01 18:58:16.921 6876-6876/com.xxx.xxx D/Hub: Call Received:
发现了一个类似的问题 here,但没有正确或可接受的解决方案。
我还添加了运行时权限代码。启动应用程序时会出现一个弹出窗口,我允许该应用程序管理 phone 呼叫。但是结果和以前一样。
任何人都可以建议我可能会出错的地方吗?我在这里遗漏了什么吗?
你检查过运行时权限了吗?
清单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;
}
}
}
这是我的主要 activity 代码:
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/Hub: CallStateListener Ringing IncomingNumber:
07-01 18:58:16.921 6876-6876/com.xxx.xxx D/Hub: Call Received:
发现了一个类似的问题 here,但没有正确或可接受的解决方案。 我还添加了运行时权限代码。启动应用程序时会出现一个弹出窗口,我允许该应用程序管理 phone 呼叫。但是结果和以前一样。 任何人都可以建议我可能会出错的地方吗?我在这里遗漏了什么吗?
你检查过运行时权限了吗?