我正在尝试编写一个函数来更新我的Android应用程序,而无需在应用程序内进行谷歌播放。我的代码非常基于this 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);
}
}
我觉得问题出在&#34; this.mContext.startActivity(i)&#34;在InstallUpdate asynctask postexecute中。我不知道使用MainActivity的上下文是否正确,或者从asynctask调用它是否会导致问题。我一直试图在网上找到一个解决方案大约一个星期,并且一直空着。我正在学习java和android编程,因为我一直在编写这个程序,所以我不能100%确定我在做什么,但这是我没有做过的第一个问题。能够自己找到解决方案。
答案 0 :(得分:0)
然后你的下载不成功检查下载后的apk大小必须与服务器中的大小相同