Create a Loading Screen without a specific time - android

Here is my problem, when I open an activity, it start to process an image using a native method (jni) and i dont want a black screen, i want to show a message of wait or a loading wheel.
I cant determite how long gonna take the image processing.

Did this and worked for me:
final ProgressDialog progress;
progress = ProgressDialog.show(PlayStaff.this, "Loading",
"Processing Image", true);
new Thread(new Runnable() {
#Override
public void run() {
// do the thing that takes a long time
processImageNativeMethodJNI();
runOnUiThread(new Runnable() {
#Override
public void run() {
progress.dismiss();
}
});
}
}).start();

You can use Handler here also.
private static int SPLASH_TIME_OUT = 3000;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
}
}, SPLASH_TIME_OUT);
You can add any other animation, message and all in your Spalshscreen.java file and it's Layout file.

Related

Delay for a certain time before going into next intent in runnable

I am now trying to delay for a certain time before going to the next intent. I tried with postdelayed method but nothing works.
final Handler mHandler = new Handler();
runInBackground(
new Runnable() {
#Override
public void run() {
if(condition has met){
Runnable runnable = new Runnable() {
public void run() {
Intent i=new Intent(DetectorActivity.this,Main4Activity.class);
startActivity(i);
finish();
}
};
mHandler.postDelayed(runnable, 1000000);
}
}
});
You have 2 Runnable but you're just running internal one by using postDelayed().
You should run the first one too.
like this
runInBackground(new Runnable() {
.
.
.
.
.
}.run());

Show Toast in a Thread of a Service

Hi i know there are lot of answers to this topic. But I tried a lot and it doesn't work. I want to show a toast inside a thread of a service. How can i solve this problem. Using getApplicationContext() etc. doesn't work.
I start the Service from an Activity (no bounding).
public class CarDataService extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
...
startThreadUpdatingDatabase();
Toast.makeText(this, message, Toast.LENGTH_LONG).show(); //it works
}
private void startThreadUpdatingDatabase(){
Log.d("Database", "startThreadUpdatingDatabase(was called)");
new Thread(new Runnable() {
public void run(){
..
// here i want to use a toast!!!
}
}).start();
}
}
Thank you!
You have to start the thread:
new Thread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(),"Your message",Toast.LENGTH_LONG).show();
}
}).start();
public Contect context;
member variable
onStartCommand(){
context = getApplicationContext)
}
acquivre reference to the context before you start the thread
new Thread(new Runnable() {
#Override
public void run() {
Toast.makeText(context,"Your message",Toast.LENGTH_LONG).show();
}
}).start();
and there you go
use AsyncTask instead that helps in context management
http://www.androidsnippets.com/use-toast-wherever-you-want
Handler h = new Handler(context.getMainLooper());
h.post(new Runnable() {
#Override
public void run() {
Toast.makeText(context,message,Toast.LENGTH_LONG).show();
}
});
see if this works out
Show your Toast using UI-Thread
new Thread(new Runnable() {
#Override
public void run() {
// SHOW TOAST
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(yourContext, "Hello from UI-thread", Toast.LENGTH_SHORT).show();
}
});
//... start DB work
}
}).start();
If you have no access to an activity, so do it this way:
new Thread(new Runnable() {
#Override
public void run() {
// no activity, so use Handler & mainlooper
new Handler(Looper.getMainLooper()).post(
new Runnable() {
public void run() {
// yourContext is Activity or Application context
Toast.makeText(yourContext, "Hello from UI-thread", Toast.LENGTH_SHORT).show();
}
}
);
//... start DB work
}
}).start();
Look at this: Static Way to get Context on android?

All projects with Thread on android tell "unfortunately <app> has stopped working"

please some help all projects i write with thread are telling me same thing
just anything with thread gives me problem
please i have a competition to win please quick answers
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
//getActionBar().hide();
/*start up the splash screen and main menu in a time delayed thread*/
new Handler().postDelayed(new Thread() {
#Override
public void run() {
Intent mainMenu = new Intent(MainActivity.this,
SFMainMenu.class);
MainActivity.this.startActivity(mainMenu);
MainActivity.this.finish();
overridePendingTransition(R.layout.fadein,R.layout.fadeout);
}
}, SFEngine.GAME_THREAD_DELAY);
}
See the API reference
public final boolean postDelayed (Runnable r, long delayMillis)
You should use Runnable. Try
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent mainMenu = new Intent(MainActivity.this,
SFMainMenu.class);
MainActivity.this.startActivity(mainMenu);
MainActivity.this.finish();
overridePendingTransition(R.layout.fadein,R.layout.fadeout);
}
}, SFEngine.GAME_THREAD_DELAY);

Android: ProgressDialog not spinning

