This is going to be a bit long but hopefully others will also benefit from this thread.
I am planning to make a private application for quite a number of people. It is NOT going to be on the google play. Thus, I am now facing a few problems: How do I download the new version and how do I re-install it?
I have read some brilliant websites about installing and uninstalling applications with another app like: http://android.amberfog.com/?p=98
and some about downloading app from online: Download a file with Android, and showing the progress in a ProgressDialog
and even this:
Auto-Update for (private) Android apps
But I can't figure out how to do what I want to. I want to create an app that can auto-update. My plan, from researching over and over, is to have 2 applications. The main one needs to be updated, while the second one is always there. Each time the main one is on, the second app is called and perform checks and if necessary, uninstall the main app. Then, it goes on to download the new version of the main app to the phone, and install it.
Here are some codes of the downloading process that I'm stuck in:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(
"http://dl.dropboxusercontent.com/u/98196898/data.json");
try{
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if(statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new
InputStreamReader (content));
String line;
while((line = reader.readLine()) != null) {
builder.append(line);
}
}
}catch (Exception e)
{
e.printStackTrace();
}
TextView tv = (TextView)findViewById(R.id.showresponse);
tv.setText(builder.toString());
try{
String answer = "";
JSONArray jsonArray = new JSONArray(builder.toString());
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
answer += jsonObject.getString("VersionNumber");
}
tv.setText(answer);
Which, doesn't work. I do not know why. My .json is made with a notepad with the following:
{
"VersionNumber": "2"
"VersionCode": "2.0"
}
And my plan is to compare the version number with the old one--> obviously if the one online has a bigger number than it needs to be updated.
And, of course, it doesn't work. I've also tried to use defaulthttpclient
and httpget
and stuff but it doesn't work. Maybe dropbox has this problem?
Next, I realized that I am wasting my time so I moved on to installing the apk ASSUMING THAT one day I will figure out how to download things from the net. I tried, and it doesn't work either. Here are the codes and problems:
String fileName = Environment.getExternalStorageDirectory()
+ "/Android/CheckWebForUpdate.apk";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
startActivity(intent);
I copy most of them here online, but it doesn't work. It prompted: "There is a problem parsing the package". I saw two ways of solving this but neither worked. I looked back it up and my codes should work. Someone mentioned that I should not have the same name, so I tried another program's name, and of course, it doesn't work. Then, I looked up again and this time I figured out that I "might" need permission android.permission.INSTALL_PACKAGES
but then I figured out that I need to root my phone (and because the app is not just for me... I need to root LOTS of phones, which just does not make sense)-- It is only for system apps.
I am stuck here in two ways. Thank you for reading. Please reply if you know how to solve these problems.