Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
android_highgold_pro
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
android
android_highgold_pro
Commits
d3788873
Commit
d3788873
authored
Mar 10, 2021
by
jyx
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
强升代码优化
parent
32edb132
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
84 additions
and
74 deletions
+84
-74
VersionUpdatePresenter.java
...ints/goldspace/mvp/presenters/VersionUpdatePresenter.java
+48
-45
UpdateService.java
.../main/java/com/mints/goldspace/service/UpdateService.java
+36
-29
No files found.
app/src/main/java/com/mints/goldspace/mvp/presenters/VersionUpdatePresenter.java
View file @
d3788873
...
...
@@ -8,7 +8,9 @@ import android.content.Intent;
import
android.content.ServiceConnection
;
import
android.net.Uri
;
import
android.os.IBinder
;
import
androidx.appcompat.app.AlertDialog
;
import
android.text.TextUtils
;
import
android.text.method.ScrollingMovementMethod
;
import
android.view.KeyEvent
;
...
...
@@ -56,6 +58,7 @@ public class VersionUpdatePresenter {
Button
btnDuUpdate
;
private
Subscription
subscription
;
private
UpdateService
mUpdateService
;
public
void
updateVersion
(
Activity
activity
)
{
if
(
activity
==
null
)
...
...
@@ -177,64 +180,61 @@ public class VersionUpdatePresenter {
if
(!
forceUpgrade
)
{
// 非强升
btnDuUpdate
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
v
)
{
if
(
dialogDetails
!=
null
&&
dialogDetails
.
isShowing
())
{
dialogDetails
.
dismiss
();
AppConfig
.
app_updateing
=
false
;
}
boolean
url
=
CommonUtils
.
isUrl
(
appFileUrl
);
if
(
url
)
{
updateApp
(
appFileUrl
,
granted
);
}
else
{
ToastUtil
.
show
(
loanApplication
.
getApplicationContext
(),
"升级地址有误,请联系管理员!"
);
}
btnDuUpdate
.
setOnClickListener
(
v
->
{
if
(
dialogDetails
!=
null
&&
dialogDetails
.
isShowing
())
{
dialogDetails
.
dismiss
();
AppConfig
.
app_updateing
=
false
;
}
boolean
url
=
CommonUtils
.
isUrl
(
appFileUrl
);
if
(
url
)
{
updateApp
(
appFileUrl
,
granted
);
}
else
{
ToastUtil
.
show
(
loanApplication
.
getApplicationContext
(),
"升级地址有误,请联系管理员!"
);
}
});
ivDilogKnow
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
v
)
{
if
(
dialogDetails
!=
null
&&
dialogDetails
.
isShowing
())
{
dialogDetails
.
dismiss
();
}
ivDilogKnow
.
setOnClickListener
(
v
->
{
if
(
dialogDetails
!=
null
&&
dialogDetails
.
isShowing
())
{
dialogDetails
.
dismiss
();
}
});
}
else
{
ivDilogKnow
.
setVisibility
(
View
.
GONE
);
btnDuUpdate
.
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
v
)
{
boolean
url
=
CommonUtils
.
isUrl
(
appFileUrl
);
if
(
url
)
{
updateApp
(
appFileUrl
,
granted
);
//如果已经获取到sd卡存储权限则把按钮设置成不可点击
if
(
granted
)
{
btnDuUpdate
.
setEnabled
(
false
);
}
btnDuUpdate
.
setOnClickListener
(
v
->
{
if
(
TextUtils
.
equals
(
btnDuUpdate
.
getText
(),
"立即安装"
))
{
if
(
mUpdateService
!=
null
)
{
mUpdateService
.
autoInstallApk
();
}
else
{
ToastUtil
.
show
(
loanApplication
.
getApplicationContext
(),
"升级地址有误,请联系管理员!"
);
ToastUtil
.
show
(
loanApplication
.
getApplicationContext
(),
"安装失败!"
);
System
.
exit
(
0
);
}
return
;
}
boolean
url
=
CommonUtils
.
isUrl
(
appFileUrl
);
if
(
url
)
{
updateApp
(
appFileUrl
,
granted
);
//如果已经获取到sd卡存储权限则把按钮设置成不可点击
if
(
granted
)
{
btnDuUpdate
.
setEnabled
(
false
);
}
}
else
{
ToastUtil
.
show
(
loanApplication
.
getApplicationContext
(),
"升级地址有误,请联系管理员!"
);
}
});
dialogDetails
.
setCanceledOnTouchOutside
(
false
);
dialogDetails
.
setOnKeyListener
(
new
DialogInterface
.
OnKeyListener
()
{
@Override
public
boolean
onKey
(
DialogInterface
dialog
,
int
keyCode
,
KeyEvent
event
)
{
if
(
keyCode
==
KeyEvent
.
KEYCODE_BACK
)
{
return
true
;
}
return
false
;
dialogDetails
.
setOnKeyListener
((
dialog
,
keyCode
,
event
)
->
{
if
(
keyCode
==
KeyEvent
.
KEYCODE_BACK
)
{
return
true
;
}
return
false
;
});
}
...
...
@@ -254,15 +254,15 @@ public class VersionUpdatePresenter {
public
void
onServiceConnected
(
ComponentName
name
,
IBinder
service
)
{
// //返回一个MsgService对象
UpdateService
u
pdateService
=
((
UpdateService
.
UpdateBinder
)
service
).
getService
();
mU
pdateService
=
((
UpdateService
.
UpdateBinder
)
service
).
getService
();
//
// //注册回调接口来接收下载进度的变化
u
pdateService
.
setUpdateNotification
(
new
UpdateService
.
UpdateNotification
()
{
mU
pdateService
.
setUpdateNotification
(
new
UpdateService
.
UpdateNotification
()
{
@Override
public
void
updateProgress
(
int
progress
)
{
if
(
pbDuUpdating
!=
null
)
{
pbDuUpdating
.
setProgress
(
progress
);
if
(
btnDuUpdate
!=
null
&&
progress
==
100
)
{
if
(
btnDuUpdate
!=
null
)
{
btnDuUpdate
.
setEnabled
(
true
);
}
}
...
...
@@ -270,7 +270,10 @@ public class VersionUpdatePresenter {
@Override
public
void
finishProgress
()
{
if
(
btnDuUpdate
!=
null
)
{
btnDuUpdate
.
setEnabled
(
true
);
btnDuUpdate
.
setText
(
"立即安装"
);
}
}
});
...
...
app/src/main/java/com/mints/goldspace/service/UpdateService.java
View file @
d3788873
...
...
@@ -12,13 +12,17 @@ import android.os.Build;
import
android.os.Handler
;
import
android.os.IBinder
;
import
android.os.Message
;
import
androidx.core.app.NotificationCompat
;
import
androidx.core.content.FileProvider
;
import
android.widget.RemoteViews
;
import
com.mints.goldspace.MintsApplication
;
import
com.mints.goldspace.R
;
import
com.mints.goldspace.common.Constant
;
import
com.mints.goldspace.ui.activitys.MainActivity
;
import
com.mints.goldspace.utils.ToastUtil
;
import
java.io.File
;
import
java.io.FileOutputStream
;
...
...
@@ -52,10 +56,8 @@ public class UpdateService extends Service {
// updateFile = FileUtils.createAttachFile(tempFilepath, tempFilename + ".apk");// 创建文件
updateFile
=
new
File
(
this
.
getCacheDir
().
getPath
()
+
"/"
+
tempFilename
+
".apk"
);
// 创建文件
//创建的file不为空的话
if
(
updateFile
!=
null
)
{
createNotification
();
// 首次创建
createThread
();
// 线程下载
}
createNotification
();
// 首次创建
createThread
();
// 线程下载
}
else
{
stopService
();
}
...
...
@@ -183,25 +185,35 @@ public class UpdateService extends Service {
}
};
final
Message
message
=
new
Message
();
new
Thread
(
new
Runnable
()
{
public
void
run
()
{
try
{
long
downloadSize
=
downloadUpdateFile
(
handler
,
down_url
,
updateFile
.
toString
());
if
(
downloadSize
>
0
)
{
// 下载成功
message
.
what
=
DOWN_OK
;
handler
.
sendMessage
(
message
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
message
.
what
=
DOWN_ERROR
;
new
Thread
(()
->
{
try
{
long
downloadSize
=
downloadUpdateFile
(
handler
,
down_url
,
updateFile
.
toString
());
if
(
downloadSize
>
0
)
{
// 下载成功
message
.
what
=
DOWN_OK
;
handler
.
sendMessage
(
message
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
message
.
what
=
DOWN_ERROR
;
handler
.
sendMessage
(
message
);
}
}).
start
();
}
/**
* 安装 apk 文件
*/
public
void
autoInstallApk
()
{
if
(
updateFile
!=
null
)
{
autoInstallApk
(
updateFile
);
}
else
{
ToastUtil
.
show
(
MintsApplication
.
getContext
(),
"安装失败!"
);
System
.
exit
(
0
);
}
}
/**
* 安装 apk 文件
*/
...
...
@@ -210,7 +222,7 @@ public class UpdateService extends Service {
if
(
Build
.
VERSION
.
SDK_INT
<
Build
.
VERSION_CODES
.
N
)
{
intent
.
setDataAndType
(
Uri
.
fromFile
(
file
),
"application/vnd.android.package-archive"
);
}
else
{
//Android7.0之后获取uri要用contentProvider
Uri
contentUri
=
FileProvider
.
getUriForFile
(
getApplicationContext
(),
Constant
.
MINTS_PKG_NAME
+
".fileprovider"
,
file
);
Uri
contentUri
=
FileProvider
.
getUriForFile
(
getApplicationContext
(),
Constant
.
MINTS_PKG_NAME
+
".fileprovider"
,
file
);
intent
.
setDataAndType
(
contentUri
,
"application/vnd.android.package-archive"
);
intent
.
addFlags
(
Intent
.
FLAG_GRANT_READ_URI_PERMISSION
);
}
...
...
@@ -250,7 +262,7 @@ public class UpdateService extends Service {
while
((
readsize
=
inputStream
.
read
(
buffer
))
!=
-
1
)
{
outputStream
.
write
(
buffer
,
0
,
readsize
);
downloadCount
+=
readsize
;
// 时时获取下载到的大小
/*
*
/*
* 每次增张5%
*/
if
(
updateCount
==
0
...
...
@@ -262,9 +274,7 @@ public class UpdateService extends Service {
handler
.
sendMessage
(
message
);
}
}
if
(
httpURLConnection
!=
null
)
{
httpURLConnection
.
disconnect
();
}
httpURLConnection
.
disconnect
();
inputStream
.
close
();
outputStream
.
close
();
return
downloadCount
;
...
...
@@ -274,12 +284,9 @@ public class UpdateService extends Service {
* 关闭service
*/
private
void
stopService
()
{
new
Handler
().
postDelayed
(
new
Runnable
()
{
@Override
public
void
run
()
{
notificationManager
.
cancel
(
notification_id
);
stopSelf
();
}
new
Handler
().
postDelayed
(()
->
{
notificationManager
.
cancel
(
notification_id
);
stopSelf
();
},
3000
);
}
...
...
@@ -287,7 +294,7 @@ public class UpdateService extends Service {
* apk更新进度通知
*/
public
interface
UpdateNotification
{
public
void
updateProgress
(
int
progress
);
void
updateProgress
(
int
progress
);
void
finishProgress
();
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment