【问题标题】:Broadcast receiver for incoming call not working?来电的广播接收器不起作用?
【发布时间】:2016-08-10 17:15:27
【问题描述】:

我正在尝试在收到来电时显示 toast。但我没有得到任何显示。我在清单文件中提到了接收器,其中还包含电话所需的权限。 以下是我使用的代码。

// inside IncomingCall broadcastreceiver
package com.example.shailesh.callbroadcastreceiver;

 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.telephony.PhoneStateListener;
 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)) {
        // This code will execute when the phone has an incoming call

        // get the phone number
        String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Toast.makeText(context, "Call from:" +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)) {
        // This code will execute when the call is disconnected
        Toast.makeText(context, "Detected call hangup event", Toast.LENGTH_LONG).show();

    }

     }
 }

我在清单文件中指定如下:

 <?xml version="1.0" encoding="utf-8"?>

<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">
        <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>

我还必须包括什么才能在收到来电时显示 toast。 我还在我的MainActivity中包含以下代码

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

    Intent i = new Intent(getApplicationContext(),IncomingCall.class);
    startService(i);
}
}

【问题讨论】:

标签: android android-studio android-broadcast


【解决方案1】:

IncomingCall 类是服务吗?如果不是,那你为什么要启动服务。

【讨论】:

  • 即使我删除了这些行并重新运行项目,它在调用接收时也没有给我任何输出..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-02
相关资源
最近更新 更多