Word Slider in Splash Screen Android - java

I am trying to do an app in which there is a splash screen in the beginning. During loading, i need to show "Loading images...Loading modules....Now you are ready to go",etc. This is similar to the way in which facebook app loads showing the splash screen. I don't know how to do this. This is my current code :
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread logoTimer = new Thread(){
public void run(){
try{
int logoTimer = 0;
while (logoTimer<5000){
sleep(100);
logoTimer=logoTimer+100;
}
startActivity(new Intent("com.package.MAIN"));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
};
logoTimer.start();
}
}
I would also like to keep a ProgressBar along with this. Can anyone help me out with a sample code or a reference. Thanks in advance.

You could use Handler to communicate from background thread to UI thread. Handler should send messages to UI when loading of one module is finished.

Related

Activity start too long

Hello I have a problem with opening Activity.
I'm calling startActivity() with Intent by clicking Button.
I need to wait 4-5 seconds before Activity shows up on the screen.
I know how to do.
itemimg = new ItemsInPacagesImageView(imglist1, this, nazovtripu, 0);
I have 17 times similar code (with other ImageViews) I have this in Method with name InitItemimg();
I tried put this method on OnStart activity with this thread
#Override
public void onStart() {
super.onStart();
timer = new Thread() { // new thread
public void run() {
Boolean b = true;
try {
sleep(20);
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
InitItemimg();;
}
});
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
}
}
};
timer.start();
}
But is no resolve my problem, please do you have some ideas? Thanks
excuse me, I figured so in this method (ItemsInPacagesImageView(imglist1, this, nazovtripu, 0);) on start id deserialization if is some deserialization in row is "fast" but if it's more in row (now 17) with deserialization program spend more time some seconds.
I resolve this problem with put explicit, class which i deserialization in method.
Now i deserialization once instead 17 times. and I safe more miliscond-seconds.

multithreading to add image into surface view

Right now I have one thread which populates the view of my activity. But I want another thread to add some textviews and imageviews in the same activity. I am using SurfaceView inside which I created this thread and I don't know how to add another thread so that it can contribute to the view of the current activity.
Help me out..
MyView view;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
view = new MyView(this);
setContentView(view);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
view.pause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
view.resume();
}
public class MyView extends SurfaceView implements Runnable {
Thread threadstill = null;
boolean isitok = false;
SurfaceHolder holder;
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
}
#Override
public void run() {
// TODO Auto-generated method stub
while (isitok == true) {
if (!holder.getSurface().isValid()) {
continue;
}
canvas = holder.lockCanvas();
canvas.drawARGB(255, 255, 255, 255);
canvas.drawBitmap(<bitmapimage>, x, y, null);
holder.unlockCanvasAndPost(canvas);
}
}
public void pause() {
isitok = false;
while (true) {
try {
threadstill.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
threadstill = null;
}
public void resume() {
isitok = true;
threadstill = new Thread(this);
threadstill.start();
}
}
You cannot directly manipulate the user interface from another thread.
The main thread is responsible for all UI changes.
However, if you want to perform some expensive work in the background (e.g. loading pictures) you can send the results to the UI thread.
Hava a look at the official documentation: https://developer.android.com/training/multiple-threads/communicate-ui.html
Or at a similiar question: How can I manipulate main thread UI elements from another thread in Android?
I don't exactly know what is your issue but regarding to I don't know how to add another thread so that it can contribute to the view of the current activity. you can create a new thread easily with a nested class...simply call:
Thread myNewThread = new Thread(new Runnable() {
public void run() {
//do code here
}
}).start();
But be Aware you can only change views from the UIThread so i would recommend you the AsyncTask:
public void myAsyncTask exstends Asynctask<Void,Void,Void> {
....
}
AsyncTask Comes with several methods the most important is the doInBackground where you can do the heavy things like Network Connections or other stuff which would freeze the UI or is simply not allowed on the UIThread(Network stuff).
After doInBackground is finished onPostExecute is called which runs on the UiThread and where you can process changes on your views.
To get more Information look at this link:
http://developer.android.com/reference/android/os/AsyncTask.html
Hope it helps and it is what you asked for. ;)
Do it with AsyncTask class. In doInBackground method download image, and in onPostExecute method apply image to ImageView.
I did it here already https://github.com/bajicdusko/AndroidJsonProvider/tree/master/app/src/main/java/com/bajicdusko/ajp/tools.
You can use my class or make your own .

