我的情况是我有100多个Android设备需要更新,这些设备没有连接到互联网而是连接到本地网络。我要做的是使用进程构建器访问本地网络上的apk并安装apk而无需用户干预。
这些设备目前尚未植根,但是因为我必须将它们全部用于此更新,无论如何,如果需要,它们都可以根植。
目前我只是从新应用程序的onCreate进程运行,当我得到代码工作时,我将把应用程序作为后台服务,对peridiocally检查网络中的新文件。
这是我尝试过的,但是我收到了IO错误,然后是设备查找该文件的设备位置列表。
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ProcessBuilder pb = new ProcessBuilder("su","-c", "'pm install -r http://192.168.0.17:8080/updateTest/my.apk'");
try {
Process shell = pb.start();
InputStream shellIn = shell.getInputStream();
String response = IOUtils.toString(shellIn, "UTF-8");
Log.w("myApp.update", response);
shellIn.close();
shell.destroy();
} catch (IOException ex) {
Log.w("myApp.update", "failed" + ex);
}
}
}
和logcat消息:
06-26 17:00:37.886: E/myApp.update(11072): failedjava.io.IOException: Error running exec(). Command: [su, -c, 'pm install -r http://192.168.0.17:8080/updateTest/myapp.apk'] Working Directory: null Environment: [ANDROID_SOCKET_zygote=9, SECONDARY_STORAGE=/storage/extSdCard:/storage/UsbDriveA:/storage/UsbDriveB:/storage/UsbDriveC:/storage/UsbDriveD:/storage/UsbDriveE:/storage/UsbDriveF, OMX_STE_ENS_COMPONENTS_DIR=/system/lib/ste_omxcomponents/, OMX_BELLAGIO_LOADER_REGISTRY=/system/omxloaders, ANDROID_BOOTLOGO=1, EXTERNAL_STORAGE=/storage/sdcard0, ANDROID_ASSETS=/system/app, ASEC_MOUNTPOINT=/mnt/asec, PATH=/system/sbin:/system/bin:/system/xbin:/sbin:/bin:/vendor/bin, LOOP_MOUNTPOINT=/mnt/obb, BOOTCLASSPATH=/system/framework/core.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/framework2.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/apache-xml.jar:/system/framework/sec_edm.jar:/system/framework/seccamera.jar, ANDROID_DATA=/data, ANDROID_ROOT=/system, LD_LIBRARY_PATH=/system/lib:/lib:/usr/lib:/system/lib/ste_omxcomponents/:/vendor/lib, ANDROID_PROPERTY_WORKSPACE=8,66560]
所以有人对此有任何想法或经验吗?
更新
好的,现在我先抓取文件,然后尝试使用下面的代码进行安装:
public class MainActivity extends ActionBarActivity {
private long enqueue;
private DownloadManager dm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Query query = new Query();
query.setFilterById(enqueue);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c
.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c
.getInt(columnIndex)) {
//ImageView view = (ImageView) findViewById(R.id.imageView1);
String uriString = c
.getString(c
.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
// view.setImageURI(Uri.parse(uriString));
installapk();
}
}
}
}
private void installapk() {
ProcessBuilder pb = new ProcessBuilder("su","-c", "pm install -r " + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/sr.apk");
try {
Process shell = pb.start();
InputStream shellIn = shell.getInputStream();
String response = IOUtils.toString(shellIn, "UTF-8");
Log.e("myApp.update", response);
shellIn.close();
shell.destroy();
} catch (IOException ex) {
Log.e("myApp.update", "failed" + ex);
}
}
};
registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
onClick();
}
public void onClick() {
dm = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request req = new DownloadManager.Request(Uri.parse("http://192.168.0.62:12345/sr.apk"));
req.setTitle("sr.apk")
.setDescription("Downloading ....")
// download the package to the /sdcard/downlaod path.
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "sr.apk");
enqueue = dm.enqueue(req);
}
}
但是我仍然得到同样的错误!该设备已植根,我的权限如下:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
任何人都有了想法?!
更新2!
好的,请仔细检查并证明手机没有根据!现在已经root并尝试在我的清单中再次添加要求超级用户权限,现在下面的代码运行完成而没有错误,但是没有安装任何内容并且响应&#39;是空的吗?
public class MainActivity extends ActionBarActivity {
private long enqueue;
private DownloadManager dm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
Query query = new Query();
query.setFilterById(enqueue);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c
.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex))
{
installapk();
}
}
}
}
private void installapk() {
try {
ProcessBuilder pb = new ProcessBuilder("su","-c", "install -r " +Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/sr.apk");
Process shell = pb.start();
pb.redirectErrorStream(true);
InputStream shellIn = shell.getInputStream();
String response = IOUtils.toString(shellIn, "UTF-8");
Log.e("myApp.update", response);
shellIn.close();
shell.destroy();
} catch (IOException ex) {
Log.e("myApp.update", "failed" + ex);
}
}
};
registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
onClick();
}
public void onClick() {
dm = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request req = new DownloadManager.Request(Uri.parse("http://192.168.0.62:12345/sr.apk"));
req.setTitle("sr.apk").setDescription("Downloading ....").setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "sr.apk");
enqueue = dm.enqueue(req);
}
}
任何人都能看到我做错了什么?