【问题标题】:Incoming call broadcast receiver not working (Android 4.1)来电广播接收器不工作(Android 4.1)
【发布时间】:2012-11-28 10:56:15
【问题描述】:

我正在尝试接收来电广播,但它不起作用。

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.bgPicture"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk android:minSdkVersion="9" />

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

<uses-feature
    android:name="android.hardware.camera"
    android:required="false" />
<uses-feature
    android:name="android.hardware.camera.front"
    android:required="false" />

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name" >
    <receiver android:name=".PhoneStateBroadcastReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>
</application>

这是我的广播接收器

package com.test.bgPicture;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class PhoneStateBroadcastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {

        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);
    }
}

还有听者..

package com.test.bgPicture;

import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class CustomPhoneStateListener extends PhoneStateListener {

//private static final String TAG = "PhoneStateChanged";
Context context; //Context to make Toast if required 
public CustomPhoneStateListener(Context context) {
    super();
    this.context = context;
}

@Override
public void onCallStateChanged(int state, String incomingNumber) {
    super.onCallStateChanged(state, incomingNumber);

    switch (state) {
    case TelephonyManager.CALL_STATE_IDLE:
        //when Idle i.e no call
        Toast.makeText(context, "Phone state Idle", Toast.LENGTH_LONG).show();
        break;
    case TelephonyManager.CALL_STATE_OFFHOOK:
        //when Off hook i.e in call
        //Make intent and start your service here
        Toast.makeText(context, "Phone state Off hook", Toast.LENGTH_LONG).show();
        break;
    case TelephonyManager.CALL_STATE_RINGING:
        //when Ringing
        Toast.makeText(context, "Phone state Ringing", Toast.LENGTH_LONG).show();
        break;
    default:
        break;
    }
}
}

但是没有显示祝酒词,有什么想法吗?

编辑:我刚刚在另一部带有 android 2.3.6 的手机上尝试过,它在那里工作,但在第一台设备(android 4.1)上它不起作用。为什么会这样?

【问题讨论】:

  • 尝试 Log.v("onCallStateChanged","Phone state Idle"); 并检查 logcat,因为您用于显示 Toast 的可能上下文可能不可用
  • 这没有帮助..我确实注意到它适用于 android 2.3.6 但不适用于 android 4.1,知道吗?

标签: android broadcastreceiver


【解决方案1】:

我发现了问题,您必须在广播接收器从 android 3.0 开始工作之前从您的应用程序手动启动一个活动。

【讨论】:

  • 我在博客的某处读到这是一些安全功能,因此用户从未通过启动活动激活的应用程序不会收到广播。
【解决方案2】:

您是否尝试过在服务中添加广播接收器?即使应用程序关闭/最小化,服务也应保持活动状态。 Check this link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多