Android:广播接收器无法正常工作

时间:2013-05-17 06:25:29

标签: android broadcastreceiver broadcast android-broadcast

我有几个广播接收器。其中一个广播用于来电,另一个用于拨出电话。我有两个与之合作的AVD。我从另一个呼叫一个,两个广播似乎都没有工作。我在其他几篇类似的帖子中读到,我的应用程序的一个活动需要打开才能使广播正常工作,但这也不起作用。 这是我的清单:

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

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.prototype_3.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".Add_Contact" />
    <activity android:name=".Edit_Contact" />

    <reciever android:name=".IncomingCallReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </reciever>

    <reciever android:name=".OutgoingCallReceiver">
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </reciever>

</application>

</manifest>

这是IncomingCallReceiver.java文件:

package com.example.prototype_3;

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

public class IncomingCallReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    PhoneCallStateListener PCSL = new PhoneCallStateListener(context);
    telephony.listen(PCSL, PhoneStateListener.LISTEN_CALL_STATE);
}

}

和PhoneCallStateListener.java文件

package com.example.prototype_3;

import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class PhoneCallStateListener extends PhoneStateListener {
Context context;

public PhoneCallStateListener(Context context){
    this.context = context;
    Log.v("In PCSL", "In PCSL");
}

public void onCallStateChanged(int state, String IncomingNumber){
    switch(state){
    case TelephonyManager.CALL_STATE_RINGING:
        Log.v("Ringing", "No.="+IncomingNumber);break;
    case TelephonyManager.CALL_STATE_OFFHOOK:
        Log.v("OFFHOOK", "No.="+IncomingNumber);break;
    case TelephonyManager.CALL_STATE_IDLE:
        Log.v("IDLE", "No.="+IncomingNumber);
    }
}
}

0 个答案:

没有答案