簡體   English   中英

廣播接收器不會扣除電話-Android

[英]Broadcast Receiver does not deduct calls - Android

我正在嘗試在來電時做一些工作。 我正在嘗試這樣

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

            }

        }

    }

這是我的宣言:

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

我檢查手機和模擬器。 但是它沒有顯示任何吐司消息。 請讓我知道我在哪里做錯了。

我從該網站獲得了參考。 鏈接1 鏈接2

繼續與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:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM