【问题标题】:Can't get incoming call event无法获得来电事件
【发布时间】:2017-02-03 13:04:19
【问题描述】:

我想实现在后台和前台获取来电事件。因此,我为此目的创建了 BroadcastReceiver,但是在我收到/获取/完成来电后,onReceive() 方法不会触发。 我尝试了很多教程,但没有任何帮助:(

请帮忙。

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="yakiv.bondar.dev.incomeoutcomecalltest">

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<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>
    <receiver
        android:name=".IncomingCall"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>
</application>

广播接收器

public class IncomingCall extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Log.d("tag", "AAAAAAAAAAAAAAAAAAAAAA");
    MyPhoneStateListener phoneListener=new MyPhoneStateListener();
    TelephonyManager telephony = (TelephonyManager)
            context.getSystemService(Context.TELEPHONY_SERVICE);
    telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}

public class MyPhoneStateListener extends PhoneStateListener {
    public void onCallStateChanged(int state,String incomingNumber){
        switch(state){
            case TelephonyManager.CALL_STATE_IDLE:
                Log.d("tag", "IDLE");
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.d("tag", "OFFHOOK");
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                Log.d("tag", "RINGING");
                break;
        }
    }
}
}

主要活动

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

【问题讨论】:

  • 您必须在您的活动中注册您的接收器
  • sripad是对的,你要注册接收器
  • @SripadRaj 我已经在 Manifest.xml 中注册了吗?还不够吗?
  • 您刚刚在清单中声明,但如果您希望广播侦听器收听某些内容,则必须在您的活动中注册它。
  • @SripadRaj 我添加了:String BROADCAST = "android.intent.action.PHONE_STATE"; IntentFilter intentFilter = new IntentFilter(BROADCAST); IncomingCall 接收者 = new IncomingCall(); registerReceiver(receiver,intentFilter);但是 onReceive() 仍然没有被触发。

标签: android broadcastreceiver android-broadcast


【解决方案1】:

在 MainActivity 中:

    IncomingCall mBroadcastReceiver = new IncomingCall();

在 onResume 中:

    IntentFilter filter = new IntentFilter();
    this.registerReceiver(mBroadcastReceiver , filter);

在暂停中:

    this.unregisterReceiver(mBroadcastReceiver );

【讨论】:

  • 我添加了:String BROADCAST = "android.intent.action.PHONE_STATE"; IntentFilter intentFilter = new IntentFilter(BROADCAST); IncomingCall 接收者 = new IncomingCall(); registerReceiver(receiver,intentFilter);但是 onReceive() 仍然没有被触发。
  • 试过了。没用:(
【解决方案2】:

您在活动中错过了呼叫registerReciever(intent,intentFilter)。在onResume() 中注册您的活动并在您的onPause() 方法中调用unregisterReciever(intent)

注意:

还要检查您是否在清单中授予了适当的权限,并注意即使您已声明所需的权限,您也必须在 Android 6.0 及更高版本的运行时处理某些权限。检查 here 以在运行时处理 Marshmallow 及更高版本的权限。

【讨论】:

    【解决方案3】:

    1) 确保您的应用具有 READ_PHONE_STATE 的权限

    2) 你不需要 registerReceiver(BroadcastReceiver, IntentFilter) 因为你的 Manifest 中有它

    3)确保为您的应用打开通知(检查设置--> 应用程序管理器--> 您的应用--> 通知)

    4) 我会尝试在 switch 语句中不使用“break”进行测试

    5) 尝试使用 Toast 而不是 Log。 Toast.makeText(Context, String, Toast.LENGTH_SHORT).show();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 2022-11-14
      • 2019-07-29
      • 1970-01-01
      相关资源
      最近更新 更多