Android采用消息推送實(shí)現(xiàn)類似微信視頻接聽
本文實(shí)例為大家分享了Android實(shí)現(xiàn)類似微信視頻接聽的具體代碼,供大家參考,具體內(nèi)容如下
1、背景需求:業(yè)務(wù)需要接入視頻審核功能,在PC 端發(fā)起視頻通話,移動(dòng)端顯示通話界面點(diǎn)擊接聽后進(jìn)行1對(duì)1視頻通話。
2、解決方案:因?yàn)轫?xiàng)目沒有IM功能。只集成了極光消息推送(極光消息推送接入?yún)⒖脊俜轿臋n,經(jīng)過跟需求溝通,采用消息推送調(diào)起通話接聽界面。再集成騰訊實(shí)時(shí)音視頻SDK(具體集成方式參考官方文檔)。最終實(shí)現(xiàn)類似微信1對(duì)1通話功能。
3、技術(shù)實(shí)現(xiàn):
A:編寫一個(gè)廣播接收器,并且在 AndroidManifest中注冊(cè),這就是一個(gè)全局的廣播接收器。應(yīng)用退到后臺(tái)或者應(yīng)用進(jìn)程被kill,只要極光的push進(jìn)程是Live,就能接受到消息,啟動(dòng)通話接聽界面。
/** * Created on 2018/3/29 16:19 * @author baokang.jia * 極光推送廣播接收器 */ public class JiGuangPushReceiver extends BroadcastReceiver { private static final String TAG = "JPushReceiver"; @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); if (bundle == null) { return; } //拿到鎖屏管理者 KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); if (km != null && km.isKeyguardLocked()) { //為true就是鎖屏狀態(tài)下 startLoginOrCallActivity(context,bundle); } else { if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) { String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID); LogUtil.d(TAG, "[MyReceiver] 接收Registration Id : " + regId); //send the Registration Id to yours server... } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) { LogUtil.d(TAG, "[MyReceiver] 接收到推送下來的自定義消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE)); } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {//接收到推送下來的通知 //啟動(dòng)通話界面 startLoginOrCallActivity(context, bundle); } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {//點(diǎn)擊通知欄 //啟動(dòng)通話界面 startLoginOrCallActivity(context, bundle); //清除所有狀態(tài)的通知 JPushInterface.clearAllNotifications(context); } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) { LogUtil.d(TAG, "[MyReceiver] 用戶收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA)); //在這里根據(jù) JPushInterface.EXTRA_EXTRA 的內(nèi)容處理代碼,比如打開新的Activity, 打開一個(gè)網(wǎng)頁(yè)等.. } } } /** * 未登錄跳轉(zhuǎn)登錄界面, * else 啟動(dòng)通話接聽界面 */ private void startLoginOrCallActivity(Context context, Bundle bundle) { //EXTRA_EXTRA String extras = bundle.getString(JPushInterface.EXTRA_EXTRA); String userID = SPUtil.getString(context, Constants.LOGIN_USER_ID); if (TextUtils.isEmpty(userID)) { //啟動(dòng)登錄界面 Intent intent = new Intent(context, LoginActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } else { //啟動(dòng)通話接聽界面 int notifyId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID); if (!TextUtils.isEmpty(extras) && extras.contains("androidNotification extras key")){ ReceiveTalkActivity.startReceiveTalkActivity(context, extras,notifyId); } } } } //在AndroidManifest注冊(cè)全局自定義廣播接收器 <receiver android:name=".event.JiGuangP`在這里插入代碼片`ushReceiver" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="cn.jpush.android.intent.REGISTRATION" /> <!-- Required 用戶注冊(cè)SDK的intent --> <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!-- Required 用戶接收SDK消息的intent --> <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!-- Required 用戶接收SDK通知欄信息的intent --> <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!-- Required 用戶打開自定義通知欄的intent --> <action android:name="cn.jpush.android.intent.CONNECTION" /> <!-- 接收網(wǎng)絡(luò)變化 連接/斷開 since 1.6.3 --> <category android:name="${PACKAGE_NAME}" /> </intent-filter> </receiver>
B:啟動(dòng)通話接聽界面,啟動(dòng)接聽界面后獲取當(dāng)前手機(jī)模式
AudioManager audio = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); //手機(jī)模式,振動(dòng),精英、響鈴,更具不同模式振動(dòng)或者響鈴,具體可參考以下的實(shí)現(xiàn)代碼。 //點(diǎn)擊接聽按鈕后跳轉(zhuǎn)騰訊視頻通話界面
/** * Created on 2019/4/28 16:19 * @author baokang.jia * 視頻預(yù)審接聽界面 */ public class ReceiveTalkActivity extends BaseActivity { private static String PUSH_MSG_KEY = "push_msg_key"; private static String NOTIFICATION_ID_KEY = "notification_id_key"; /** * 騰訊云注冊(cè)分配的appId */ private int sdkAppId = /** * 檢查運(yùn)行時(shí)權(quán)限 */ private boolean mCheckPermissionResult = false; private PushMsgBean mPushMsgBean; /** * 媒體 */ private MediaPlayer mMediaPlayer; /** * 震動(dòng) */ private Vibrator mVibrator; @Override protected void onCreate(Bundle savedInstanceState) { Window window = getWindow(); //懸浮窗 WindowViewUtil.setWindowFloatAndScreenOn(window,this); super.onCreate(savedInstanceState); //初始化倒計(jì)時(shí)器 initCountDownTimer(); //請(qǐng)求權(quán)限 requestMustPermission(); initViews(); //根據(jù)通知Id清除狀態(tài)欄對(duì)應(yīng)的通知 JPushInterface.clearAllNotifications(this); //持續(xù)震動(dòng)和響鈴 continuedVibratorAndMediaPlayer(); } /** * 60秒后關(guān)閉activity */ private void initCountDownTimer() { long time = 30000; long countDownInterval = 1000; CountDownTimer downTimer = new CountDownTimer(time, countDownInterval) { @Override public void onTick(long millisUntilFinished) { } @Override public void onFinish() { finish(); } }; downTimer.start(); } @Override protected int getLayoutId() { return R.layout.activity_receive_talk; } @Override protected boolean initToolbar() { return false; } @Override protected void getIntent(Intent intent) { String pushMsg = getIntent().getStringExtra(PUSH_MSG_KEY); //notificationId = getIntent().getIntExtra(NOTIFICATION_ID_KEY, 0); parsePushMsg(pushMsg); } @Override protected void initViews() { Button btnCancel = findViewById(R.id.btn_cancel_call); Button btnAnswer = findViewById(R.id.btn_answer_call); btnCancel.setOnClickListener(v ->{ mVibrator.cancel(); mMediaPlayer.stop(); finish(); }); btnAnswer.setOnClickListener(v -> { mVibrator.cancel(); mMediaPlayer.stop(); if (mCheckPermissionResult) { Intent intent = new Intent(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("需要的權(quán)限被拒絕,無法開啟視頻審核"); } }); } /** * 持續(xù)響鈴和震動(dòng) */ private void continuedVibratorAndMediaPlayer() { //獲取媒體 器 mMediaPlayer = new MediaPlayer(); try { mMediaPlayer.setDataSource(this, RingtoneManager .getDefaultUri(RingtoneManager.TYPE_RINGTONE));//這里我用的通知聲音,還有其他的,大家可以點(diǎn)進(jìn)去看 mMediaPlayer.prepare(); } catch (IOException e) { e.printStackTrace(); } //取得震動(dòng)服務(wù)的句柄 mVibrator = (Vibrator)this. getSystemService(VIBRATOR_SERVICE); //獲取當(dāng)前手機(jī)模式 AudioManager audio = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); if (audio != null) { switch (audio.getRingerMode()) { case AudioManager.RINGER_MODE_SILENT://靜音 //do sth break; case AudioManager.RINGER_MODE_NORMAL://響鈴 mMediaPlayer.start(); mMediaPlayer.setLooping(true); //循環(huán) break; case AudioManager.RINGER_MODE_VIBRATE://震動(dòng) //數(shù)組參數(shù)意義:第一個(gè)參數(shù)為等待指定時(shí)間后開始震動(dòng), //震動(dòng)時(shí)間為第二個(gè)參數(shù)。后邊的參數(shù)依次為等待震動(dòng)和震動(dòng)的時(shí)間 //第二個(gè)參數(shù)為重復(fù)次數(shù),-1為不重復(fù),0為一直震動(dòng) if (mVibrator != null) { mVibrator.vibrate( new long[]{1000,1000},0); } break; } } } private void parsePushMsg(String pushMsg) { if (!TextUtils.isEmpty(pushMsg)) { CustomerMsg customerMsg = GsonUtil.fromJson(pushMsg, CustomerMsg.class); String pushMsgContent = customerMsg.getPushMsgContent(); pushMsgContent = pushMsgContent.replace("\\", ""); LogUtil.d(Constants.LOG,"pushMsgContent="+pushMsgContent); mPushMsgBean = GsonUtil.fromJson(pushMsgContent, PushMsgBean.class); } } /** * 申請(qǐng)應(yīng)用必須的權(quán)限 */ private void requestMustPermission() { 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) -> //再次申請(qǐng)被拒絕的權(quán)限 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(new PermissionListener() { @Override public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) { mCheckPermissionResult = true; } @Override public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) { mCheckPermissionResult = false; } }) .start(); } /** * 界面未銷毀,啟動(dòng)此界面時(shí)回調(diào) */ @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); String pushMsg = intent.getStringExtra(PUSH_MSG_KEY); //notificationId = intent.getIntExtra(NOTIFICATION_ID_KEY, 0); parsePushMsg(pushMsg); } /** * 提供給外部調(diào)用啟動(dòng)接聽界面的activity * * @param cex 上下文對(duì)象 * @param pushMsg 消息內(nèi)容 * @param notifyId 通知id */ public static void startReceiveTalkActivity(Context cex, String pushMsg, int notifyId) { Intent calIntent = new Intent(cex, ReceiveTalkActivity.class); //攜帶數(shù)據(jù) calIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); calIntent.putExtra(PUSH_MSG_KEY, pushMsg); calIntent.putExtra(NOTIFICATION_ID_KEY, notifyId); cex.startActivity(calIntent); } @Override protected void onDestroy() { super.onDestroy(); mMediaPlayer.stop(); mVibrator.cancel(); } } //注冊(cè)ReceiveTalkActivity, android:launchMode="singleTask" <activity android:name=".trtc.view.ReceiveTalkActivity" android:launchMode="singleTask" android:screenOrientation="portrait" />
總結(jié):項(xiàng)目中考慮時(shí)間和成本問題。沒有接入IM功能。消息推送不可靠,極光的push進(jìn)程被殺,是收不到消息。當(dāng)打開app后,會(huì)蹦出很多通知。這只是簡(jiǎn)易的實(shí)現(xiàn)了在pc調(diào)起移動(dòng)端進(jìn)行視頻通話。這有很多因素是沒有考慮進(jìn)去的,在此先記錄下吧。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Android基礎(chǔ)控件RadioGroup使用方法詳解
欄 目:Android
下一篇:RadioGroup實(shí)現(xiàn)單選框的多行排列
本文標(biāo)題:Android采用消息推送實(shí)現(xiàn)類似微信視頻接聽
本文地址:http://mengdiqiu.com.cn/a1/Android/9080.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)登錄
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10Emoji表情在Android JNI中的兼容性問題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10android開發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解
- 01-10android異步消息機(jī)制 源碼層面徹底解析(1)


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問題詳
隨機(jī)閱讀
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?