Android采用消息推送实现类似微信视频接听
本文实例为大家分享了Android实现类似微信视频接听的具体代码,供大家参考,具体内容如下
1、背景需求:业务需要接入视频审核功能,在PC端发起视频通话,移动端显示通话界面点击接听后进行1对1视频通话。
2、解决方案:因为项目没有IM功能。只集成了极光消息推送(极光消息推送接入参考官方文档,经过跟需求沟通,采用消息推送调起通话接听界面。再集成腾讯实时音视频SDK(具体集成方式参考官方文档)。最终实现类似微信1对1通话功能。
3、技术实现:
A:编写一个广播接收器,并且在AndroidManifest中注册,这就是一个全局的广播接收器。应用退到后台或者应用进程被kill,只要极光的push进程是Live,就能接受到消息,启动通话接听界面。
/**
*Createdon2018/3/2916:19
*@authorbaokang.jia
*极光推送广播接收器
*/
publicclassJiGuangPushReceiverextendsBroadcastReceiver{
privatestaticfinalStringTAG="JPushReceiver";
@Override
publicvoidonReceive(Contextcontext,Intentintent){
Bundlebundle=intent.getExtras();
if(bundle==null){
return;
}
//拿到锁屏管理者
KeyguardManagerkm=(KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE);
if(km!=null&&km.isKeyguardLocked()){//为true就是锁屏状态下
startLoginOrCallActivity(context,bundle);
}else{
if(JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())){
StringregId=bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
LogUtil.d(TAG,"[MyReceiver]接收RegistrationId:"+regId);
//sendtheRegistrationIdtoyoursserver...
}elseif(JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())){
LogUtil.d(TAG,"[MyReceiver]接收到推送下来的自定义消息:"+bundle.getString(JPushInterface.EXTRA_MESSAGE));
}elseif(JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())){//接收到推送下来的通知
//启动通话界面
startLoginOrCallActivity(context,bundle);
}elseif(JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())){//点击通知栏
//启动通话界面
startLoginOrCallActivity(context,bundle);
//清除所有状态的通知
JPushInterface.clearAllNotifications(context);
}elseif(JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())){
LogUtil.d(TAG,"[MyReceiver]用户收到到RICHPUSHCALLBACK:"+bundle.getString(JPushInterface.EXTRA_EXTRA));
//在这里根据JPushInterface.EXTRA_EXTRA的内容处理代码,比如打开新的Activity,打开一个网页等..
}
}
}
/**
*未登录跳转登录界面,
*else启动通话接听界面
*/
privatevoidstartLoginOrCallActivity(Contextcontext,Bundlebundle){
//EXTRA_EXTRA
Stringextras=bundle.getString(JPushInterface.EXTRA_EXTRA);
StringuserID=SPUtil.getString(context,Constants.LOGIN_USER_ID);
if(TextUtils.isEmpty(userID)){
//启动登录界面
Intentintent=newIntent(context,LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}else{
//启动通话接听界面
intnotifyId=bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
if(!TextUtils.isEmpty(extras)&&extras.contains("androidNotificationextraskey")){
ReceiveTalkActivity.startReceiveTalkActivity(context,extras,notifyId);
}
}
}
}
//在AndroidManifest注册全局自定义广播接收器
B:启动通话接听界面,启动接听界面后获取当前手机模式
AudioManageraudio=(AudioManager)this.getSystemService(Context.AUDIO_SERVICE); //手机模式,振动,精英、响铃,更具不同模式振动或者响铃,具体可参考以下的实现代码。 //点击接听按钮后跳转腾讯视频通话界面
/**
*Createdon2019/4/2816:19
*@authorbaokang.jia
*视频预审接听界面
*/
publicclassReceiveTalkActivityextendsBaseActivity{
privatestaticStringPUSH_MSG_KEY="push_msg_key";
privatestaticStringNOTIFICATION_ID_KEY="notification_id_key";
/**
*腾讯云注册分配的appId
*/
privateintsdkAppId=
/**
*检查运行时权限
*/
privatebooleanmCheckPermissionResult=false;
privatePushMsgBeanmPushMsgBean;
/**
*媒体播放
*/
privateMediaPlayermMediaPlayer;
/**
*震动
*/
privateVibratormVibrator;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
Windowwindow=getWindow();
//悬浮窗
WindowViewUtil.setWindowFloatAndScreenOn(window,this);
super.onCreate(savedInstanceState);
//初始化倒计时器
initCountDownTimer();
//请求权限
requestMustPermission();
initViews();
//根据通知Id清除状态栏对应的通知
JPushInterface.clearAllNotifications(this);
//持续震动和响铃
continuedVibratorAndMediaPlayer();
}
/**
*60秒后关闭activity
*/
privatevoidinitCountDownTimer(){
longtime=30000;
longcountDownInterval=1000;
CountDownTimerdownTimer=newCountDownTimer(time,countDownInterval){
@Override
publicvoidonTick(longmillisUntilFinished){
}
@Override
publicvoidonFinish(){
finish();
}
};
downTimer.start();
}
@Override
protectedintgetLayoutId(){
returnR.layout.activity_receive_talk;
}
@Override
protectedbooleaninitToolbar(){
returnfalse;
}
@Override
protectedvoidgetIntent(Intentintent){
StringpushMsg=getIntent().getStringExtra(PUSH_MSG_KEY);
//notificationId=getIntent().getIntExtra(NOTIFICATION_ID_KEY,0);
parsePushMsg(pushMsg);
}
@Override
protectedvoidinitViews(){
ButtonbtnCancel=findViewById(R.id.btn_cancel_call);
ButtonbtnAnswer=findViewById(R.id.btn_answer_call);
btnCancel.setOnClickListener(v->{
mVibrator.cancel();
mMediaPlayer.stop();
finish();
});
btnAnswer.setOnClickListener(v->{
mVibrator.cancel();
mMediaPlayer.stop();
if(mCheckPermissionResult){
Intentintent=newIntent(this,TRTCMainActivity.class);
intent.putExtra("roomId",Integer.valueOf(mPushMsgBean.getRoomId()));
intent.putExtra("userId",mPushMsgBean.getUserId());
intent.putExtra("sdkAppId",sdkAppId);
intent.putExtra("userSig",mPushMsgBean.getUserSig());
startActivity(intent);
finish();
}else{
ToastUtil.longToast("需要的权限被拒绝,无法开启视频审核");
}
});
}
/**
*持续响铃和震动
*/
privatevoidcontinuedVibratorAndMediaPlayer(){
//获取媒体播放器
mMediaPlayer=newMediaPlayer();
try{
mMediaPlayer.setDataSource(this,RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_RINGTONE));//这里我用的通知声音,还有其他的,大家可以点进去看
mMediaPlayer.prepare();
}catch(IOExceptione){
e.printStackTrace();
}
//取得震动服务的句柄
mVibrator=(Vibrator)this.getSystemService(VIBRATOR_SERVICE);
//获取当前手机模式
AudioManageraudio=(AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
if(audio!=null){
switch(audio.getRingerMode()){
caseAudioManager.RINGER_MODE_SILENT://静音
//dosth
break;
caseAudioManager.RINGER_MODE_NORMAL://响铃
mMediaPlayer.start();
mMediaPlayer.setLooping(true);//循环播放
break;
caseAudioManager.RINGER_MODE_VIBRATE://震动
//数组参数意义:第一个参数为等待指定时间后开始震动,
//震动时间为第二个参数。后边的参数依次为等待震动和震动的时间
//第二个参数为重复次数,-1为不重复,0为一直震动
if(mVibrator!=null){
mVibrator.vibrate(newlong[]{1000,1000},0);
}
break;
}
}
}
privatevoidparsePushMsg(StringpushMsg){
if(!TextUtils.isEmpty(pushMsg)){
CustomerMsgcustomerMsg=GsonUtil.fromJson(pushMsg,CustomerMsg.class);
StringpushMsgContent=customerMsg.getPushMsgContent();
pushMsgContent=pushMsgContent.replace("\\","");
LogUtil.d(Constants.LOG,"pushMsgContent="+pushMsgContent);
mPushMsgBean=GsonUtil.fromJson(pushMsgContent,PushMsgBean.class);
}
}
/**
*申请应用必须的权限
*/
privatevoidrequestMustPermission(){
AndPermission.with(this)
.requestCode(Constants.REQUEST_CODE_PERMISSION)
.permission(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.VIBRATE,
Manifest.permission.DISABLE_KEYGUARD,
Manifest.permission.WAKE_LOCK
)
.rationale((requestCode,rationale)->
//再次申请被拒绝的权限
AlertDialog.newBuilder(this)
.setTitle(R.string.title_dialog)
.setMessage(R.string.message_permission_failed)
.setPositiveButton(R.string.ok,(dialog,which)->{
dialog.cancel();
rationale.resume();
})
.setNegativeButton(R.string.no,(dialog,which)->{
dialog.cancel();
rationale.cancel();
}).show())
.callback(newPermissionListener(){
@Override
publicvoidonSucceed(intrequestCode,@NonNullListgrantPermissions){
mCheckPermissionResult=true;
}
@Override
publicvoidonFailed(intrequestCode,@NonNullListdeniedPermissions){
mCheckPermissionResult=false;
}
})
.start();
}
/**
*界面未销毁,启动此界面时回调
*/
@Override
protectedvoidonNewIntent(Intentintent){
super.onNewIntent(intent);
StringpushMsg=intent.getStringExtra(PUSH_MSG_KEY);
//notificationId=intent.getIntExtra(NOTIFICATION_ID_KEY,0);
parsePushMsg(pushMsg);
}
/**
*提供给外部调用启动接听界面的activity
*
*@paramcex上下文对象
*@parampushMsg消息内容
*@paramnotifyId通知id
*/
publicstaticvoidstartReceiveTalkActivity(Contextcex,StringpushMsg,intnotifyId){
IntentcalIntent=newIntent(cex,ReceiveTalkActivity.class);
//携带数据
calIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
calIntent.putExtra(PUSH_MSG_KEY,pushMsg);
calIntent.putExtra(NOTIFICATION_ID_KEY,notifyId);
cex.startActivity(calIntent);
}
@Override
protectedvoidonDestroy(){
super.onDestroy();
mMediaPlayer.stop();
mVibrator.cancel();
}
}
//注册ReceiveTalkActivity,android:launchMode="singleTask"
总结:项目中考虑时间和成本问题。没有接入IM功能。消息推送不可靠,极光的push进程被杀,是收不到消息。当打开app后,会蹦出很多通知。这只是简易的实现了在pc调起移动端进行视频通话。这有很多因素是没有考虑进去的,在此先记录下吧。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。