首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正在加载进度条android

正在加载进度条android
EN

Stack Overflow用户
提问于 2012-08-21 23:53:34
回答 3查看 1.4K关注 0票数 0

*我有加载一些文件和更新UI...It的类需要一些时间来查看结果,...所以我想添加一个加载栏或进度条。某些数据已由其他活动传递(捆绑包extras1 = getIntent().getExtras();)。但是我正在下载每个项目的一张图片...我认为这需要更多的时间。有谁能帮我吗?

这是我的代码:

代码语言:javascript
复制
public class ShowSelectedEvents extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewdetailsevents);
    Bundle extras1 = getIntent().getExtras();
    String eventTitle = extras1.getString("eventTitle");
    final String address = extras1.getString("address");
    String date = extras1.getString("date");
    String time = extras1.getString("time");
    final String fix = extras1.getString("fix");
    final String mobile = extras1.getString("mobile");
    final String web = extras1.getString("web");
    final String mail = extras1.getString("mail");
    String imageLink = extras1.getString("imageLink");
    final String videoLink = extras1.getString("videoLink");
    // Add item image
    Bitmap bitMap = null;
    try {
        bitMap = DownloadImage("imageLink);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ImageView imageItem = (ImageView) findViewById(R.id.imageItem);
    imageItem.setImageBitmap(bitMap);
    TextView viewTitle = (TextView) findViewById(R.id.viewTitle);
    viewTitle.setText(eventTitle);
    TextView viewDateTime = (TextView) findViewById(R.id.dateTime);
    viewDateTime.setText("Event is on "+date +" @ "+ time);
    // View Address
    TextView viewAdd = (TextView) findViewById(R.id.viewAddress);   
    viewAdd.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC),Typeface.ITALIC);
    viewAdd.setText(address);
    // On click open Navigation
    if (!(address.equals("-"))){
        ImageView navigationIcon = (ImageView) findViewById(R.id.imageMapNavigation);
        navigationIcon.setOnClickListener(new OnClickListener() {           
            @Override
            public void onClick(View v) {
                String url = "google.navigation:q="+address;
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));            
                startActivity(i);
            }
        }); 
    }else{
        Context context = getApplicationContext();
        CharSequence text = "Address is incomplete!";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }
    // View Phone number
    TextView viewPhoneNumber = (TextView) findViewById(R.id.viewPhoneNumber);
    viewPhoneNumber.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC),Typeface.ITALIC);
    viewPhoneNumber.setText(fix);
    // On click open call 
    if (!(fix.equals("-"))){
        viewPhoneNumber.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "tel:"+fix;
                Intent i = new Intent(Intent.ACTION_CALL, Uri.parse(url));  
                startActivity(i);
            }
        });
    }
    //View Mobile number
    TextView viewMobileNumber = (TextView) findViewById(R.id.viewMobileNumber);
    viewMobileNumber.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC),Typeface.ITALIC);
    viewMobileNumber.setText(mobile);
    // on click call mobile number
    if (!(mobile.equals("-"))){
        viewMobileNumber.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "tel:"+mobile;
                Intent i = new Intent(Intent.ACTION_CALL, Uri.parse(url));  
                startActivity(i);
            }
        });
    }
    //View web url
    TextView viewWeb = (TextView) findViewById(R.id.viewWeb);
    viewWeb.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC),Typeface.ITALIC);
    viewWeb.setText(web);
    //on click open web browser
    if (!(web.equals("-"))){
        viewWeb.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(web));
                startActivity(i);
            }
        });
    }
    TextView viewMail = (TextView) findViewById(R.id.viewMailAddress);
    viewMail.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC),Typeface.ITALIC);
    viewMail.setText(mail);
    if(!(mail.equals("-"))){
        viewMail.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {mail});
                emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Contact from tamilpage.ch");
                emailIntent.setType("text/plain");
                startActivity(Intent.createChooser(emailIntent, "Send a mail ..."));
            }
        });
    }
    // On click play the video
    if (!(address.equals("-"))){
        ImageView videoIcon = (ImageView) findViewById(R.id.videoButton);
        videoIcon.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View v) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse(videoLink));
                startActivity(browserIntent);
            }
        }); 
    }else{
        Context context = getApplicationContext();
        CharSequence text = "No advert video for this event";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }
}
private Bitmap DownloadImage(String url) throws Exception {
    Bitmap bitmap = null;
    InputStream in = null;
    try {
        in = OpenHttpConnection(url);
        bitmap = BitmapFactory.decodeStream(in);
        in.close();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return bitmap;
}
private InputStream OpenHttpConnection(String url) throws Exception {
    InputStream in = null;
    int response = -1;
    System.out.println("Nishi1"); 
    URL url1 = new URL(url); 
    URLConnection conn = url1.openConnection();
    if (!(conn instanceof HttpURLConnection)) 
        throw new IOException("Not an HTTP connection");
    try{
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect(); 
        response = httpConn.getResponseCode(); 
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream(); 
            System.out.println(in);
        } 
    }
    catch (Exception ex)
    {
        throw new IOException("Error connecting"); 
    }
    return in; 
}

}

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-08-22 00:08:38

,所以我想添加一个加载栏或进度条

对于您的方法,您需要使用Threads。我特别推荐你去看看

  • Handler
  • AsyncTask

这两种方法都提供了使用线程的功能。AsyncTask比处理程序更复杂,而且它是泛型类型,因此提供了更多的类型安全和更快的工作。

您应该阅读一些教程,以便

在Vogella有非常棒和有用的教程

  • Android Threads, Handlers and AsyncTask - Tutorial
票数 1
EN

Stack Overflow用户

发布于 2012-08-21 23:56:22

我会考虑使用AsyncTask。这里有一个可以帮助你入门的链接:http://developer.android.com/reference/android/os/AsyncTask.html

票数 0
EN

Stack Overflow用户

发布于 2012-08-21 23:59:03

您需要在具有更新进度条的回调的ASyncTask中执行此操作。我已经在下面包含了一个示例,它实际上并没有做任何事情。您可以将用于调用下载的代码放在doInBackground()中,并从onProgressUpdate更新进度。

代码语言:javascript
复制
public class BackgroundAsyncTask extends
    AsyncTask<Void, Integer, Void> {

  int myProgress;

  @Override
  protected void onPostExecute(Void result) {
   // TODO Auto-generated method stub
   Toast.makeText(AndroidAsyncTaskProgressBar.this,
         "onPostExecute", Toast.LENGTH_LONG).show();
  }

  @Override
  protected void onPreExecute() {
   // TODO Auto-generated method stub
   Toast.makeText(AndroidAsyncTaskProgressBar.this,
         "onPreExecute", Toast.LENGTH_LONG).show();
   myProgress = 0;
  }

  @Override
  protected Void doInBackground(Void... params) {
   // TODO Auto-generated method stub
   while(myProgress<100){
    myProgress++;
    publishProgress(myProgress);
       SystemClock.sleep(100);
   }
   return null;
  }

  @Override
  protected void onProgressUpdate(Integer... values) {
   // TODO Auto-generated method stub
   progressBar.setProgress(values[0]);
  }

 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12058687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档