两个问题:
1.直播过程中,如果接听电话,那么需要将直播声音关闭,电话挂断,需要恢复到原声音大小
2.直播过程中,按Home键退到桌面(后台还在继续直播,还能听到声音),需要在状态栏显示通知xxx正在直播
第一个需要监听广播
private AudioManager audioMgr = null; // Audio管理器,用了控制音量
private int maxVolume; // 最大音量值
private int currentVolume;//当前音量值
private void changeVoice()
{
audioMgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// 获取最大音乐音量
maxVolume = audioMgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
if (android.os.Build.VERSION.SDK_INT >= 21) {
requestPhoneAccess();
}
else
{
initPhone();
}
}
private void getCurrentVoice()
{
currentVolume =audioMgr.getStreamVolume(AudioManager.STREAM_MUSIC);
Log.e("abc--->最大音量------》",maxVolume+"当前音量----->"+currentVolume);
}
class EncryptPhoneStateListener extends PhoneStateListener {
EncryptPhoneStateListener() {
}
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_OFFHOOK:// 拨打电话或者接听电话时
audioMgr.setStreamVolume(AudioManager.STREAM_MUSIC, 0,
AudioManager.FLAG_PLAY_SOUND);
break;
case TelephonyManager.CALL_STATE_RINGING:// 电话进来时
audioMgr.setStreamVolume(AudioManager.STREAM_MUSIC, 0,
AudioManager.FLAG_PLAY_SOUND);
break;
case TelephonyManager.CALL_STATE_IDLE:// 挂起电话时候,或者没有任何反映
audioMgr.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume,
AudioManager.FLAG_PLAY_SOUND);
break;
default:
break;
}
}
}
private void initPhone()
{
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
// 开始监听
PhoneStateListener phoneListener = new EncryptPhoneStateListener();
telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
getCurrentVoice();
}
/**
* 申请电话状态
*/
private void requestPhoneAccess() {
if (!(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED)) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE, Manifest.permission.CALL_PHONE,Manifest.permission.PROCESS_OUTGOING_CALLS}, REQUEST_PHONE_CODE);
}
else
{
initPhone();
}
}
入口:调用changeVoice();
第二个需要监听home键广播
private NotificationManager notificationManager;
private Notification notification;
@SuppressLint("NewApi")
public void sendnotification() {
if (notificationManager == null) {
notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
}
Intent notificationIntent = new Intent(this, LiveActivity.class);
notificationIntent.putExtra("videoinfo",reservation);
notificationIntent.putExtra("videowidht", videoWidht);
notificationIntent.putExtra("videoheight", videoHeight);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
// ③ 定义notification的消息 和 PendingIntent
if (android.os.Build.VERSION.SDK_INT >= 16) {
notification = new Notification.Builder(this)
.setAutoCancel(true).setContentTitle("米多财富")
.setContentText("米多财富正在直播......")
.setContentIntent(contentIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis()).build();
} else {
Notification.Builder builder = new Notification.Builder(this)
.setAutoCancel(true).setContentTitle("米多财富")
.setContentText("米多财富正在直播......").setContentIntent(contentIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis()).setOngoing(true);
notification = builder.getNotification();
}
notificationManager.notify(getNotificationId(this), notification);
}
private int notyfyid;
/**
* 从配置文件中获取通知id(id为累加,防止重复)
*
* @return
*/
public int getNotificationId(Context context) {
id = SharePrefUtil.getInt(context, "NOTIFICATION_ID", 0);
int tempid = ++id;
SharePrefUtil.saveInt(context, "NOTIFICATION_ID", tempid > 1000 ? 0
: tempid);
notyfyid=id;
return id;
}
/*
*监听是否点击了home键将客户端推到后台
*/
private BroadcastReceiver mHomeKeyEventReceiver = new BroadcastReceiver() {
String SYSTEM_REASON = "reason";
String SYSTEM_HOME_KEY = "homekey";
String SYSTEM_HOME_KEY_LONG = "recentapps";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
String reason = intent.getStringExtra(SYSTEM_REASON);
if (TextUtils.equals(reason, SYSTEM_HOME_KEY)) {
//表示按了home键,程序到了后台
try {
sendnotification();
}
catch (Exception e)
{
}
}else if(TextUtils.equals(reason, SYSTEM_HOME_KEY_LONG)){
//表示长按home键,显示最近使用的程序列表
}
}
}
};
注册广播:
registerReceiver(mHomeKeyEventReceiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
反注册广播:
unregisterReceiver(mHomeKeyEventReceiver);
mHomeKeyEventReceiver=null;