Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:广播接收器_Android - Fatal编程技术网

Android:广播接收器

Android:广播接收器,android,Android,当联系人打电话给我时,我试图收听电话状态,但我收到一些错误 ServiceReceiver.java package org.example.servicereceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.PhoneStateListener; import andr

当联系人打电话给我时,我试图收听电话状态,但我收到一些错误

ServiceReceiver.java

package org.example.servicereceiver;

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


public class ServiceReceiver extends BroadcastReceiver {

    private static final String TAG = "ServiceReceiver"; 

    @Override
    public void onReceive(Context context, Intent intent) {
        MyPhoneStateListener phoneListener=new MyPhoneStateListener();
        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        Log.d(TAG, "telephony: " + telephony);

        telephony.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
    }
}
MyPhoneStateListener.java

package org.example.servicereceiver;

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

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

}
错误:

04-13 18:25:21.845: ERROR/AndroidRuntime(14861): FATAL EXCEPTION: main
04-13 18:25:21.845: ERROR/AndroidRuntime(14861): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.example.servicereceiver/org.example.servicereceiver.ServiceReceiver}: java.lang.ClassCastException: org.example.servicereceiver.ServiceReceiver
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2709)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at android.os.Looper.loop(Looper.java:144)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at android.app.ActivityThread.main(ActivityThread.java:4937)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at java.lang.reflect.Method.invokeNative(Native Method)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at java.lang.reflect.Method.invoke(Method.java:521)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at dalvik.system.NativeStart.main(Native Method)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861): Caused by: java.lang.ClassCastException: org.example.servicereceiver.ServiceReceiver
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at android.app.Instrumentation.newActivity(Instrumentation.java:1036)
04-13 18:25:21.845: ERROR/AndroidRuntime(14861):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2701)
XML:



您的AndroidManifest.xml文件中有什么?我添加到AndroidManifest.xml任务前读取状态并注册了接收者对不起,您怎么看?我只是在学英语,我完全不明白你的意思。我必须删除这个?@Husky:是的-删除AndroidManifest.xml中的部分。Android正试图将你的接收器作为一项活动“启动”。你不能“启动”接收器-它将在你安装应用程序时由系统注册,并在手机状态发生变化时启动。@Husky:很抱歉,我的答案中缺少了一个词-我已经编辑了它。如果我删除它,我无法运行我的应用程序。当我单击该图标时,我的设备上没有安装应用程序。@Husky:你的应用程序中有活动类吗?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.example.servicereceiver"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ServiceReceiver"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".ServiceReceiver">
        <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE"/>
        </intent-filter>
        </receiver>

    </application>
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

</manifest> 
<activity android:name=".ServiceReceiver"