package
com.kbtx;
import
android.app.Activity;
import
android.app.PendingIntent;
import
android.content.Intent;
import
android.content.IntentSender;
import
android.content.pm.PackageInstaller;
import
android.net.Uri;
import
android.os.Build;
import
android.os.Bundle;
import
android.util.Log;
import
android.view.View;
import
androidx.annotation.Nullable;
import
androidx.annotation.RequiresApi;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.OutputStream;
public
class
AppInstaller
extends
Activity
implements
View.OnClickListener {
private
static
final
String action =
"com.kbtx.Install_APK"
;
private
boolean
should_kill =
false
;
@Override
protected
void
onCreate([url=home.php?mod=space&uid=
1043391
]
@nullable
[/url] Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
Uri uri = getIntent().getData();
should_kill =
false
;
if
(uri !=
null
&& uri.getScheme().equals(
"file"
)) {
File file =
new
File(uri.getPath());
try
{
Install(file);
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
@Override
protected
void
onResume() {
super
.onResume();
if
(should_kill) finish();
should_kill =
true
;
}
@RequiresApi
(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected
void
onNewIntent(Intent intent) {
super
.onNewIntent(intent);
if
(intent==
null
|| !intent.getAction().equals(action))
return
;
int
status = intent.getExtras().getInt(PackageInstaller.EXTRA_STATUS);
switch
(status){
case
PackageInstaller.STATUS_PENDING_USER_ACTION:{
startActivity((Intent) intent.getExtras().get(Intent.EXTRA_INTENT));
break
;
}
case
PackageInstaller.STATUS_SUCCESS:{
Log.i(
"FeiGe"
,
"应用安装成功"
);
break
;
}
case
PackageInstaller.STATUS_FAILURE:{
Log.i(
"FeiGe"
,
"应用安装失败"
);
break
;
}
}
}
public
void
Install(File apk)
throws
IOException {
if
(Build.VERSION.SDK_INT <
30
)
return
;
PackageInstaller packageInstaller = getPackageManager().getPackageInstaller();
PackageInstaller.SessionParams params =
new
PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL);
int
sessionId = packageInstaller.createSession(params);
PackageInstaller.Session session = packageInstaller.openSession(sessionId);
addApkToInstallSession(apk,session);
Intent intent =
new
Intent(
this
,AppInstaller.
class
);
intent.setAction(action);
PendingIntent pendingIntent = PendingIntent.getActivity(
this
,
0
,intent,
0
);
IntentSender statRecv = pendingIntent.getIntentSender();
session.commit(statRecv);
}
private
void
addApkToInstallSession(File apk, PackageInstaller.Session session){
if
(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
Log.e(
"feige"
,
"准备为新版安卓设备安装apk..."
);
Log.e(
"feige"
,
"安装包路径: "
+ apk.getAbsolutePath());
try
(OutputStream packageInSession = session.openWrite(
"package"
,
0
,-
1
);
InputStream is =
new
FileInputStream(apk)
){
byte
[] buffer =
new
byte
[
16384
];
int
n;
while
((n = is.read(buffer)) >=
0
){
packageInSession.write(buffer,
0
,n);
}
}
catch
(IOException e){
e.printStackTrace();
Log.e(
"feige"
,
"传输进程出错!"
);
return
;
}
Log.e(
"feige"
,
"数据传输完毕"
);
}
}
@Override
public
void
onClick(View view) {
finish();
}
}