I am trying to install a 3rd party free app through code. Here's the file which downloads the apk from the web-site, stores it on a SD Card.
Using java Syntax Highlighting
- public class DownloadFile extends Activity implements View.OnClickListener {
- private static final int WAIT_DIALOG_KEY = 123;
- private ProgressDialog waitDialog;
- private String url, root = "/data/data/wms.apps.android/", filename;
- private boolean isDownloaded = false;
- private Bitmap bitmap;
- private ImageView image;
- private File downloadFile;
- private TextView tv;
- private class DownloadTask extends AsyncTask<Void, String, Void> {
- @Override
- protected Void doInBackground(Void... arg0) {
- // TODO Auto-generated method stub
- try {
- URL imageUrl = new URL(url);
- InputStream in = openHttpConnection(imageUrl);
- // bitmap = BitmapFactory.decodeStream(in);
- ByteArrayBuffer buffer = new ByteArrayBuffer(500);
- int byteRead = 0;
- while ((byteRead = in.read()) != -1) {
- buffer.append(byteRead);
- }
- String filename = Environment.getExternalStorageDirectory()
- + "/megan.apk";
- downloadFile = new File(filename);
- // downloadFile = new File(root, "test.apk");
- FileOutputStream out = new FileOutputStream(downloadFile);
- out.write(buffer.toByteArray());
- in.close();
- out.close();
- isDownloaded = true;
- } catch (Exception e) {
- }
- return null;
- }
- private InputStream openHttpConnection(URL aUrl) {
- InputStream in = null;
- try {
- URLConnection connection = aUrl.openConnection();
- HttpURLConnection httpConnection = (HttpURLConnection) connection;
- httpConnection.setAllowUserInteraction(false);
- httpConnection.setInstanceFollowRedirects(true);
- httpConnection.setRequestMethod("GET");
- httpConnection.connect();
- in = httpConnection.getInputStream();
- } catch (Exception e) {
- }
- return in;
- }
- protected void onPreExecute() {
- showDialog(WAIT_DIALOG_KEY);
- }
- protected void onPostExecute(Void voids) {
- // image.setImageBitmap(bitmap);
- if (isDownloaded)
- tv.setText("APK downloaded");
- waitDialog.dismiss();
- // Toast.makeText(DownloadFile.this, "Done", Toast.LENGTH_SHORT)
- // .show();
- }
- }
- protected Dialog onCreateDialog(int id) {
- switch (id) {
- case WAIT_DIALOG_KEY: {
- waitDialog = new ProgressDialog(this);
- waitDialog.setMessage("Please wait while loading...");
- waitDialog.setIndeterminate(true);
- waitDialog.setCancelable(true);
- return waitDialog;
- }
- }
- return null;
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.list);
- Bundle intentData = this.getIntent().getExtras();
- url = intentData.getString("url");
- }
- public void onStart() {
- super.onStart();
- new DownloadTask().execute();
- tv = (TextView) findViewById(R.id.label);
- image = (ImageView) findViewById(R.id.image);
- Button aButton = (Button) findViewById(R.id.install);
- aButton.setOnClickListener(this);
- if (isDownloaded)
- tv.setText("Image downloaded");
- else
- tv.setText("Image download failed");
- }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
Then when the user clicks on the Install button I am running this code
Using java Syntax Highlighting
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- boolean success;
- int result = Settings.Secure.getInt(getContentResolver(),
- Settings.Secure.INSTALL_NON_MARKET_APPS, 0);
- if (result == 0)
- success = Settings.Secure.putString(getContentResolver(),
- Settings.Secure.INSTALL_NON_MARKET_APPS, "1");
- Intent intent = new Intent();
- intent.setAction(android.content.Intent.ACTION_VIEW);
- intent.setDataAndType(Uri.fromFile(downloadFile),
- "application/vnd.android.package-archive");
- startActivity(intent);
- }
- } //End of class file
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
I read somewhere in order to install 3rd party apps we need to set INSTALL_NON_MARKET_APPS. For setting these settings , is it needed to use any permission in the manifest file?
Also, when I did a logcat the error was
- Code: Select all
Unable to read AndroidManifest.xml of /sdcard/test.apk and then it threw a IOException
What exactly could be the problem? I would very much apreciate it if somebody would tell me what are the steps needed to install a 3rd party application.
Thanks



