兩個問題:
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;