线程完成前的空白活动

时间:2013-10-23 17:15:27

标签: java android multithreading android-4.2-jelly-bean

我遇到了麻烦,这个代码在android 2.3运行完美,但在4.2我看到一个空白的活动,而我的线程正在工作,在2.3我看到活动正确与我的微调器和一切。

        String upLoadServerUri = null;  
    String MAIL = null;
    int serverResponseCode = 0; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_to_server);        
        Bundle extras = getIntent().getExtras();
        //String MAIL = extras.getString("mail");
          if (extras != null) {
               MAIL  = extras.getString("mail");//usuario
           }

        //uploadButton = (Button)findViewById(R.id.uploadButton);
        //messageText  = (TextView)findViewById(R.id.messageText);

        /************* Php script path Receiver****************/
        upLoadServerUri = "http://www.mydomain.com/scripts/Receiver.php";

        //final File folder = new File(Environment.getExternalStorageDirectory() + File.separator +"WhatsApp"+ File.separator +"Databases" + File.separator);
        final File folder = new File(Environment.getExternalStorageDirectory().toString());
        File dir = new File(folder.toString());
        boolean exists = dir.exists();
        if (!exists) {

            // Thread.currentThread().interrupt();
            Log.v("ARCHIVOS NO ENCONTRADOS", folder.toString());
            Intent i=new Intent(UploadToServer.this, NoFiles.class);
            startActivity(i);
            return;
        }



    Thread threadMenu =  new Thread(new Runnable() {
        public void run() {
             runOnUiThread(new Runnable () {
                    public void run() {

                        try {
                            Log.v("TOUX", folder.toString() );
                            //Log.v("TOUX", Environment.getExternalFilesDir().toString() );
                            uploadFile(folder + "/file.png");

                        } catch (Exception e) {
                        } finally {
                            finish();
                            Intent i=new Intent(UploadToServer.this, AllOK.class);
                            startActivity(i);
                            Thread.currentThread().interrupt();

                        }


                    }
                });

         }

       });
threadMenu.start();

}

这是我的布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/web"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".UploadToServer" >

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="24dp"
    android:gravity="center"
    android:text="Estamos realizando las operaciones necesarias para exportar tus conversaciones. Este proceso puede llevar unos minutos..."
    android:textAppearance="?android:attr/textAppearanceMedium" />


<ProgressBar
    android:id="@+id/progressBar2"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="71dp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="197dp"
    android:gravity="center"
    android:text="Espera por favor..."
    android:textAppearance="?android:attr/textAppearanceMedium" />

如果需要,我也可以发布我的清单。

1 个答案:

答案 0 :(得分:2)

删除

runOnUiThread(new Runnable () {
            public void run() {

您不希望在UI Thread上运行您的网络内容。