My question is suppose i have to views(Screen). when i open my application from the emulator first screen should stay for just 5 secs and automatically new screen should appear(2nd).
i Know i can do this through timer concept but dnt know hwto do that in android?
So pls help me in this problem.
I completely agree with EboMike about not showing a splash screen. It's a bad practice with any app (desktop or android) and often just covers up sloppy programming. Get the user to do useful work as soon as possible and do any other startup work asynchronously.
However, sometimes lawyers are in charge of software, sometimes it's a marketing person, and sometime you just can't defer long startup code. In this case, you need an answer. The simplest is to have your main activity be your splash scree. In the splash screen activity, create a handler to start your real main activity doing something like this:
Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
startActivity(...);
finish();
}
};
handler.sendEmptyMessageDelayed(0, 5000);
Related
Everyone knows that when you are manipulating with Database you should do that in another Thread. But I don't understand is that really necessary when you are just inserting one item for example, or when it is happening when user opens Activity or Fragment for example and data is just loading from Database you user would wait for loading ending whatever.
Maybe it even stops app a bit while creating new Thread ect.
So what do you think is that "must be" to create new Threads?
A thread should be used in a long running process that would block the UI from updating. If it's more than a second or two you might want to put it into a background thread and notify the user with a dialog or spinner or something. If you lock the UI thread for more than 5 seconds the user will be prompted with a kill or wait option by the OS.
To have a good user experience heavy works should run in another thread, in this way there aren't any lags or blocks and the user experience is better.
The time taken to create a new thread is a lot less than the time taken to performe a query or an http request or other heavy works. Maybe on your phone this time is short but on low level phone it can take more time. After 5s Android shows to the user an allert to ask if user wants kill the app or wait, this isn't good.
Another point, it's true that the user must wait data to use it BUT if you performe a request in main thread the view will blocked, if you do it in another thread thed view is smooth, you can show easy a progress bar and if user want return back the app still responsive.
I can understand that send messages beetwen threads isn't easy like do it in main thread, but if you use a bus, like OTTO Bus (http://square.github.io/otto/) and extend the bus class in this way
public class AndroidBus extends Bus{
private final Handler mainThread = new Handler(Looper.getMainLooper());
#Override
public void post(final Object event) {
if (Looper.myLooper() == Looper.getMainLooper()) {
super.post(event);
} else {
mainThread.post(new Runnable() {
#Override
public void run() {
post(event);
}
});
}
}
}
In this way u can easly send messages beetwen threads
Given a one-activity app like this:
public class MyActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
loadingScreen = new RelativeLayout(this);
// add a bitmap to loadingScreen
setContentView(loadingScreen);
Dict dict = new Dict();
dict.init();
// this method takes ~10 seconds
}
}
In theory, it looks like this would display a loading screen while the dictionary initializes. In practice, nothing is displayed until onCreate() returns, so the user sees a blank white screen for 10 seconds. What's the best way to display a please wait while this app loads screen?
I believe I could create a separate thread for dict.init(), but that seems like overkill in this case because I don't want the app to be usable or interactive while dict.init() runs. I'd like it to run on the main thread (and hang the rest of the app while it executes), I just want to display something on the screen first.
PS, I tried moving dict.init() to onStart(), that appeared to have no effect.
edit: Perhaps I should have clarified this to avoid getting "You're doing it wrong" type answers, but the init takes 2 or 3 seconds on modern phones and tables, 10 seconds is a worst-case on old phones. This app is a word game, it can't be used without the dictionary. Moving dict.init() to an async task will not improve the user's experience, and the question I asked is whether it's possible to display a splash screen without doing that. I gather that the answer is "No."
I'd like it to run on the main thread (and hang the rest of the app while it executes)
No, no wouldn't and your users wouldn't like you much either
I believe I could create a separate thread for dict.init(), but that seems like overkill in this case because I don't want the app to be usable or interactive while dict.init() runs.
This is exactly what you should do. You can use a variety of ways including an AsyncTask
and show a ProgressDialog while the work is being done.
so the user sees a blank white screen for 10 seconds.
If it is taking this long then you might want to rethink your flow. Even if you go with a separate Thread and show a ProgressBar, most users aren't going to want to stare at that for 10 seconds. You should load the data in the background and allow them to do something else while it loads, if possible. You could use something like an IntentService depending on how you are getting the data.
Example of AsyncTask
Painless Threading
I am experimenting with an app I am developping.
When I launch the app, there is currently a 3 second delay before the app UI is usable. During the delay the screen is black, apart from the task bar and, below it, the app title bar.
I was thinking about displaying a splashscreen as a dialog in the main Activity. However, it is only displayed after those 3 seconds, which makes it useless. This means that nearly all of the 3-second delay takes place between the launch and the call to
super.onCreate(savedInstanceState).
Can anyone educate me on what is happening behing the scenes during this delay ? Is there anything I can do to shorten it ?
Try to locate the slow code and put it into a second thread.
new Thread(new Runnable() {
#Override
public void run() {
// slow code goes here.
}
}).start();
This is the first time I ask something, so if there is something wrong just tell me and I´ll try to fix it ASAP.
We have a customer that wants us to login in their servers with a native Android app, but without giving us a custom way to do this. They want us to use the current website they have to log and, after authentication takes place, retrieve within the browser a XML which contains the data we need. After that, use the data in the native app. All of this with the user not knowing/seeing that a browser is being used. A total mess IMHO.
Of course, I have never tried this approach in the past and my first tests make me feel like this is impossible (or extremely difficult) to achieve. Whenever I try to load the URL in a hidden WebView the default browser pops up showing the website.
My main question is, is it possible to load a webview and work with it (invoke javascript, etc...) in the background?
Thank you.
You could set the WebView to hidden by default with the attribute android:visibility="gone", interact with it at runtime then when you need to show it to the user after you've loaded data, just call setVisibility(View.VISIBLE)
Hope this helps!
Ofc, you must to use a Thread :
protected void getPage(){
Thread th = new Thread(){
public void run(){
//Download and make things
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
//print int the activity
}
});
}
};
th.start();
Remember, and thats is VERY important, you CANT draw from thread to the main activity. The only who can draw in the screen is the main activity. You can draw with 2 methods:
One , with the method _mActivity.runOnUiThread(new Runnable() {_ like the example i put.
Two, use a Handler to send messages from thread to main activity with the information that you want to draw.
*Main activity is the activity that its in the screen in that moment, not the first activity of the app
I want to force android to wait AND continue processing something at the same time. I have seen the Thread wait function, but that just makes things hang for a while not actually letting the app do anything. Subsequent processes are simply queued up waiting their turn.
I want to force the timing of a process. This is kind of a combination between having a thread with a wait AND an asynctask
insight appreciated
public class yourActivity extends Activity{
final WebView yourWebview; //this is the webview
Context mContext = this;
public void onCreate(Bundle B){
setContentView(R.id.somethingtoshow);//this will be shown while webview working
Runnable yourRun = new Runnable(){
public void run(){
yourWebview = new WebView(mContext);
//do whatever you want with it
//loadUrl and whatever you want
//when your done
runOnUiThread(new Runnable(){
public void run(){
setContentView(yourWebView);
}
});
}
};
Thread T= new Thread(yourRun);
T.start();
}
}
'Waiting' means to put the thread in a suspended state - do you mean having the app simply do nothing until the process is completed?
You never want to make the main event thread hang or wait, that will make the user think the app is frozen. To do what you are wanting, you will probably spawn an async thread that loads the page from the main activity. The activity will continue to display whatever you had it doing last, and will not hang up or freeze while the async is going in the background. However, the user will still be able to press buttons, and might mess you up.
So to get the app to appear unfrozen and allow a process to occur in the background, you will want to enter into some loading screen or limit the user's options on the main layout. This will allow activity to continue occurring but allow the user a smooth experience.