I am having problems getting the ProgressDialog wheel spinning. Here is my code:
final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading...", true, false);
Thread thread=new Thread(new Runnable(){
public void run(){
runOnUiThread(new Runnable(){
#Override
public void run() {
if(dialog.isShowing())
// starts a foreground service, does database stuff,
// sets up a spinner with values
dialog.dismiss();
}
});
}
});
thread.start();
Everything goes as planned, I get the ProgressDialog, stuff happens in the background and once set, ProgressDialog goes away - the only problem is that the animation in ProgressDialog is not spinning, pretty much rendering it useless.
What am I doing wrong?
The code you omitted here
// starts a foreground service, does database stuff,
// sets up a spinner with values
must do something that block the UI thread. Just put them outside the runOnUiThread() Method.
Thread thread=new Thread(new Runnable(){
public void run(){
// starts a foreground service, does database stuff,
// sets up a spinner with values
runOnUiThread(new Runnable(){
#Override
public void run() {
if(dialog.isShowing())
dialog.dismiss();
}
});
}
});
ProgressDialog example using handler android
final ProgressDialog dialog = ProgressDialog.show(this, "Title",
"Message", true);
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
dialog.dismiss();
}
};
Thread t = new Thread() {
public void run() {
// write process code here.
handler.sendEmptyMessage(0);
}
};
t.start();
Fix issue: ProgressDialog not working
Because you put the processing dialog in to wrong area, for example with my error:
I have a two activities: MainActivity and ShowingActivity, MainActivity will show processing dialog after new intent tranfer to ShowingActivity and ShowingActivity will get some DATA from server (it will be blocked here). When I call ProcessingDialog in MainActivity, it showed but not spinning because my Intent move to ShowingActivity and it block Ui because action of get DATA from server must wait some seconds. So I fixed it:
1/ in MainActivity call:
final Intent working = new Intent(getApplicationContext(),
WorkingActivity.class);
final ProgressDialog ringProgressDialog = ProgressDialog.show(
MainActivity.this, "Please wait ...", "Connecting ...",
true);
ringProgressDialog.setCancelable(false);
new Thread(new Runnable() {
#Override
public void run() {
//Menthod need time to load:
getDatafromServer();
if (BookListFragment.isLoaded) {
ringProgressDialog.dismiss();
startActivity(working);
return;
}
});
So it will not block Ui because MainActivity still runing after DATA was gotten.
And in ShowingActivity I will use this DATA (because I set DATA is a static String).

Access a custom class from a new thread in a ProgressDialog on Android

I have created a ProgressDialog in android and it works when I do a simple example.
For example, this works.
public void onClick(View v)
{
// Perform action on click
System.out.println("Progess Bar");
final ProgressDialog myProgressDialog = ProgressDialog.show(AndroidTestApplicationActivity.this,
"Please wait...", "Getting updates...", true);
new Thread()
{
public void run()
{
try
{
// Do some Fake-Work
sleep(5000);
}
catch (Exception e)
{
}
// Dismiss the Dialog
myProgressDialog.dismiss();
}
}.start();
}
But once I add in a reference to my custom class, it just stops running this new thread.
button1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// Perform action on click
System.out.println("Progess Bar");
// Display an indeterminate Progress-Dialog
final ProgressDialog myProgressDialog = ProgressDialog.show(AndroidTestApplicationActivity.this,
"Please wait...", "Getting Updates...", true);
new Thread()
{
public void run()
{
try
{
HealthySubObject hsObject = new HealthySubObject();
// Do some more work with my hsObject - nothing happens after this point.
sleep(5000);
}
catch (Exception e)
{
}
// Dismiss the Dialog
myProgressDialog.dismiss();
}
}.start();
}
});
What happens is that as soon as I click this button, the progress dialog flashes up on the screen real quick and then disappears. But if you look at my code, it should wait 5 seconds before disappearing. I have put debug statements before and after the reference to my custom class and I can see the statements before but not the ones after. Does anyone have any idea why that is happening? As long as my class is public I should be able to call it from a new thread, right?
I am still pretty new to android and this is my first adventure into multi-threaded android apps. Any help would be much appreciated.
SOLUTION
Thanks for your help everyone. It is working now.
button1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// Perform action on click
System.out.println("Progess Bar");
//ProgressDialog dialog = ProgressDialog.show(AndroidTestApplicationActivity.this, "", "Loading. Please wait...", true);
// Display an indeterminate Progress-Dialog
final ProgressDialog myProgressDialog = ProgressDialog.show(AndroidTestApplicationActivity.this,
"Please wait...", "Doing Extreme Calculations...", true);
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
HealthySubObject hsObject = new HealthySubObject();
ArrayList<HashMap<String, String>> onlineDB = hsObject.jsonToArray();
//
// more stuff goes here.
//
//
myProgressDialog.dismiss();
}
}, 1500);
}
});
I would really recommend to use Handler instead of Thread. Using the Thread.sleep method is actually discouraged. Something like this is much better:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
HealthySubObject hsObject = new HealthySubObject();
myProgressDialog.dismiss();
}
}, 5000);
The problem is that you need to be on the UI thread to do modify the UI, and inside the run() method of your Thread you are in a "background" thread. Try using a handler inside your thread when you need to access the UI thread.

Categories

Resources