简体   繁体   English

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

[英]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 链接1 链接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 : 继续与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>

The CallDetector class looks like this(ur code): 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:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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