正在加载Progress Bar android

时间:2012-08-21 15:53:34

标签: android progress-bar development-environment

*我有一个加载一些文件并更新UI的类...查看结果需要一些时间,...所以我想添加一个加载栏或进度条。一些数据已被其他活动传递(Bundle extras1 = getIntent()。getExtras();)。但我每个项目下载一张图片......我认为这需要更多时间。任何人都可以帮助我吗?

这是我的代码:

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; 
}

}

3 个答案:

答案 0 :(得分:1)

  

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

对于您的方法,您需要使用Threads。特别是我建议你看看

这两种方法都可以使用Threads。 AsyncTask比Handler更复杂,也是通用类型,因此提供更安全,更快速的工作。

你应该阅读一些教程,所以

在Vogella有非常棒的有用教程

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您希望这样做的方式是在ASyncTask中,该ASyncTask具有更新进度条的回调。我在下面提供了一个实际上没有做任何事情的样本。您可以在doInBackground()中调用代码来调用下载,并从onProgressUpdate更新进度。

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]);
  }

 }