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
}
}
}
精彩评论