Advice on how to fix frame skipping in android

So I've just noticed that my app is skipping quite a few frames when running in the emulator. This is my first app and I did some reading on the topic and found that I might not be starting the activities correctly. However, my activities are loaded through the settings menu and I don't know where this is in my code. If this is a big issue it would be appreciated if someone could point me in the right direction in relation to my specific code?
https://github.com/addrum/Calculate
I can post code here in preference if needed.
Edit: It appears to skip frames on the splash activity:
package com.main.androidcalculator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class SplashActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread() {
public void run() {
try{
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent openMain = new Intent("com.main.androidcalculator.MAINACTIVITY");
startActivity(openMain);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
I think the emulator is just too slow.
Your code works fine on a real device. I've tested on GS3.
Maybe ProgressBar is just too heavy for the emulator.
The view has animation and a lot of stuffs.
(Remove the ProgressBar and the issue's gone!)
See also:
Choreographer(639): Skipped 50 frames

background music automatically stops

I have made an application with Splash Music. But whenever I go in preferences of app, the music automatically stops, and then never plays untill I restart the application. Same is the case when I open an activity, which tells whether phone is in "Normal" or in "Silent" Mode.
What is the reason for this weird behavior?
Here is the Splash music code where I check whether to play music or not..
public class SplashScreen extends Activity{
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
mp= MediaPlayer.create(SplashScreen.this, R.raw.got);
SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
;
if(pref.getBoolean("music", true))
{
mp.start();
}
if(pref.getBoolean("loop", true))
{
mp.setLooping(true);
}
Thread timer= new Thread()
{
public void run()
{
try
{
sleep(5000);
Class ourclass = Class.forName("com.umer.practice2.Menu");
Intent myintent= new Intent(SplashScreen.this,ourclass);
startActivity(myintent);
}
catch(Exception e)
{
e.printStackTrace();
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
Thanks
It is because you are creating and playing your mediaplayer object on the main thread. When your activity is getting paused, you are finishing the activity which is stopping your mediaplayer. If you want to keep you music independent of the activity, offshore it to a service.
Running in a Service should help you.

Android app reopening and proceeding on the next activity even when pressing the home button or back button

I wrote this app that in the first screen it has an included Thread on it. So it has I timed it like 7 seconds then it will proceed to the next activity.
The problem is whenever I hit the home button the music will stop and it will go to android homescreen but after my timed is done which is the 7 seconds, the app will reappear and will show the next activity.
I tried putting finish(); in the onpause(); but it's still showing the next activity.
here's the actual code.
public class HelloWorldActivity extends Activity {
MediaPlayer mp;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mp = MediaPlayer.create(this, R.raw.otj);
mp.start();
Thread LogoTimer = new Thread(){
public void run(){
try{
int LogoTimer = 0;
while(LogoTimer < 7000){
sleep(100);
LogoTimer = LogoTimer + 100;
}
startActivity(new Intent("com.example.HelloWorld.CLEARSCREEN"));
} catch (InterruptedException e) {
e.printStackTrace();
}
finally{
finish();
}
}
};
LogoTimer.start();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mp.release();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mp.pause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
}
First, that's a really inefficient way to run a timer. Try this way instead:
new Handler().postDelayed(new Runnable() {
public void run() {
// Do some work.
}
}, delayTimeInMs);
Second, your starting a new activity when that timer eventually fires. It doesn't matter that the originating activity is finished. Your startActivity() is running on it's own thread and will execute regardless.
It's possible the postDelayed() method will function like you expect. If not you'll need to have it check when it runs whether it should really start the activity. However, I think the Handler is attached to the default Looper which means it will stop (or rather, the message won't be posted) if the main activity finishes.
The application is still in the background and the thread is not destroyed so it will fire the startActivity.
I would not really setup a splash screen this way, or use a thread unless I wanted it off the UI for some reason, even then there are better options.
For educational purposes to take care of this you need to be able to abort the thread safely in onPause() one way to do so is below
Modifed Thread
Thread LogoTimer = new Thread() {
private volatile boolean abortThread = false;
public void run(){
long stopAt = System.currentTimeMillis() + 7000;
while (!abortThread && stopAt > System.currentTimeMillis())
yield();
if (!abortThread)
startActivity ...
}
public synchronized void stopThread() {
abortThread = true;
}
};

Categories

Resources