Android 12 引入了新的来电和去电通知样式,也增加了显示prominent chip的能力。
示例代码:
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
PhoneStateListener phoneStateListener = new PhoneStateListener() { @Override public void onCallStateChanged(int state, String phoneNumber) { super.onCallStateChanged(state, phoneNumber); switch (state) { case TelephonyManager.CALL_STATE_RINGING: // 发送通知 Toast.makeText(MainActivity.this, "Incoming call", Toast.LENGTH_SHORT).show(); break; case TelephonyManager.CALL_STATE_IDLE: // 关闭通知 Toast.makeText(MainActivity.this, "Call ended", Toast.LENGTH_SHORT).show(); break; } } }; // 监听来电状态 telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
示例代码:
private static final String OUTGOING_CALL_ACTION = "android.intent.action.NEW_OUTGOING_CALL"; private BroadcastReceiver outgoingCallReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (action.equals(OUTGOING_CALL_ACTION)) { String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); // 发送通知 Toast.makeText(MainActivity.this, "Outgoing call to " + phoneNumber, Toast.LENGTH_SHORT).show(); } } };
// 注册BroadcastReceiver IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(OUTGOING_CALL_ACTION); registerReceiver(outgoingCallReceiver, intentFilter);
示例代码:
// 创建ChipDrawable ChipDrawable chipDrawable = ChipDrawable.createFromResource(this, R.xml.chip);
// 设置ChipDrawable的文