优化退出应用的机制

This commit is contained in:
lyswhut 2021-08-07 11:45:55 +08:00
parent a2e64dbda3
commit 29396d17df
8 changed files with 50 additions and 16 deletions

View File

@ -37,7 +37,23 @@ public class UtilsModule extends ReactContextBaseJavaModule {
@ReactMethod
public void exitApp() {
// https://github.com/wumke/react-native-exit-app/blob/master/android/src/main/java/com/github/wumke/RNExitApp/RNExitAppModule.java
android.os.Process.killProcess(android.os.Process.myPid());
// android.os.Process.killProcess(android.os.Process.myPid());
// https://stackoverflow.com/questions/6330200/how-to-quit-android-application-programmatically
Activity currentActivity = reactContext.getCurrentActivity();
if (currentActivity == null) {
Log.d("Utils", "killProcess");
android.os.Process.killProcess(android.os.Process.myPid());
} else {
if(Build.VERSION.SDK_INT >= 21){
currentActivity.finishAndRemoveTask();
} else if(Build.VERSION.SDK_INT >= 16){
currentActivity.finishAffinity();
} else{
currentActivity.finish();
}
System.exit(0);
}
}
@ReactMethod

4
package-lock.json generated
View File

@ -10682,8 +10682,8 @@
"integrity": "sha512-Ls9qiNZzW/OLFoI25wfjjAcrf2DZ975hn2vr6U9gyuxi2nooVbzQeFoQS5vQcbCt9QX5NY8ASEEAtlLdIa6KVg=="
},
"react-native-track-player": {
"version": "git+https://github.com/lyswhut/react-native-track-player.git#732171172d64194698a130ab5edbde6c3fa00a44",
"from": "git+https://github.com/lyswhut/react-native-track-player.git#732171172d64194698a130ab5edbde6c3fa00a44"
"version": "git+https://github.com/lyswhut/react-native-track-player.git#7d5ed18acc7cade159d895c4148b97082e1a08a1",
"from": "git+https://github.com/lyswhut/react-native-track-player.git#7d5ed18acc7cade159d895c4148b97082e1a08a1"
},
"react-native-vector-icons": {
"version": "8.1.0",

View File

@ -61,7 +61,7 @@
"react-native-pager-view": "^5.4.0",
"react-native-randombytes": "^3.6.1",
"react-native-splash-screen": "^3.2.0",
"react-native-track-player": "git+https://github.com/lyswhut/react-native-track-player.git#732171172d64194698a130ab5edbde6c3fa00a44",
"react-native-track-player": "git+https://github.com/lyswhut/react-native-track-player.git#7d5ed18acc7cade159d895c4148b97082e1a08a1",
"react-native-vector-icons": "^8.1.0",
"react-redux": "^7.2.4",
"readable-stream": "1.0.33",

View File

@ -2,6 +2,10 @@
- 新增局域网同步功能实验性首次使用前建议先备份一次列表此功能需要配合PC端使用移动端与PC端处在同一个局域网路由器的网络下时可以多端实时同步歌曲列表使用问题请看"常见问题"。
### 优化
- 优化退出应用的机制,现在在需要退出应用的场景将会完全退出应用
### 修复
- 修复某些情况下出现恢复播放信息失败的问题

View File

@ -16,7 +16,7 @@ const VersionModal = ({ componentId }) => {
const handleRejct = () => {
exitApp()
Navigation.dismissOverlay(componentId)
// Navigation.dismissOverlay(componentId)
}
const handleConfirm = () => {

View File

@ -3,7 +3,7 @@ import { getStore } from '@/store'
import { action as playerAction, STATUS } from '@/store/modules/player'
import { isTempTrack } from './utils'
import { play as lrcPlay, pause as lrcPause } from '@/utils/lyric'
import { exitApp } from '@/utils/tools'
import { exitApp } from '@/utils/utils'
const store = getStore()
@ -50,9 +50,9 @@ let errorTime = 0
// },
// }
// 销毁播放器并退出
const stopPlay = () => {
const handleExitApp = async() => {
global.isPlayedExit = false
TrackPlayer.pause()
await TrackPlayer.pause()
store.dispatch(playerAction.destroy()).finally(() => {
exitApp()
})
@ -88,8 +88,20 @@ export default async() => {
TrackPlayer.addEventListener('remote-stop', async() => {
console.log('remote-stop')
await store.dispatch(playerAction.destroy())
exitApp()
handleExitApp()
})
TrackPlayer.addEventListener('remote-duck', async({ permanent, paused, ducking }) => {
console.log('remote-duck')
if (paused) {
store.dispatch(playerAction.setStatus({ status: STATUS.pause, text: '已暂停' }))
lrcPause()
} else {
store.dispatch(playerAction.setStatus({ status: STATUS.playing, text: '播放中...' }))
TrackPlayer.getPosition().then(position => {
lrcPlay(position * 1000)
})
}
})
TrackPlayer.addEventListener('playback-error', async err => {
@ -164,11 +176,11 @@ export default async() => {
console.log('playback-state', info)
break
}
if (global.isPlayedExit) return stopPlay()
if (global.isPlayedExit) return handleExitApp()
})
TrackPlayer.addEventListener('playback-track-changed', async info => {
// console.log('nextTrack====>', info)
if (global.isPlayedExit) return stopPlay()
if (global.isPlayedExit) return handleExitApp()
trackId = await TrackPlayer.getCurrentTrack()
if (trackId && isTempTrack(trackId)) {

View File

@ -1,5 +1,5 @@
import { Alert } from 'react-native'
import { exitApp } from '@/utils/tools'
import { exitApp } from '@/utils/utils'
import { setJSExceptionHandler, setNativeExceptionHandler } from 'react-native-exception-handler'
import { log } from '@/utils/log'

View File

@ -2,7 +2,7 @@ import { useEffect, useState } from 'react'
import BackgroundTimer from 'react-native-background-timer'
import { getStore } from '@/store'
import { action as playerAction, STATUS } from '@/store/modules/player'
import { exitApp } from '@/utils/tools'
import { exitApp } from '@/utils/utils'
const isStop = status => {
switch (status) {
@ -23,8 +23,10 @@ const timeoutTools = {
if (state.common.setting.player.timeoutExitPlayed && !isStop(state.player.status)) {
global.isPlayedExit = true
} else {
store.dispatch(playerAction.destroy()).finally(() => {
exitApp()
store.dispatch(playerAction.pauseMusic()).finally(() => {
store.dispatch(playerAction.destroy()).finally(() => {
exitApp()
})
})
}
},