我使用AsyncTask在多个地方的闪屏和数据库加载.这是个好主意吗?



我有一个启动屏幕和主要活动。这个想法是在执行db加载时显示splash。我不能使用仿真模式,因为它与直接在手机上安装的apk不一样。当APK安装启动屏幕显示5秒,然后db列表视图屏幕显示。这是正确的。但是当我按下手机上的应用程序启动图标时,闪屏没有出现,闪屏时间显示白色屏幕,然后出现db列表视图。以下是相关文件,如果需要可以提供更多:

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.loadrunner"
 android:versionCode="1"
 android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.loadrunner.SplashScreenActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.loadrunner.MainActivity"
        android:exported="true"
        android:label="@string/app_name" >
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

package com.loadrunner;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
public class SplashScreenActivity extends Activity {
private static final int SPLASH_SHOW_TIME = 5000;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new BackgroundSplashTask().execute();
}
/*@Override
protected void onStart(){
    setContentView(R.layout.activity_main);
    new BackgroundSplashTask().execute();
}*/
/**
 * Async Task: can be used to load DB, images during which the splash screen
 * is shown to user
 */
private class BackgroundSplashTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected Void doInBackground(Void... arg0) {
        // I have just give a sleep for this thread
        // if you want to load database, make
        // network calls, load images
        // you can do here and remove the following
        // sleep
        // do not worry about this Thread.sleep
        // this is an async task, it will not disrupt the UI
        try {
            Thread.sleep(SPLASH_SHOW_TIME);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        Intent i = new Intent(SplashScreenActivity.this,
                MainActivity.class);
        // any info loaded can during splash_show
        // can be passed to main activity using
        // below
        i.putExtra("loaded_info", " ");
        startActivity(i);
        finish();
    }
}
}

package com.loadrunner;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.Table.TableMainLayout;
public class MainActivity extends Activity {
final String TAG = "MainActivity.java";
public static DatabaseHandler db;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//*************************************************************************************************
    // remove title bar.
    //requestWindowFeature(Window.FEATURE_NO_TITLE); //splash
    //setContentView(R.layout.activity_main); //splash
    /* Loads next module */
    // get the splash image
    //ImageView splashImage = (ImageView) MainActivity.this.findViewById(R.id.imageViewSplashLogo); //splash
    //ImageView splashImage = (ImageView) findViewById(R.id.imageViewSplashLogo); //splash
    //Log.d("Loadrunner", "splashImage: " + splashImage);
    //splashImage.setImageResource(R.drawable.shoppingcart);
    // make the splash image invisible
    //splashImage.setVisibility(View.GONE); //splash
    // splashImage.setVisibility(View.VISIBLE); //splash
    // specify animation
    //Animation animFadeOut = AnimationUtils.loadAnimation(MainActivity.this, R.anim.splash_screen_fadeout); //splash
    // apply the animattion
    //splashImage.startAnimation(animFadeOut); //splash
//*************************************************************************************************
    db = new DatabaseHandler(this);
    db.getWritableDatabase();
    this.db.insertFast(100);
    int dbreccnt = db.countRecords();
    Log.d("AppLoadrunner ", "Loadrunner record count " + dbreccnt);
    setContentView(new TableMainLayout(this));
    Log.d("AppLoadrunner ", "Loadrunner MainActivity Content set");
}

}

package com.loadrunner;
import android.os.AsyncTask;
import android.util.Log;

