如果电池电量严重不足,我需要我的应用程序在来电时弹出消息。有没有办法做到这一点(如果可能的话,切断电话)?在网上搜索对我没有多大帮助。
问问题
1116 次
1 回答
0
用于在来电期间显示某些内容
public class IncomingCallReciever extends BroadcastReceiver {
private Context mContext;
private Intent mIntent;
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
mIntent = intent;
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int events = PhoneStateListener.LISTEN_CALL_STATE;
tm.listen(phoneStateListener, events);
}
private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
String callState = "UNKNOWN";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
callState = "IDLE";
break;
case TelephonyManager.CALL_STATE_RINGING:
// write your code display Toast or Dialog
break;
}
Log.i(">>>Broadcast", "onCallStateChanged " + callState);
super.onCallStateChanged(state, incomingNumber);
}
};
}
在你的清单文件中声明这种方式
<receiver android:name=".IncomingCallReciever"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
添加关注权限
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
于 2013-06-28T18:33:58.907 回答