Android中的背景图片下载需要帮助吗?
我有一个图像视图,我写过刷卡,在刷卡的时候,图像从互联网上下载,所以我想我必须在刷卡之前在后台下载图像,因为我需要使用asynctask或Service或IntentService,所有这些都有助于下载和存储在数据/数据/ mypackages中,但仍然在我的情况下刷卡任何想法,也告诉我哪一个是最好的,是我正在以正确的方式调用
asynctask
2.服务
3.意向服务如下图所示,
我很困惑哪一个是正确的方法,因为我的问题仍未解决
这是asynctask代码示例代码段
public class Demo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new FirstTask().execute(); // calling Asynctask here
}
}
异步任务代码
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();
}
}
}
这是我的服务
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));
}
}
这是IntentService代码
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());
}
}
}
从MyActivity调用IntentService
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);
}
}
没有找到相关结果
已邀请:
5 个回复
谷靛
辽躺
芜任亮蜡
这就是你所需要的。然后,当您需要加载图像时,您可以调用:
而已。 如果您有来自JSON对象的URL,请将URL解析为您的字符串,以便:
然后拨打
抵浮细
豆兢