广播接收器不会扣除电话 - Android

时间:2015-05-21 06:19:57

标签: android broadcastreceiver

我正在尝试在来电时做一些工作。我正在尝试这样

public class callDeductor extends BroadcastReceiver {



       @Override
        public void onReceive(Context context, Intent intent) {                                       
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);                    


            if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
            {
                 Toast.makeText(context, "Phone Is Ringing", Toast.LENGTH_LONG).show();

            }

            if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
            {
                 Toast.makeText(context, "Call Recieved", Toast.LENGTH_LONG).show();
            }

            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))
            {
                Toast.makeText(context, "Phone Is Idle", Toast.LENGTH_LONG).show();

            }

        }

    }

这是我的Manifeast:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="Call Deduct" >
        <receiver android:name="com.deduct.calldeduct.callDeductor" > 
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

我查看手机和模拟器。但它没有显示任何Toast消息。请告诉我错误的地方。

我从这个网站上得到了参考。 Link 1 Link 2

1 个答案:

答案 0 :(得分:1)

继续我与asker的对话作为答案。我刚开始一个新的Android应用程序,就像我在评论中说的那样......清单看起来像这样:

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

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="19" />

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


     <receiver android:name=".CallDetector">
        <intent-filter>                
            <action android:name="android.intent.action.PHONE_STATE" />                
        </intent-filter>
    </receiver>

    <activity
        android:name=".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>
</application>

CallDetector类看起来像这样(你的代码):

public class CallDetector extends BroadcastReceiver{

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


    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);                    


    if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
    {
        Toast.makeText(context, "Phone Is Ringing", Toast.LENGTH_LONG).show();

    }

    if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
    {
        Toast.makeText(context, "Call Recieved", Toast.LENGTH_LONG).show();
    }

    if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))
    {
        Toast.makeText(context, "Phone Is Idle", Toast.LENGTH_LONG).show();

    }

}

}

ScreenShot of the project set up: