I want to populate a list after toast is disappear
I have a multiprocessing thing and in one method more around 10 toast is shown after doing something, i want to show in the list what is done what is not processed.
My problem is program runs at once and list will be populated as well. I want to show toast as it takes some delay than update list
Creating a Thread that lasts as long as the Toast is displayed and then you can do your work.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// your other stuff
Toast.makeText(this,"This is a Toast", Toast.LENGTH_LONG).show();
thread.start();
}
Now create a thread that waits for (LENGTH_LONG = 3.5) or (LENGTH_SHORT = 2) seconds
Thread thread = new Thread(){
#Override
public void run() {
try {
Thread.sleep(3500); // As I am using LENGTH_LONG in Toast
Your_Activity.this.finish();
} catch (Exception e) {
e.printStackTrace();
}
}
};
Related
I have a single handler instance, and I try to post two Runnables. But what I observe is only the latest Toast is getting printed on the device.
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),"Showing from main activity",
Toast.LENGTH_SHORT).show();
}
});
handler.post(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),"Showing from main activity new",
Toast.LENGTH_SHORT).show();
}
});
As per the explanation of Handler, it enqueues the runnables into the messages queue of the thread to which it it associated. Which means Both the toast should be displays in the order in which they are enqueued.
Can someone please explain this?
When you make a Handler associated with the main looper, you should keep in mind that it's associated with the main thread. So calling Thread.sleep in the main thread is absolutely discouraged and should be avoided.
Toasts use the UI thread as well, but you prevent it from appearing by freezing this thread. The steps happening in your code are as follow:
The action to show first Toast is enqueued
The action to show second Toast is enqueued
// First action execution
Make the thread sleep for 3
seconds
Showing first toast is enqueued
// Here first Toast should appear, but it doesn't happen right at the moment you called the method. Treat it as another message enqueued in the main looper
Make the thread sleep for 3 seconds
Showing second toast is enqueued
First toast is shown
Second toast is shown
In the end both toasts are shown, but you can see only the last one, because it's shown after the first and covers it. If you want to show two toasts with a short delay, use post delayed method, or something like:
final Handler handler = new Handler(Looper.getMainLooper());
final Context context = getApplicationContext();
handler.post(new Runnable() {
#Override
public void run() {
Toast.makeText(context, "Showing from main activity",
Toast.LENGTH_SHORT).show();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Toast.makeText(context, "Showing from main activity new",
Toast.LENGTH_SHORT).show();
}
}, 3000);
}
});
I try to show a progress dialog upon showing a sliding drawer.
this is opening drawer event handler:
public void onDrawerOpened(View drawerView) {
progress = ProgressDialog.show(activity, "dialog title",
"dialog message", true);
openDrawer();
}
Inside openDrawer() i call a function fillCommunityList() that i need to show the progress dialog while its execution
fillCommunityList() implementation is as the following:
private void fillCommunityList(){
new Thread(new Runnable() {
#Override
public void run() {
try {
// Here you should write your time consuming task...
UIManager manager = new UIManager(activity);
coms = manager.getCommunities();
progress.dismiss();
getOutTread = false;
} catch (Exception e) {
}
getOutTread = false;
}
}).start();
// to stop code till thread be finished
while(getOutTread){ }
SlidingMenuExpandableListAdapter adapter = new SlidingMenuExpandableListAdapter(this,
navDrawerItems, coms, mDrawerList);
mDrawerList.setAdapter(adapter);
}
Note:
I put a thread just to make progress dialog works
My problem is the following two points:
1- progress dialog appears too late for sudden and then disappears
2- Thread takes alot of time in its execution (without thread fillCommunityList() takes around 10 seconds but with a thread it takes more than a minute)
Note: manager.getCommunities() has asyncTask in its implementation
The problem is the following line
while(getOutTread){ }
this is call busy waiting. The UI Thread is busy looping and can't, at the same time, draw/update the ProgressDialog
I want when my app exit, after exit if there is no action perform on screen for 5 minutes a video will play every time, this video id define in my app.
Any help would be appreciated. Thankx in advance.
I have the following class but it is not worked fine, how can make the same code as Services ?
public class IdlePhoneState extends Activity {
Handler hl_timeout = new Handler();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
try{
hl_timeout.postDelayed(DoOnTimeOut, 120000); // 2 min
}catch(Exception e)
{
e.printStackTrace();
}
}
// Toast
Thread DoOnTimeOut = new Thread() {
public void run() {
try{
Toast.makeText(getApplicationContext(), "System is idle", Toast.LENGTH_LONG).show();
}catch(Exception e)
{
e.printStackTrace();
}
}
};
#Override
public void onUserInteraction()
{
super.onUserInteraction();
//Remove any previous callback
try{
hl_timeout.removeCallbacks(DoOnTimeOut);
hl_timeout.postDelayed(DoOnTimeOut, 120000);
System.out.println("ggggggggggggggggggggg");
Intent intr= new Intent(getApplicationContext(), VideoPlayerActivity.class);
startActivity(intr);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
Do like below.
you should set an id on the outer-most element of your layout:
android:id="#+id/entire_view"
In java file find it like below.
View view = getViewById(R.id.entire_view);
Write touchlistener code for that root layout.
view.setOnTouchListener( ...
in the touch save the touched time to shared prefereces are something like that. Then compare the saved time with current time. if the difference exceeds five mins then play the video.
For time duration checking try to use Alarm Manager else use CountDownTimer.
I want to make a Toast appear and THEN let a sleep operate.
If i do that, the Toast appears AFTER the sleep, but i want it the other way around. Anyone has a suggestion? Here my code for this
switch (checkedRadioButton) {
case R.id.radio0 : radioButtonSelected = "radiobutton1";
Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_LONG).show();
vd.vibrate(100);
android.os.SystemClock.sleep(1000);
vd.vibrate(100);
thanks so far
The display of toast is an asynchronous (i.e not a blocking call) operation, means once the toast request is executed, the operating system jumps to the next operation and meanwhile the toast is prepared and displayed.
To acquire your default behavior, you should execute the thread-sleep call after few seconds of delay. Use a Handler and its postDelay method for this.
Delay time should be like:
LONG_DELAY = 3500; // 3.5 seconds
SHORT_DELAY = 2000; // 2 seconds
Try to use AsyncTask class.Write this code as it is. no need to take the name of variable of class AsyncTask.
new AsyncTask<Void, Void, Void>() {
#Override
protected void onPostExecute(Void result) {
Toast toast2 = Toast.makeText(context, "Task completed",
Toast.LENGTH_SHORT);
toast2.show();
}
#Override
protected void onPreExecute() {
Toast.makeText(getApplicationContext(), "here is your text before Sleep", Toast.LENGTH_LONG).show();
}
#Override
protected Void doInBackground(Void... voids) {
try {
Thread.sleep(1000); // time in milisec 1000ms= 1sec
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
}.execute();
I make a dialog based on this website http://www.mkyong.com/android/android-progress-bar-example/
When progress bar goes to 100%, dialog closed but a toast still appears continuously. I noticed that after bar goes to 100%, progressHandler still runs looping.
How can I solved this problem?
Thing that I want : When progress bar goes to 100% then dialog closed and Toast shows and closed.
Thread background = new Thread(new Runnable() {
#SuppressWarnings("null")
public void run() {
try {
while (dialog.getProgress() <= dialog.getMax()) {
// wait 500ms between each update
Thread.sleep(100);
// active the update handler
progressHandler.sendMessage(progressHandler.obtainMessage());
}
} catch (java.lang.InterruptedException e) {
// if something fails do something smart
Toast.makeText(getApplicationContext(), "Error Occurs",Toast.LENGTH_LONG).show();
}
}
});
// start the background thread
background.start();
}
// handler for the background updating
Handler progressHandler = new Handler() {
public void handleMessage(Message msg) {
dialog.incrementProgressBy(increment);
if(dialog.getProgress()==100){
editor.putInt("lastestSummaryNoSent",summary.getCurrentSummaryNo());
editor.commit();
dialog.dismiss();
Toast.makeText(getApplicationContext(), "Update Finished", Toast.LENGTH_LONG).show();
}
}
};
This error
Can't create handler inside thread that has not called Looper.prepare()
is because of the Toast you have provided inside the background Thread. Remove the Toast inside the Catch phrase and you will be good to go.