来电广播接收器不工作(安卓4.1)接收器、工作

由网友(那么爱劈腿怎么不去跳芭蕾)分享简介:我想抓住一个进来的电话广播,但它不能正常工作。I'm trying to catch an incomming call broadcast but it isn't working.这是我的清单:我想抓住一个进来的电话广播,但它不能正常工作。

I'm trying to catch an incomming call broadcast but it isn't working.

这是我的清单:

<?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;
    }
}
}

但祝酒词中不显示,任何想法的?

But the toasts aren't displayed, any idea's?

编辑:我只是想用另一部手机采用Android 2.3.6,并在那里工作,但第一个设备上(安卓4.1),没有工作。为什么会这样?

I just tried it with another phone with android 2.3.6 and it worked there, but on the first device (android 4.1) it didn't work. Why could this be ?

推荐答案

如果发现了问题,必须手动从您的应用程序启动的活动的广播接收器开始工作前,作为安卓3.0。

If found the problem, you must manually start an activity from your application before the broadcast receivers starts to work as of android 3.0.

阅读全文

相关推荐

最新文章