【移动应用开发技术】怎么在Android P中实现静默安装_第1页
【移动应用开发技术】怎么在Android P中实现静默安装_第2页
【移动应用开发技术】怎么在Android P中实现静默安装_第3页
【移动应用开发技术】怎么在Android P中实现静默安装_第4页
【移动应用开发技术】怎么在Android P中实现静默安装_第5页
免费预览已结束,剩余2页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】怎么在AndroidP中实现静默安装

怎么在AndroidP中实现静默安装?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。Android9.0无法通过以下两种方式实现静默安装:1.runtime执行shellcmd2.PackageInstall反射机制但是Google已经给我们推荐了相关的APIDemos,所以建议大家多看看源码~在frameworks/base/core/java/android/content/pm/PackageInstaller.java有段关于该类的介绍:TheApiDemosprojectcontainsexamplesofusingthisAPI:<code>ApiDemos/src/com/example/android/apis/content/InstallApk*.java</code>./**

*

Offers

the

ability

to

install,

upgrade,

and

remove

applications

on

the

*

device.

This

includes

support

for

apps

packaged

either

as

a

single

*

"monolithic"

APK,

or

apps

packaged

as

multiple

"split"

APKs.

*

<p>

*

An

app

is

delivered

for

installation

through

a

*

{@link

PackageInstaller.Session},

which

any

app

can

create.

Once

the

session

*

is

created,

the

installer

can

stream

one

or

more

APKs

into

place

until

it

*

decides

to

either

commit

or

destroy

the

session.

Committing

may

require

user

*

intervention

to

complete

the

installation.

*

<p>

*

Sessions

can

install

brand

new

apps,

upgrade

existing

apps,

or

add

new

splits

*

into

an

existing

app.

*

<p>

*

Apps

packaged

as

multiple

split

APKs

always

consist

of

a

single

"base"

APK

*

(with

a

{@code

null}

split

name)

and

zero

or

more

"split"

APKs

(with

unique

*

split

names).

Any

subset

of

these

APKs

can

be

installed

together,

as

long

as

*

the

following

constraints

are

met:

*

<ul>

*

<li>All

APKs

must

have

the

exact

same

package

name,

version

code,

and

signing

*

certificates.

*

<li>All

APKs

must

have

unique

split

names.

*

<li>All

installations

must

contain

a

single

base

APK.

*

</ul>

*

<p>

*

###########此处告诉开发者如何调用API安装apk##############

*

The

ApiDemos

project

contains

examples

of

using

this

API:

*

<code>ApiDemos/src/com/example/android/apis/content/InstallApk*.java</code>.

*/

public

class

PackageInstaller翻阅源码,InstallApk*.java相关的一共两个demoInstallApkSessionApi.java//静默安装InstallApk.java//普通安装,调用系统installintent进行安装下面是InstallApkSessionApi.java的具体demopackage

com.example.android.apis.content;

//

Need

the

following

import

to

get

access

to

the

app

resources,

since

this

//

class

is

in

a

sub-package.

import

com.example.android.apis.R;

import

android.app.Activity;

import

android.app.PendingIntent;

import

android.content.Context;

import

android.content.Intent;

import

android.content.IntentSender;

import

android.content.pm.PackageInstaller;

import

android.os.Bundle;

import

android.view.View;

import

android.view.View.OnClickListener;

import

android.widget.Button;

import

android.widget.Toast;

import

java.io.IOException;

import

java.io.InputStream;

import

java.io.OutputStream;

/**

*

Demonstration

of

package

installation

and

uninstallation

using

the

package

installer

Session

*

API.

*

*

@see

InstallApk

for

a

demo

of

the

original

(non-Session)

API.

*/

public

class

InstallApkSessionApi

extends

Activity

{

private

static

final

String

PACKAGE_INSTALLED_ACTION

=

"com.example.android.apis.content.SESSION_API_PACKAGE_INSTALLED";

@Override

protected

void

onCreate(Bundle

savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.install_apk_session_api);

//

Watch

for

button

clicks.

Button

button

=

(Button)

findViewById(R.id.install);

button.setOnClickListener(new

OnClickListener()

{

public

void

onClick(View

v)

{

PackageInstaller.Session

session

=

null;

try

{

PackageInstaller

packageInstaller

=

getPackageManager().getPackageInstaller();

PackageInstaller.SessionParams

params

=

new

PackageInstaller.SessionParams(

PackageInstaller.SessionParams.MODE_FULL_INSTALL);

int

sessionId

=

packageInstaller.createSession(params);

session

=

packageInstaller.openSession(sessionId);

addApkToInstallSession("HelloActivity.apk",

session);

//

Create

an

install

status

receiver.

Context

context

=

InstallApkSessionApi.this;

Intent

intent

=

new

Intent(context,

InstallApkSessionApi.class);

intent.setAction(PACKAGE_INSTALLED_ACTION);

//此处也可以使用getBoradcast或者getService回调通知

PendingIntent

pendingIntent

=

PendingIntent.getActivity(context,

0,

intent,

0);

IntentSender

statusReceiver

=

pendingIntent.getIntentSender();

//

Commit

the

session

(this

will

start

the

installation

workflow).

mit(statusReceiver);

}

catch

(IOException

e)

{

throw

new

RuntimeException("Couldn't

install

package",

e);

}

catch

(RuntimeException

e)

{

if

(session

!=

null)

{

session.abandon();

}

throw

e;

}

}

});

}

private

void

addApkToInstallSession(String

assetName,

PackageInstaller.Session

session)

throws

IOException

{

//

It's

recommended

to

pass

the

file

size

to

openWrite().

Otherwise

installation

may

fail

//

if

the

disk

is

almost

full.

try

(OutputStream

packageInSession

=

session.openWrite("package",

0,

-1);

InputStream

is

=

getAssets().open(assetName))

{

byte[]

buffer

=

new

byte[16384];

int

n;

while

((n

=

is.read(buffer))

>=

0)

{

packageInSession.write(buffer,

0,

n);

}

}

}

//

Note:

this

Activity

must

run

in

singleTop

launchMode

for

it

to

be

able

to

receive

the

intent

//

in

onNewIntent().

@Override

protected

void

onNewIntent(Intent

intent)

{

Bundle

extras

=

intent.getExtras();

if

(PACKAGE_INSTALLED_ACTION.equals(intent.getAction()))

{

int

status

=

extras.getInt(PackageInstaller.EXTRA_STATUS);

String

message

=

extras.getString(PackageInstaller.EXTRA_STATUS_MESSAGE);

switch

(status)

{

case

PackageInstaller.STATUS_PENDING_USER_ACTION:

//

This

test

app

isn't

privileged,

so

the

user

has

to

confirm

the

install.

Intent

confirmIntent

=

(Intent)

extras.get(Intent.EXTRA_INTENT);

startActivity(confirmIntent);

break;

case

PackageInstaller.STATUS_SUCCESS:

Toast.makeText(this,

"Install

succeeded!",

Toast.LENGTH_SHORT).show();

break;

case

PackageInstaller.STATUS_FAILURE:

case

PackageInstaller.STATUS_FAILURE_ABORTED:

case

PackageInstaller.STATUS_FAILURE_BLOCKED:

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论