Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed Help in Downloading in Background Images in Android?
    primarykey
    data
    text
    <p>I have an image view , i had written swiping , at that time of swiping,the images are downloading from Internet, so i thought i have to download the images in the background before swiping , for that which i need to use asynctask or Service or IntentService, all these will help in downloading and storing in data/data/mypackages , but still swiping gets slow in my case any idea, also convey me which one is best one, is it i'm calling in a right way</p> <p><strong>1. asynctask</strong> </p> <p><strong>2. services</strong> </p> <p><strong>3. Intent Service</strong> as shown below, </p> <p>i m confused which one is right method because still my problem not solved</p> <p><strong>Here's asynctask code sample snippet</strong></p> <pre><code>public class Demo extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new FirstTask().execute(); // calling Asynctask here } </code></pre> <p>}</p> <p>Async Task code</p> <pre><code>private class FirstTask extends AsyncTask&lt;Void, Void, Void&gt; { private final ProgressDialog dialog = new ProgressDialog(Catalogue.this); int temp = 0; // can use UI thread here protected void onPreExecute() { this.dialog.setMessage("Loading..."); this.dialog.setCancelable(false); //this.dialog.show(); System.gc(); Toast.makeText(Catalogue.this, "My Async Created", Toast.LENGTH_LONG).show(); } @Override protected Void doInBackground(Void... params) { Looper.prepare(); try { myddownloadmethod();// calling my download method } catch (Exception e) { Util.trace("Error in Async"+e.getMessage()); } Looper.loop(); return null; } protected void onPostExecute(Void result) { if (this.dialog.isShowing()) { Toast.makeText(Catalogue.this, "My Async destroyed", Toast.LENGTH_LONG).show(); Toast.makeText(Catalogue.this, "count" + temp, Toast.LENGTH_LONG).show(); this.dialog.dismiss(); } } } </code></pre> <p><strong>Here's My Service sinppet</strong></p> <pre><code>public class MyService extends Service implements Runnable { @Override public void onCreate() { super.onCreate(); Thread mythread = new Thread(this); mythread.start(); } public void run() { Looper.prepare(); try { myddownloadmethod();// calling my download method } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } Looper.loop(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } } Invoking Service public class ServicesDemo extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startService(new Intent(this, MyService.class)); } } </code></pre> <p><strong>Here's IntentService Code</strong></p> <pre><code>public class Downloader extends IntentService { public Downloader() { super("Downloader"); } @Override public void onCreate() { super.onCreate(); } @Override public void onDestroy() { super.onDestroy(); } @Override public void onHandleIntent(Intent i) { try { myddownloadmethod();// calling my download method } catch (Exception e1) { // TODO Auto-generated catch block Log.d("Error",e1.getMessage()); } } } </code></pre> <p>Calling IntentService from MyActivity</p> <pre><code>public class ServicesDemo extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent i1=new Intent(this, Downloader.class); startService(i1); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload