0

着信時にさまざまな機能を実行するアプリケーションを作成しようとしています。小さな実用的な例を作成するために、クラスを拡張BroadcastReceiverし、トースト通知を表示しようとしました。

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class IncomingCallInterceptor extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Do something.", Toast.LENGTH_LONG).show();
    }
}

AndroidManifest.xmlファイルにこの権限を追加しました:

<application android:icon="@drawable/icon" android:label="Incoming Call Interceptor">
    <receiver android:name="IncomingCallInterceptor">
        <intent-filter>
             <action android:name="android.intent.action.PHONE_STATE"/>
        </intent-filter>
    </receiver>
</application>

私のテスト デバイスは Android 4.4.2 を実行しています。誰かが電話をかけているときにトースト通知が表示されることはありません.

4

2 に答える 2

0

このコードを試すMonitor the state of the Phone

import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class PhoneReceiver extends PhoneStateListener {
Context context;
public PhoneReceiver(Context context) {
this.context = context;
}
@Override
public void onCallStateChanged(int state, 
String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
Toast.makeText(context, "onCallStateChanged state=" + 
state + "incomingNumber=" + incomingNumber, 
Toast.LENGTH_LONG).show(); 
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(context, "idle", 
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(context, "ringing", 
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(context, "offhook", 
Toast.LENGTH_LONG).show();
break;
}
}
}
于 2014-04-12T20:24:07.340 に答える