android 中静默安装APK实现代码
来源:互联网 发布:淘宝唯美的刻字印章 编辑:程序博客网 时间:2024/05/21 04:02
/*
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
package com.android.packageinstaller;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.RemoteException;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import android.content.pm.IPackageManager;
import android.content.pm.IPackageInstallObserver2;
import android.content.pm.VerificationParams;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
public class SilentInstallReceiver extends BroadcastReceiver {
private final String TAG = "SilentInstallReceiver";
private String InstallAction = "mia_control_installApp";
Context mContext;
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
String action = intent.getAction();
String path = intent.getStringExtra("apkName");
Log.i(TAG,"action :"+action);
Log.i(TAG,"path :"+path);
if (InstallAction.equals(action)) {
install(path);
}
}
public void install(String path) {
File fileName = new File(path);
try {
if(fileName.exists()){
Class<?> clazz = Class.forName("android.os.ServiceManager");
Method method = clazz.getMethod("getService", String.class);
IBinder iBinder = (IBinder) method.invoke(null, "package");
IPackageManager ipm = IPackageManager.Stub.asInterface(iBinder);
@SuppressWarnings("deprecation")
VerificationParams verificationParams = new VerificationParams(null, null, null, VerificationParams.NO_UID, null);
ipm.installPackage(fileName.getPath(), new PackageInstallObserver(), 2, null, verificationParams, null);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i(TAG,"Exception :"+e);
}
}
class PackageInstallObserver extends IPackageInstallObserver2.Stub {
@Override
public void onUserActionRequired(Intent intent) throws RemoteException {
// TODO Auto-generated method stub
}
@Override
public void onPackageInstalled(String basePackageName, int returnCode, String msg, Bundle extras) throws RemoteException {
Log.i(TAG,"onPackageInstalled() basePackageName :"+basePackageName);
Log.i(TAG,"onPackageInstalled() returnCode :"+returnCode);
Log.i(TAG,"onPackageInstalled() msg :"+msg);
MysendBroadcast(basePackageName,returnCode);
Log.e("keven","basePackageName =="+basePackageName+"returnCode =="+returnCode);
if ("com.cnlaunch.x431.ProMiNi".equals(basePackageName)
&& returnCode == 1) {
Log.e("keven","*********");
Intent mIntent = new Intent();
ComponentName cn = new ComponentName(
"com.cnlaunch.x431.ProMiNi",
"com.cnlaunch.x431.ProMiNi.WelcomeActivity");
mIntent.setComponent(cn);
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
mContext.startActivity(mIntent);
}
}
}
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
package com.android.packageinstaller;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.RemoteException;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import android.content.pm.IPackageManager;
import android.content.pm.IPackageInstallObserver2;
import android.content.pm.VerificationParams;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
public class SilentInstallReceiver extends BroadcastReceiver {
private final String TAG = "SilentInstallReceiver";
private String InstallAction = "mia_control_installApp";
Context mContext;
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
String action = intent.getAction();
String path = intent.getStringExtra("apkName");
Log.i(TAG,"action :"+action);
Log.i(TAG,"path :"+path);
if (InstallAction.equals(action)) {
install(path);
}
}
public void install(String path) {
File fileName = new File(path);
try {
if(fileName.exists()){
Class<?> clazz = Class.forName("android.os.ServiceManager");
Method method = clazz.getMethod("getService", String.class);
IBinder iBinder = (IBinder) method.invoke(null, "package");
IPackageManager ipm = IPackageManager.Stub.asInterface(iBinder);
@SuppressWarnings("deprecation")
VerificationParams verificationParams = new VerificationParams(null, null, null, VerificationParams.NO_UID, null);
ipm.installPackage(fileName.getPath(), new PackageInstallObserver(), 2, null, verificationParams, null);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i(TAG,"Exception :"+e);
}
}
class PackageInstallObserver extends IPackageInstallObserver2.Stub {
@Override
public void onUserActionRequired(Intent intent) throws RemoteException {
// TODO Auto-generated method stub
}
@Override
public void onPackageInstalled(String basePackageName, int returnCode, String msg, Bundle extras) throws RemoteException {
Log.i(TAG,"onPackageInstalled() basePackageName :"+basePackageName);
Log.i(TAG,"onPackageInstalled() returnCode :"+returnCode);
Log.i(TAG,"onPackageInstalled() msg :"+msg);
MysendBroadcast(basePackageName,returnCode);
Log.e("keven","basePackageName =="+basePackageName+"returnCode =="+returnCode);
if ("com.cnlaunch.x431.ProMiNi".equals(basePackageName)
&& returnCode == 1) {
Log.e("keven","*********");
Intent mIntent = new Intent();
ComponentName cn = new ComponentName(
"com.cnlaunch.x431.ProMiNi",
"com.cnlaunch.x431.ProMiNi.WelcomeActivity");
mIntent.setComponent(cn);
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
mContext.startActivity(mIntent);
}
}
}
以上是实现代码,通过发广播的形式将安装apk路径以广播的形式发送,这样就实现了静默安装
0 0
- android 中静默安装APK实现代码
- 代码中执行apk静默安装
- android静默安装apk已经成功实现
- android静默安装apk已经成功实现
- android静默安装apk已经成功实现
- Android下实现静默安装指定APK
- android静默安装apk已经成功实现
- Android 实现Apk的后台静默安装
- Android 无需root实现APK静默安装
- android 静默安装apk
- android 静默安装Apk
- Android静默安装apk
- android静默安装apk
- android apk静默安装
- Android静默安装apk
- android APK静默安装
- Android 静默安装apk
- 代码中执行apk安装、卸载、更新以及静默安装
- 华西乐购
- python+selenium firefox 驱动
- 如何下载和安装、使用xshell(安全终端模拟软件)工具
- python学习笔记4-字典
- Java中File的getAbsolutePath()包含文件的名称
- android 中静默安装APK实现代码
- app全fragment混少量Activity的切换逻辑
- 第八届蓝桥杯JAVAB组第四题
- WHAT
- 系统安装名词大全
- 最近整理的一些msm8909平台英文文档1
- gas-station
- 计算机科学界名言
- js ==与===区别