有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在两个活动之间更改主任务?

我对安卓系统的水平要求太高了。。所以我需要你的建议

我有两项活动

A->;主要活动

B->;负载活性

我的问题是

当我执行应用程序时,首先启动一个数据库,执行数据库任务需要4秒钟。。因此,我认为最好只为显示加载页面而进行另一项活动

所以在main活动中,我编写了以下代码

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    startActivity(new Intent(this, LoadingActivity.class));  <-- this
    setContentView(R.layout.activity_main);

我的意图是,应该在启动此MainActivity之前启动LoadingActivity,以便LoadingActivity在屏幕上显示加载图像4秒钟,以覆盖MainActivity数据库任务

所以,我想要的顺序是

B(在屏幕上显示4秒)->;A(开始时与B几乎相同,在B活动屏幕后面工作)>;B被终止(调用finish()方法)->;然后,使用该应用程序

如果您能提供任何建议,我将不胜感激

这是我的LoadingActivity类代码

public class LoadingActivity extends FragmentActivity{

private static int SPLASH_TIME_OUT = 6000;
private String tag = "LoadingSplash";
@Override
protected void onCreate(Bundle savedInstanceState) {
   // TODO Auto-generated method stub
   super.onCreate(savedInstanceState);
   setContentView(R.layout.start_up);

   String localUrl ="file:///安卓_asset/loading_gif.gif";



   WebView wv=(WebView) findViewById(R.id.startup);

   //wv.getSettings().setJavaScriptEnabled(true);
   wv.getSettings().setPluginState(PluginState.ON);
   //wv.getSettings().setAllowFileAccess(true);
   Log.i(tag, "WebView get Setting finish....");

   wv.setPadding(0, 0, 0, 0);
   wv.setInitialScale(getScale());

   //wv.getSettings().setLoadWithOverviewMode(true);
   //wv.getSettings().setUseWideViewPort(true);
   wv.loadUrl(localUrl); 

   new Handler().postDelayed(new Runnable() {
       /*
        * Showing splash screen with a timer. This will be useful when you
        * want to show case your app logo / company
        */
       @Override
       public void run() {
           // This method will be executed once the timer is over
           // Start your app main activity      
           Log.i(tag, "Here is Runnable Method...");
           finish();
       }
   }, SPLASH_TIME_OUT);

    @SuppressLint("NewApi") 
    private int getScale(){
    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
    Point p = new Point();
    display.getSize(p);
    int width = p.x;
    Double val = new Double(width)/new Double(800);
    val = val * 100d;
    return val.intValue();
}
}

共 (2) 个答案

  1. # 1 楼答案

    可以使用Asynctask进行后台处理。处理完成后,您可以开始下一个活动。您应该在异步任务上使用进度对话框:)

    如果你不理解,那么你可以问我

    问候 阿西姆

  2. # 2 楼答案

    你不需要两个独立的活动,您可以执行一个AsyncTask线程来执行数据库工作,并在AsyncTask类的“onPreExecute”方法中开始显示您的“所需自定义视图”(可能包含progressBar),并在AsyncTask类的“doInBackground”方法和“onPost”方法中执行您的工作,只需将“所需自定义视图”的可见性设置为Gone即可。 希望这能奏效