简体   繁体   中英

Broadcast Receiver does not deduct calls - Android

I am trying to do some work at incoming call times. I am trying like this

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();

            }

        }

    }

This is my 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>

I check with mobile and emulator. But it does not show any toast message. Please let me know where I did mistake.

I got reference from this websites. Link 1 Link 2

Continuing my conversation with asker as an answer.I Just started a new Android application and worked out like i have said in the comments.. The manifest looks like this :

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

The CallDetector class looks like this(ur code):

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM