I will open the Activity(name is VideoAcivity). This Activity is doing some video proccess and show the video. I know how to use progress dialog, Already I'm using progressdialog between my activities but i couldn't use progressdialog without AsyncTask, onPreExecute, onPostExecute. I want to show progressdialog when activity started without using AsyncTask because i'm using Thread in this Activity.
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
txtDisplay = (TextView) findViewById(R.id.textView1);
getin = getIntent();
video = getin.getStringExtra("Video_URL");
subtitle = getin.getStringExtra("Subtitle_URL");
mPreview = (SurfaceView)findViewById(R.id.surfaceView1);
holder = mPreview.getHolder();
holder.setFixedSize(800, 480);
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(this);
mediaController = new MediaController(this);
} // this is oncrete
// ---> mediaplyer and mediacontroller codes..
public void onPrepared(MediaPlayer mediaPlayer) {
Log.d(TAG, "onPrepared");
mediaController.setMediaPlayer(this);
mediaController.setAnchorView(findViewById(R.id.surfaceView1));
handler.post(new Runnable() {
public void run() {
mediaController.setEnabled(true);
mediaController.show();
}
});
}
Thread th = new Thread(new Runnable() {
#Override
public void run() {
// Codes..
}
});
th.start();
th.join();
I tried to add some codes and debugged, whatever i tried, ProgressDialog is not appearing when the activity start. I couldn't find the reason. It is appearing a little time at the end of the proccess. (All Proccess is taking 7-8 seconds, it is appearing only 1 second) Where am i doing wrong ? Where should I add the progress dialog codes? Hope you can help me..
Thanks in advance
Get progressDialog declared and put this in onCreate method-
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loding");
progressDialog.show();
progressDialog.setCancelable(false);
And when your background task completes -
progressDialog.dismiss();
You need some kinda callback when your thread stops or you complete the task which you were doing in background. In asynctask you get onPostExecute method and it runs on main UI thread so you can dismiss progressDialog or do some other UI stuff. But in case of thread you don't have such method.
You need to identify when thread stops and then use this if you're still in thread-
runOnUiThread(new Runnable() {
#Override
public void run() {
progressDialog.dismiss();
}
});
You are calling the Thread.join() method on your worker thread, seemingly from your main thread. This will make your main thread wait until your worker thread has completed. It won't be able to update the UI properly during this time. Android needs the main thread to be free to display the ProgressDialog.
What are you trying to accomplish with Thread.join() here? Try to remove it and see what happens.
Related
EDIT:
I did like this with the help of my stack overflow friends inside my thread to display the video view inside of the dialog in the android but it freezes my application help me Big thanks in advance
This is My activity from the Thread :
Auto_Bucket_Tests_Thread = new Thread(new Runnable()
{
#SuppressWarnings("deprecation")
#Override
public void run() {
while(Test_Completed==false)
{
if(Login.Bucket_Status==true && Video_Status==false)
{
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
Bucket_Open_Error();
}
});
}
else if(Login.Bucket_Status==false && Video_Status==true)
{
videodialog.cancel();
}
}
Auto_Bucket_Tests_Thread.stop();
}
});
}
protected void Bucket_Open_Error() {
videodialog = new Dialog(this);
videodialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
videodialog.setContentView(R.layout.videodialog);
videodialog.show();
WindowManager.LayoutParams layout_params =new WindowManager.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layout_params.copyFrom(videodialog.getWindow().getAttributes());
layout_params.dimAmount=0;
videodialog.getWindow().setAttributes(layout_params);
final VideoView video = (VideoView)videodialog.findViewById(R.id.videoView_dialog);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.doorsclosing);
video.setVideoURI(uri);
video.start();
video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
Video_Status=true;
video.start();
//app_message.show();
}
});
You are trying to create a Handler from a back thread. If u dont want the handler to communicate with the UI thread then create a different thread and not Handler. If u want the Handler to communicate with the UI thread then u need pass a Looper to the constructor.
new Handler(Looper.getMainLooper());
EDIT: replace
runOnUiThread
with
new Handler(Looper.getMainLooper()).post
runOnUiThread is an Activity method which create a handler and post the Runnable in the UI thread, but because youare running on a back thread (created new Thread and ran it) you cant post to the UI like this becuase the new Handler has no connection to the UI (hence the didint call Looper prepare exeception).
I have been getting help to create a progress bar for my Android application. Lots of help here! I'm having an issue though that I am having a hard time fixing. I have a progress bar shown while the application attempts to download files from a networked computer. This works perfectly fine, however I need to update my UI incase an error occurs. I can't update the UI inside the thread and I want to update the UI from getRaceResultsHandler. Unfortunately it executes that code prior to the thread being completed. I have tried a few things with no luck. I have a code sample with my comments below if anyone can help.
public void getRaceResultsHandler (View view) {
dialog = new ProgressDialog(this);
dialog.setCancelable(true);
dialog.setMessage("Attempting to transfer race files. Please wait...");
// Set progress style to spinner
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
// display the progressbar
dialog.show();
// create a thread for downloading the files
Thread background = new Thread (new Runnable() {
public void run() {
//The Code here to execute the file download from the networked computer....
//Dismiss the progress bar because the download is either completed or failed...
dialog.dismiss();
}
});
// start the background thread
background.start();
//All Other Code Goes here to update the UI. Shows either an error message or a success based on the results of the download.
//My problem is that this code executes before the background thread is completed. I need it to wait until the thread is completed.
}
Try to dismiss that dialog using Handler
Handler h = new Handler(); // Create this object in UI Thread
Thread background = new Thread (new Runnable() {
public void run() {
h.post(new Runnable()
{
public void run()
{
dialog.dismiss();
}
};
});
You should use AsyncTask instead of normal Thread
AsyncTask<Void,Void,Void> aTask = new AsyncTask<Void,Void,Void>()
{
#Override
public void onPreExecute()
{
// Setup some UI Objects
}
#Override
public void onPostExecute(Void result)
{
dialog.dismiss();
}
#Override
protected Void doInBackground(Void...params)
{
// your download stuff
publishProgress(object) // <-- if you want to update the progress of your download task
}
});
Note: Didn't try my own, have no IDE here in my friend's laptop
I am using this code to display a Progress Dialog which is working fine:
dialog = ProgressDialog.show(this, "Please wait",
"Gathering Information...", true);
Thread thread = new Thread()
{
#Override
public void run() {
if(Chapter_sync.size()>0){
storemodule();
c.open();
for(int i=0;i<Chapter_sync.size();i++)
{
downloadPDF(Chapter_sync.get(i));
System.out.println("SYNCED"+i);
c.update(Chapter_sync.get(i));
}
}dialog.dismiss();
}
};thread.start();
LinearLayout parentlayout=(LinearLayout)findViewById(R.id.chapterholder);
parentlayout.removeAllViews();
setUpViews();
}
}
Here what I am trying to do is display a Progress dialog till all computation is done.
As it completes i wanted to setup all views again. But the setUpViews() is called before the thread starts. I am not so good at thread basics .Could any one help me understand why is this happening and how can I get my own results?
The problem is you are not using handlers. Simply do this,
dialog = ProgressDialog.show(this, "Please wait",
"Gathering Information...", true);
Thread thread = new Thread()
{
#Override
public void run() {
if(Chapter_sync.size()>0){
storemodule();
c.open();
for(int i=0;i<Chapter_sync.size();i++)
{
downloadPDF(Chapter_sync.get(i));
System.out.println("SYNCED"+i);
c.update(Chapter_sync.get(i));
}
}dialog.dismiss();
}
handler.sendemptyMessage(0);
};thread.start();
And in your onCreate() create Handlers,
Handler handler=null;
handler=new Handler()
{
public void handleMessage(Message msg)
{
progressDialog.cancel();
if(msg.what==0)
{
LinearLayout parentlayout=(LinearLayout)findViewById(R.id.chapterholder);
parentlayout.removeAllViews();
setUpViews();
};
You can't update your UI from background thread. Either you have to use AsyncTask or to use handlers from your background thread to inform your main thread that the background action has been completed.
Thread scheduling is dependent on the operating system. So instantiating your thread does not ensure that your thread will run whenever you want.
The problem you are facing can be best handled using async task. Or if you have a callback that lets you know when your download is completed then you can dismiss the dialog on the callback. Make sure you dismiss it inside a UI thread by doing.
mActivity.runOnUiThread() or any other such methods.
In your code if u see
After Starting the Thread you have call your method setUpViews(), which does not wait for your thread to complete and setups your views.
Use Handler.post after the dialog is dismissed in your thread which gather your information.
handler.post(new Runnable()
{
setUpViews();
});
So after the your operations completed your setupViews will be called by your Handler.
Sorry, the title is a bit hard to understand but I'm not 100% sure as to what to ask. its easier to show you the code and see if you understand from that. I found the way to use the progress dialog from another post on here, so this is basically adding onto that post. ( ProgressDialog not showing until after function finishes )
btw, this is using eclipse environment with the android plugin.
final MyClass mc = new MyClass(text,info, this);
final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
Thread t = new Thread(new Runnable()
{
public void run()
{
// keep sure that this operations
// are thread-safe!
Looper.prepare(); //I had to include this to prevent force close error
mc.doStuff();//does ALOT of stuff and takes about 30 seconds to complete... which is why i want it in a seperate thread
runOnUiThread(new Runnable()
{
#Override
public void run() {
if(dialog.isShowing())
dialog.dismiss();
}
});
}
});
t.start();
tmp = mc.getStuff();
now the issue is that tmp is always null because mc isnt finished doing stuff. So, if i do this it finishes doing stuff, but doesnt show the progress dialog..
t.start();
while(t.isAlive());//noop
tmp = mc.getStuff();
Any thoughts or ideas would be greatly appreciated!
In your second attempt, you are making the main thread wait for the new thread to complete.
The runnable in the runOnUiThread call is where you want to do tmp = mc.getStuff();
That will then be executed on the main thread after mc has finished doStuff().
But otherwise, check out the link blindstuff commented, it simplifies threading.
-->I am new to Android And i want to show two progress dialog one after another??
-->First i want to show when my image is load from internet, when this process is done i have set A button on that Remote image.
-->When i click that button i want Dialog for second time..(on clicking button i have set video streaming code.. before video is start i want to close that Dialog..)
Any Help????
Thanks...
You can create 2 threads. First for image when it is loaded then call another thread of video.
Make two runnable actions and two handlers to handle that
public void onCreate(Bundle savedInstanceState) {
ProgressDialog pd = ProgressDialog.show(this, "","Please Wait", true, false);
Thread th = new Thread(setImage);
th.start();
}
public Runnable setImage = new Runnable() {
public void run() {
//your code
handler.sendEmptyMessage(0);
}
};
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
if (pd != null)
pd.dismiss();
}
};
On Android it's best to use AsyncTask to execute tasks in the background while still updating UI:
Extend the AsyncTask
Start the progress dialog in onPreExecute()
Define the background task in doInBackground(Params...)
Define updating of the progress dialog in onProgressUpdate(Progress...)
Update dialog by calling publishProgress() from doInBackground()
Start a new Dialog #2 in onPostExecute().