如果电池严重不足,我需要我的应用程序在来电时弹出一条消息。有没有办法做到这一点(如果可能的话,切断电话)?在网上搜索对我没什么帮助。
答案 0 :(得分: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"/>