![](/img/trans.png)
[英]app:dexRelease error when generatingsigned apk in Android Studio
[英]Parsing Error when updating apk from within android app
我试图编写一个功能来更新我的android应用程序,而无需在该应用程序中使用google play。 我的代码很大程度上来自于这个 stackoverflow问题的答案。 我已经能够解决大多数已发生的问题,但是现在出现“解析错误:解析程序包时出现问题”。 我到处寻找这个问题的答案,我觉得我已经消除了明显的原因。 我知道程序包没有损坏,因为我在模拟器中运行了我的应用程序,然后使用监视器从数据/数据位置获取了apk文件,将该apk上传到了我的网站,然后将该apk下载到了我的手机上,并从下载管理器,它的工作原理。 这是代码:
MainActivity调用asynctask,以检查当前版本是否等于在线的最新版本。
public class MainActivity extends Activity {
ListView list;
//private ProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//pb.setVisibility(View.VISIBLE);
int v = 0;
try {
v = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
} catch (PackageManager.NameNotFoundException e) {
// Huh? Really?
}
new CheckUpdates(this).execute(v);
}
}
如果有较新的版本,则它将调用InstallUpdate asynctask。
private class CheckUpdates extends AsyncTask<Integer, Integer, String>{
private Context mContext;
private ProgressDialog pdia;
public CheckUpdates (Context c) {
this.mContext = c;
}
@Override
protected void onPreExecute(){
super.onPreExecute();
pdia = new ProgressDialog(mContext);
pdia.setMessage("Checking for update...");
pdia.show();
}
@Override
protected String doInBackground(Integer... params) {
return postData(params[0]);
}
@Override
protected void onPostExecute(final String responseString){
pdia.dismiss();
if (!responseString.equals("")) {
AlertDialog dialog = new AlertDialog.Builder(mContext).create();
dialog.setTitle("Confirmation");
dialog.setMessage("There is an update. Download and Install?");
dialog.setCancelable(false);
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int buttonId) {
new InstallUpdate(mContext).execute("apk url");
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int buttonId) {
}
});
dialog.setIcon(android.R.drawable.ic_dialog_alert);
dialog.show();
}
}
public String postData(Integer version) {
HttpClient httpclient = new DefaultHttpClient();
// specify the URL you want to post to
HttpPost httppost = new HttpPost("check for update php file");
HttpResponse response = null;
try {
// create a list to store HTTP variables and their values
List nameValuePairs = new ArrayList();
// add an HTTP variable and value pair
nameValuePairs.add(new BasicNameValuePair("currentVersion", Integer.toString(version)));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// send the variable and value, in other words post, to the URL
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// process execption
} catch (IOException e) {
// process execption
}
HttpEntity entity = response.getEntity();
String responseString = "";
try {
responseString = EntityUtils.toString(entity, "UTF-8");
} catch (IOException e) {
//really?
}
return responseString;
}
}
如果有更新,则会调用InstallUpdate并下载apk并尝试安装它。
public class InstallUpdate extends AsyncTask<String, Integer, String> {
private Context mContext;
private ProgressDialog pdia;
public InstallUpdate (Context c) {
this.mContext = c;
}
@Override
protected void onPreExecute(){
super.onPreExecute();
pdia = new ProgressDialog(mContext);
pdia.setMessage("Downloading update");
pdia.setIndeterminate(false);
pdia.setMax(100);
pdia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pdia.setCancelable(true);
pdia.show();
}
@Override
protected String doInBackground(String... sUrl) {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/plm/update.apk";
try {
URL url = new URL(sUrl[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
int fileLength = connection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(path);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e("YourApp", "Well that didn't work out so well...");
Log.e("YourApp", e.getMessage());
}
return path;
}
@Override
protected void onProgressUpdate(Integer... progress) {
Log.v("progress", Integer.toString(progress[0]));
pdia.setProgress(progress[0]);
}
// begin the installation by opening the resulting file
@Override
protected void onPostExecute(String path) {
pdia.dismiss();
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive" );
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Log.d("Lofting", "About to install new .apk");
this.mContext.startActivity(i);
}
}
我觉得问题出在InstallUpdate asynctask后执行中的“ this.mContext.startActivity(i)”上。 我不知道使用MainActivity的上下文是否正确,或者从asynctask调用它是否引起问题。 我一直在寻找一个在线解决方案大约一个星期,并且一直空着。 我正在编写程序时正在学习Java和android编程,所以我不确定100%正在做什么,但这是我自己无法找到解决方案的第一个问题。
然后您的下载失败,请检查apk大小,该大小必须与下载后服务器中的apk大小相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.