Sunday, 21 July 2013

closing Background processes from Android app.

As of Android 2.2 and above you can only kill background processes. If you want to kill a background process you can use the following android snacks:

         public void KillBackgroundProcess(String packagename)
          {
            
         private ActivityManager objactivitymngr =  
             (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); 
 
             objactivitymngr.killBackgroundProcesses(packagename);

           }

Note :-  Also This permission is required in your android manifest
         
               android.permission.KILL_BACKGROUND_PROCESSES

Tuesday, 16 April 2013

How To download image from URL?

For downloading image from url there are two way :-
 1- using Thread and Handler
 2- using AsyncTask
Here we will use by AsyncTask:-

  1- create AsynkTask

                                    private DownLoader extends AsyncTask<Void,Void,Void>
                                       {
                                         }

 call AsyncTask by passing image url whole code snaps is as:-

  private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
        private final ProgressDialog objprogress = new ProgressDialog(
                DealDetailPage.this);
      
        @Override
        protected void onPreExecute() {
            objprogress.setMessage("Loading...");
            objprogress.show();
        }

        @Override
        protected BitmapdoInBackground(String... params) {
            try {
                status = params[1];
                URL feedImage = new URL(params[0]);
                HttpURLConnection conn = (HttpURLConnection) feedImage
                        .openConnection();
                InputStream is = conn.getInputStream();
                android.graphics.BitmapFactory.Options op = new android.graphics.BitmapFactory.Options();
                op.inTempStorage = new byte[16 * 1024];
                op.inSampleSize = 0;
               
                Bitmap imageUser = BitmapFactory.decodeStream(is, null, op);
               

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return imageUser ;
        }
@Override
        protected void onPostExecute(Bitmap result) {
            super.onPostExecute(result);
          // use bitmap here
                           
 }
}