public class AsyncInsertData extends AsyncTask<String, String, String> {
DatabaseHandler databaseHandler;
String type;
long timeElapsed;
protected AsyncInsertData(String type){
    this.type  = type;
    //this.databaseHandler = new DatabaseHandler(this);
    //(MainActivity.this);
}

//@type - can be 'normal' or 'fast'
@Override
protected void onPreExecute() {
    super.onPreExecute();
    //tvStatus.setText("Inserting " + editTextRecordNum.getText() + " records...");
}

@Override
protected String doInBackground(String... aurl) {
    Log.d("AppSynch", "AsynchInsertData.java");
    try {
        // empty the table
        databaseHandler.deleteRecords();
        // keep track of execution time
        long lStartTime = System.nanoTime();
        type = "fast";
        int insertCount = 20; // This never seems to be called

        if (type.equals("normal")) {
            databaseHandler.insertNormal(insertCount);
        } else {
            databaseHandler.insertFast(insertCount);
        }
        // execution finished
        long lEndTime = System.nanoTime();
        // display execution time
        timeElapsed = lEndTime - lStartTime;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
protected void onPostExecute(String unused) {
    //Toast.makeText(getApplicationContext(),"This is an Android Toast Message", Toast.LENGTH_LONG).show();
    //tvStatus.setText("Done " + choice + " inserting " + databaseHandler.countRecords() + " records into table: [" + this.databaseHandler.tableName + "]. Time elapsed: " + timeElapsed / 1000000 + " ms.");
}

}

package com.loadrunner;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.Table.TableMainLayout;
public class MainActivity extends Activity {
final String TAG = "MainActivity.java";
public static DatabaseHandler db;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//*************************************************************************************************
    // remove title bar.
    //requestWindowFeature(Window.FEATURE_NO_TITLE); //splash
    //setContentView(R.layout.activity_main); //splash
    /* Loads next module */
    // get the splash image
    //ImageView splashImage = (ImageView) MainActivity.this.findViewById(R.id.imageViewSplashLogo); //splash
    //ImageView splashImage = (ImageView) findViewById(R.id.imageViewSplashLogo); //splash
    //Log.d("Loadrunner", "splashImage: " + splashImage);
    //splashImage.setImageResource(R.drawable.shoppingcart);
    // make the splash image invisible
    //splashImage.setVisibility(View.GONE); //splash
    // splashImage.setVisibility(View.VISIBLE); //splash
    // specify animation
    //Animation animFadeOut = AnimationUtils.loadAnimation(MainActivity.this, R.anim.splash_screen_fadeout); //splash
    // apply the animattion
    //splashImage.startAnimation(animFadeOut); //splash
//*************************************************************************************************
    db = new DatabaseHandler(this);
    db.getWritableDatabase();
    this.db.insertFast(100);
    int dbreccnt = db.countRecords();
    Log.d("AppLoadrunner ", "Loadrunner record count " + dbreccnt);
    setContentView(new TableMainLayout(this));
    Log.d("AppLoadrunner ", "Loadrunner MainActivity Content set");
}

}

我认为这是因为一旦你安装应用程序并重新打开它,应用程序已经在运行。如果你在重新启动它之前没有杀死它,它将恢复它原来的活动,而不是启动你的SplashScreen。所以简而言之,为了验证它是否有效,请尝试在安装后杀死应用程序,或者设置它,以便在恢复时显示加载屏幕(可能需要进行重大重构)。从本质上讲,您需要从仅在onCreate()中开始重构加载。

主题启动屏幕可能有助于解决此问题,延迟可能来自初始化布局。下面的文章展示了如何使用样式设置闪屏而不调用setContentView()

https://www.bignerdranch.com/blog/splash-screens-the-right-way/

应用程序终止进程不会终止这个。我在用HTC EVO。底部有4个按钮。HOME就像一个应用程序切换器。它不会杀人。第三个按钮是[返回]。这个作品。它遍历未执行finish();的活动。我本来是用Home键退出应用程序的。这只会让它留在堆栈上,然后返回到Home屏幕。如果我不使用后退键,应用程序就不会被杀死,我也不会看到启动画面。由于之前使用过home键,当我运行这款应用时,它会在闪屏暂停的同时显示一个白屏。就像图形不显示一样。这可能是另一个问题。现在我已经找到了最初的闪屏显示问题,并解决了它。我必须找到与这种行为相关的活动函数。谢谢大家帮忙解决这个问题。

相关内容

  • 没有找到相关文章

最新更新