2012-12-09 72 views
0

我想为我的Android应用程序做一个飞溅。该功能是当应用程序打开时,首先显示启动页面。直到从GPS获得第一个数据,然后才转向主要活动。 logcat报告的代码中的wait()存在一些问题。如何在android编程中使用wait()方法进行初始化?

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageInfo; 
import android.content.pm.PackageManager; 
import android.content.pm.PackageManager.NameNotFoundException; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.Handler; 
import android.widget.TextView; 

public class splash extends Activity 
{ int waittime; 
    public void onCreate(Bundle icicle) 
    { 
     super.onCreate(icicle); 
     setContentView(R.layout.splash); 
     final TextView text=(TextView)findViewById(R.id.textView1); 
     text.setText("Waiting for the GPS data update!"); 
     LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     String provider = locationManager.GPS_PROVIDER; 
     int c=0; 
     for(int i=0; i<20;i++) 
     { 
      Location location = locationManager.getLastKnownLocation(provider); 
       //get the location data from every loop 
      if(location!=null)//if get the data, then turn to the main activity 
      { 
       new Handler().postDelayed(new Runnable() { 
        public void run() { 

         Intent mainIntent = new Intent(splash.this, MainActivity.class); 
         splash.this.startActivity(mainIntent); 
        splash.this.finish(); 
        } 
       }, 1000); 
      } 
       else 
       { 
        try { 
         wait(3000);//if not get the data, wait 3 seconds until the next loop 
        } catch (InterruptedException e) { 

         e.printStackTrace(); 
        } 
       } 
      } 

     } 
    } 

logcat的信息如下: enter image description here

+0

在这里添加您logcat的消息。帮助很多:) – AndroidPenguin

+0

我更新了它并添加了logcat信息。 – HeikiCyan

回答

0

为了在对象上调用wait(),您必须持有该对象的同步锁(虽然锁,而线程实际发布在等待)。 还有thread.sleep如果更好,但我不确定是否会。

0

使用的Thread.sleep而不是等待()之类

try { 
      Thread.sleep(1500); 
    } 
    catch (InterruptedException e) 
    { 
       e.printStackTrace(); 
    }