开发者

How to create Custom Broadcast Receiver

I开发者_如何学Python need to create custom broadcast receiver which will show me about any PHONE_STATE event occurred.


In your manifest you should define the following:

<receiver
        android:enabled="true"
        android:label="@string/app_name"
        android:name=".PhoneStateReceiver">
        <intent-filter>
            <action
                android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
</receiver>

The sample receiver class:

public class PhoneStateReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    TelephonyManager telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    switch(telManager.getCallState()) {
    case TelephonyManager.CALL_STATE_IDLE:
      //do something
    }
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