Tôi có một cái nhìn hình ảnh, tôi đã viết swiping, tại thời điểm swiping, những hình ảnh được tải xuống từ Internet, vì vậy tôi nghĩ rằng tôi phải tải về các hình ảnh trong nền trước khi swiping, cho rằng tôi cần phải sử dụng asynctask hoặc Dịch vụ hoặc IntentService, tất cả những điều này sẽ giúp tải xuống và lưu trữ dữ liệu/dữ liệu/mypackages, nhưng vẫn vuốt chậm trong trường hợp của tôi, cũng truyền đạt cho tôi cái nào tốt nhất, là tôi đang gọi đúng cáchCần trợ giúp khi tải xuống trong hình nền trong Android?
1. AsyncTask
2. dịch vụ
3. Dịch vụ Ý định như hình dưới đây,
im nhầm lẫn đó là một phương pháp đúng bởi vì vẫn còn vấn đề của tôi không được giải quyết
Dưới đây là AsyncTask đoạn mã mẫu
public class Demo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new FirstTask().execute(); // calling Asynctask here
}
}
Mã tác vụ không đồng bộ
private class FirstTask extends AsyncTask<Void, Void, Void> {
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();
}
}
}
Đây là dịch vụ My sinppet
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));
}
}
Dưới đây là IntentService Mã
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());
}
}
}
Calling IntentService từ MyActivity
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);
}
}
Looper.prepare(); trong doInBackground tại sao? trông giống như lỗi thiết kế – Selvin
@selvin: tôi có ngoại lệ này, Không thể tạo trình xử lý bên trong chuỗi chưa được gọi là Looper.prepare(), đó là lý do tại sao tôi đã sử dụng –
heh ** không sử dụng chuỗi giao diện người dùng trong doInBackground ** .. Sử dụng publishProgress và ghi đè onProgressUpdate và trong phương pháp này làm somtheting trên UI thread ... thats cách AsyncTask hoạt động ... – Selvin