【问题标题】:What's wrong with my code in detecting the incoming call in android?我的代码在检测android中的来电时有什么问题?
【发布时间】:2016-05-17 16:58:34
【问题描述】:

我搜索了很多资源,但没有任何帮助.. 我也试过this,但没用...

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chotu.mybot">

<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

<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"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".IncomingCall">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>
</application>

IncomingCall.java:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class IncomingCall extends BroadcastReceiver
{

@Override
public void onReceive(Context context, Intent intent)
{
    if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
        String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Toast.makeText(context," its name :"+incomingNumber, Toast.LENGTH_LONG).show();
    } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE) || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
        Toast.makeText(context," something", Toast.LENGTH_LONG).show();
    }
}
}

【问题讨论】:

  • 你在 manifest 中有 权限吗?
  • 就像其他人说的,添加 READ_PHONE_STATE 权限,如果你在棉花糖上开发,你需要在运行时请求所有权限
  • @ShashankUdupa :是的,我尝试了包含它,但它仍然没有用......
  • 你在用棉花糖吗?
  • @Opiatefuchs :所有权限意味着..?什么权限?我也应该包括 OUTGOING_CALL...?

标签: java android


【解决方案1】:

您需要添加以下权限才能使此代码工作:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-03-05
  • 1970-01-01
  • 2011-07-28
  • 1970-01-01
相关资源
最近更新 更多