/**
* 静默安装的实现类,调用install()方法执行具体的静默安装逻辑。
* 原文地址:http://blog.csdn.net/guolin_blog/article/details/47803149
* @author guolin
* @since 2015/12/7
*/
public class SilentInstall {
/**
* 执行具体的静默安装逻辑,需要手机ROOT。
* @param apkPath
* 要安装的apk文件的路径
* @return 安装成功返回true,安装失败返回false。
*/
public boolean install(String apkPath) {
boolean result = false;
DataOutputStream dataOutputStream = null;
BufferedReader errorStream = null;
try {
// 申请su权限
Process process = Runtime.getRuntime().exec("su");
dataOutputStream = new DataOutputStream(process.getOutputStream());
// 执行pm install命令
String command = "pm install -r " + apkPath + "\n";
dataOutputStream.write(command.getBytes(Charset.forName("utf-8")));
dataOutputStream.flush();
dataOutputStream.writeBytes("exit\n");
dataOutputStream.flush();
process.waitFor();
errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String msg = "";
String line;
// 读取命令的执行结果
while ((line = errorStream.readLine()) != null) {
msg += line;
}
Log.d("TAG", "install msg is " + msg);
// 如果执行结果中包含Failure字样就认为是安装失败,否则就认为安装成功
if (!msg.contains("Failure")) {
result = true;
}
} catch (Exception e) {
Log.e("TAG", e.getMessage(), e);
} finally {
try {
if (dataOutputStream != null) {
dataOutputStream.close();
}
if (errorStream != null) {
errorStream.close();
}
} catch (IOException e) {
Log.e("TAG", e.getMessage(), e);
}
}
return result;
}
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.installtest.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onChooseApkFile" android:text="选择安装包" /> <TextView android:id="@+id/apkPathText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/darker_gray" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onSilentInstall" android:text="秒装" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/darker_gray" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onForwardToAccessibility" android:text="开启智能安装服务" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onSmartInstall" android:text="智能安装" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="4dp" android:orientation="horizontal"> <ImageView android:id="@+id/img" android:layout_width="32dp" android:layout_margin="4dp" android:layout_gravity="center_vertical" android:layout_height="32dp"/> <TextView android:id="@+id/name" android:textSize="18sp" android:textStyle="bold" android:layout_width="match_parent" android:gravity="center_vertical" android:layout_height="50dp"/> </LinearLayout>
public class FileExplorerActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
ListView listView;
SimpleAdapter adapter;
String rootPath = Environment.getExternalStorageDirectory().getPath();
String currentPath = rootPath;
List<Map<String, Object>> list = new ArrayList<>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_explorer);
listView = (ListView) findViewById(R.id.list_view);
adapter = new SimpleAdapter(this, list, R.layout.list_item,
new String[]{"name", "img"}, new int[]{R.id.name, R.id.img});
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
refreshListItems(currentPath);
}
private void refreshListItems(String path) {
setTitle(path);
File[] files = new File(path).listFiles();
list.clear();
if (files != null) {
for (File file : files) {
Map<String, Object> map = new HashMap<>();
if (file.isDirectory()) {
map.put("img", R.drawable.directory);
} else {
map.put("img", R.drawable.file_doc);
}
map.put("name", file.getName());
map.put("currentPath", file.getPath());
list.add(map);
}
}
adapter.notifyDataSetChanged();
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
currentPath = (String) list.get(position).get("currentPath");
File file = new File(currentPath);
if (file.isDirectory())
refreshListItems(currentPath);
else {
Intent intent = new Intent();
intent.putExtra("apk_path", file.getPath());
setResult(RESULT_OK, intent);
finish();
}
}
@Override
public void onBackPressed() {
if (rootPath.equals(currentPath)) {
super.onBackPressed();
} else {
File file = new File(currentPath);
currentPath = file.getParentFile().getPath();
refreshListItems(currentPath);
}
}
}
/**
* 仿360手机助手秒装和智能安装功能的主Activity。
* 原文地址:http://blog.csdn.net/guolin_blog/article/details/47803149
* @author guolin
* @since 2015/12/7
*/
public class MainActivity extends AppCompatActivity {
TextView apkPathText;
String apkPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
apkPathText = (TextView) findViewById(R.id.apkPathText);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0 && resultCode == RESULT_OK) {
apkPath = data.getStringExtra("apk_path");
apkPathText.setText(apkPath);
}
}
public void onChooseApkFile(View view) {
Intent intent = new Intent(this, FileExplorerActivity.class);
startActivityForResult(intent, 0);
}
public void onSilentInstall(View view) {
if (!isRoot()) {
Toast.makeText(this, "没有ROOT权限,不能使用秒装", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(apkPath)) {
Toast.makeText(this, "请选择安装包!", Toast.LENGTH_SHORT).show();
return;
}
final Button button = (Button) view;
button.setText("安装中");
new Thread(new Runnable() {
@Override
public void run() {
SilentInstall installHelper = new SilentInstall();
final boolean result = installHelper.install(apkPath);
runOnUiThread(new Runnable() {
@Override
public void run() {
if (result) {
Toast.makeText(MainActivity.this, "安装成功!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "安装失败!", Toast.LENGTH_SHORT).show();
}
button.setText("秒装");
}
});
}
}).start();
}
public void onForwardToAccessibility(View view) {
}
public void onSmartInstall(View view) {
}
/**
* 判断手机是否拥有Root权限。
* @return 有root权限返回true,否则返回false。
*/
public boolean isRoot() {
boolean bool = false;
try {
bool = new File("/system/bin/su").exists() || new File("/system/xbin/su").exists();
} catch (Exception e) {
e.printStackTrace();
}
return bool;
}
}
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.installtest"> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".FileExplorerActivity"/> </application> </manifest>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" android:packageNames="com.android.packageinstaller" android:description="@string/accessibility_service_description" android:accessibilityEventTypes="typeAllMask" android:accessibilityFlags="flagDefault" android:accessibilityFeedbackType="feedbackGeneric" android:canRetrieveWindowContent="true" />
<resources> <string name="app_name">InstallTest</string> <string name="accessibility_service_description">智能安装服务,无需用户的任何操作就可以自动安装程序。</string> </resources>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.installtest"> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> ...... <service android:name=".MyAccessibilityService" android:label="我的智能安装" android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"> <intent-filter> <action android:name="android.accessibilityservice.AccessibilityService" /> </intent-filter> <meta-data android:name="android.accessibilityservice" android:resource="@xml/accessibility_service_config" /> </service> </application> </manifest>
/**
* 智能安装功能的实现类。
* 原文地址:http://blog.csdn.net/guolin_blog/article/details/47803149
* @author guolin
* @since 2015/12/7
*/
public class MyAccessibilityService extends AccessibilityService {
Map<Integer, Boolean> handledMap = new HashMap<>();
public MyAccessibilityService() {
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
AccessibilityNodeInfo nodeInfo = event.getSource();
if (nodeInfo != null) {
int eventType = event.getEventType();
if (eventType== AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED ||
eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
if (handledMap.get(event.getWindowId()) == null) {
boolean handled = iterateNodesAndHandle(nodeInfo);
if (handled) {
handledMap.put(event.getWindowId(), true);
}
}
}
}
}
private boolean iterateNodesAndHandle(AccessibilityNodeInfo nodeInfo) {
if (nodeInfo != null) {
int childCount = nodeInfo.getChildCount();
if ("android.widget.Button".equals(nodeInfo.getClassName())) {
String nodeContent = nodeInfo.getText().toString();
Log.d("TAG", "content is " + nodeContent);
if ("安装".equals(nodeContent)
|| "完成".equals(nodeContent)
|| "确定".equals(nodeContent)) {
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
return true;
}
} else if ("android.widget.ScrollView".equals(nodeInfo.getClassName())) {
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
}
for (int i = 0; i < childCount; i++) {
AccessibilityNodeInfo childNodeInfo = nodeInfo.getChild(i);
if (iterateNodesAndHandle(childNodeInfo)) {
return true;
}
}
}
return false;
}
@Override
public void onInterrupt() {
}
}
/**
* 仿360手机助手秒装和智能安装功能的主Activity。
* 原文地址:http://blog.csdn.net/guolin_blog/article/details/47803149
* @author guolin
* @since 2015/12/7
*/
public class MainActivity extends AppCompatActivity {
......
public void onForwardToAccessibility(View view) {
Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivity(intent);
}
public void onSmartInstall(View view) {
if (TextUtils.isEmpty(apkPath)) {
Toast.makeText(this, "请选择安装包!", Toast.LENGTH_SHORT).show();
return;
}
Uri uri = Uri.fromFile(new File(apkPath));
Intent localIntent = new Intent(Intent.ACTION_VIEW);
localIntent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(localIntent);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有