【问题标题】:How to use wait() method in android programming for the splash?如何在 android 编程中使用 wait() 方法进行启动?
【发布时间】:2012-12-09 14:24:02
【问题描述】:

我想为我的 Android 应用程序大放异彩。功能是当应用打开时,首先显示启动页面。直到从 GPS 获得第一个数据,然后它才转向主要活动。 logcat报告的我的代码中的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信息如下:

【问题讨论】:

  • 在此处添加您的 logcat 消息。很有帮助:)
  • 我更新了它并添加了 logcat 信息。

标签: android splash-screen time-wait


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    为了在一个对象上调用wait(),你必须持有那个对象上的同步锁(虽然锁实际上是在线程等待时释放的)。 如果效果更好,还有thread.sleep,但我不确定它是否会。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-23
      • 1970-01-01
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多