启动后即可收听手机状态

时间:2015-02-11 19:57:36

标签: android broadcastreceiver phone-state-listener

在我的应用程序中,我使用Broadcast Receiver收听电话状态事件(即收到呼叫,空闲等)。代码如下:

public class PhoneCallreceiver extends BroadcastReceiver {
    private boolean ringing;
    private boolean received;

    public PhoneCallreceiver() {
    }

@Override
public void onReceive(Context context, Intent intent) {
    Log.v("ranjith", "enterted receiver");
    Bundle bundle = intent.getExtras();
    String incomingnumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
    String state = bundle.getString(TelephonyManager.EXTRA_STATE);
    Log.v("ranjapp", state);

    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
        ringing = true;
        received = false;
    } 
}

的Manifest.xml

    <receiver
        android:name=".PhoneCallreceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE"/>
        </intent-filter>
    </receiver>

我的问题是我想在重新启动设备后自动启动BroadCast Receiver。我有一个BroadCast Receiver来监听启动,但我不知道如何启动PhoneCallreceiver BroadCast接收器。

的Manifest.xml

   <receiver
        android:name=".backend.BootStarter"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

代码:

public class BootStarter extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) { 
        PhoneCallreceiver callreceiver = new PhoneCallreceiver();
        context.registerReceiver(callreceiver,new IntentFilter(**what to put here**));
            }
        }

1 个答案:

答案 0 :(得分:1)

找到我的问题的答案,intentfilter接收phonestate的行动是android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED

我的更新代码如下:

public class BootStarter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) { 
    PhoneCallreceiver callreceiver = new PhoneCallreceiver();
            context.registerReceiver(new PhoneCallreceiver(), new IntentFilter(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED));
        }
    }