0

I would like to make a splash for my android app. The function is that when the app is open, first shows the splash page. Until get the first data from GPS, then it turns to the main activity. There is some problems for the wait() in my code reported by logcat.

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 information is as follows: enter image description here

HeikiCyan
  • 157
  • 1
  • 2
  • 11

2 Answers2

2

use Thread.sleep instead of wait() like

 try {
            Thread.sleep(1500);
     }
    catch (InterruptedException e)
     {
              e.printStackTrace();
     }
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
0

In order to call wait() on an object, you have to hold the synchronized lock on that object (though the lock is actually released while the thread is waiting). There is also thread.sleep if that works better but I'm not really sure if it will.

AndroidPenguin
  • 3,445
  • 2
  • 21
  • 42