问题
At the moment I work on an android-phonegap project.
Before the app loads the index.html there is some native code doing work. In detail it's in AsyncTask which is knocking around on the SD card (if available).
I managed to show a progress bar while the A.Task is working but furthermore I'd like to add a Splash screen in the background. I mostly worked with Phonegap and just start with native code. So I'm kinda confused by all these layouts, themes and what else you may define in the .xml files. I'm pretty sure it's also a good idea for bigger UI designs but for the simple Splash screen I want right now it feels like a total overkill.
This is a snippet from the source. Just straight forward. onCreate() calls the AsyncTask wich does some work and starts PhoneGap in it's PostExecute method. I'd like the screen to appear either in the onCreate method or onPreExecute. After the job is done the screen I'd dismiss the screen in onPostExecute(). I also added comments to illustrate my idea.
public class myAct extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Show Splash here or in onPreExecute()!
new myAsTa().execute();
}
}
class myAsTa extends AsyncTask<Void, Integer, Boolean> {
ProgressDialog dialog = new ProgressDialog(myAct.this);
@Override
protected void onPreExecute() {
//or show splash here!
dialog.setMessage("Cool Progressbar!");
dialog.show();
super.onPreExecute();
}
protected Boolean doInBackground(Void... values) {
//magician at work!
return true;
}
@Override
protected void onProgressUpdate(Integer... values) {
//insult user here
}
protected void onPostExecute(Boolean result) {
dialog.dismiss();
//dismiss splashscreen
//start Phonegap
}
}
Thanks for reading and your help :)
回答1:
I have done such a splash screen here. This loads the splash layout and is itself triggered directly from the system when the app is loaded (AndroidManifest.xml)
<activity android:name=".SplashActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
What is shown on screen is defined in setContentView(R.layout.splash);
- this layout is basically
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/desktop_rhq"
>
<TextView android:layout_gravity="bottom"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginBottom="10dp"
android:text="@string/loading"
/>
</LinearLayout>
Which defines a background image and some text to be shown. To be honest, I haven't thought much about orientation change while the splash is running - I guess this will restart the background task.
回答2:
splash.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/splash_image">
</ImageView>
SplashActivity.class
public class SplashActivity extends Activity {
protected boolean _active = true;
protected int _splashTime = 1000; // time to display the splash screen in ms
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
// thread for displaying the SplashScreen
Thread splashTread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
while(_active && (waited < _splashTime)) {
sleep(100);
if(_active) {
waited += 100;
}
}
} catch(InterruptedException e) {
} finally {
startActivity(new Intent(SplashActivity.this, NextActivity.class));
finish();
//stop();
}
}
};
splashTread.start();
}
}
For acting on orientation change: i Set in AndroidManifest.xml screenOrientation="portrait"
<activity
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar"
android:name=".SplashActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
来源:https://stackoverflow.com/questions/8654285/android-splashscreen-while-asynctask-is-running