Is it ok to block UI thread while showing an Alert dialog - java

I have this method which basically waits for items in the singleton queue to become empty, there is a background service which is running and the service stops once it removes all items in the queue and process each one by one. This code runs in the main thread, what will happen when I call wait here? will the alert dialog still be showing and blocking the user from performing any other action?
void waitForService() {
openConnectionToUploadQueue();
if(answersQueue.getCount(objInterviewQuestion.getQid()) <= 0){
answersQueue.close();
return;
}
if(!answersQueue.isInterviewUploadServiceRunning()) {
answersQueue.startInterviewUploadService();
}
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(getString(R.string.auto_submit_alert_title));
builder.setCancelable(false);
builder.setMessage(R.string.uploading_pending_answers);
AlertDialog waitForServiceDialog = builder.create();
waitForServiceDialog.show();
while (answersQueue.getCount(objInterviewQuestion.getQid()) > 0) {
// do nothing and keep loop running till answersQueue is empty
}
waitForServiceDialog.dismiss();
}

You should never block UI Thread. When you hold UI Thread for too long, this is when the system will show a dialog saying XXX is not responding and ask user to kill your application.
Instead, you should use a callback style call, and when the service is up, and you receive the method call from callback, you dismiss the dialog.
Edit:
As discussed, you would need to implement BroadcastReceiver
Here is a demo project of mine for something else, you can use it as a sample on how to create and use BroadcastReceiver.
https://github.com/cyfung/ActivityRecognitionSample

Related

How to dismiss an alert in JavaFX?

I need to have a sort of "blocked alert" that can't be dismissed by the user until some event occurs. So, I created the alert and removed all the buttons from it:
Alert waitingAlert = new Alert(Alert.AlertType.INFORMATION);
waitingAlert.setTitle("Proposta Inviata");
waitingAlert.getButtonTypes().setAll();
waitingAlert.setHeaderText("La proposta รจ stata inviata a " + trade.getPlayer2Name());
waitingAlert.showAndWait();
Then, I need to dismiss it when a particular event occurs. I just tried doing it this way:
waitingAlert.close();
But it doesn't work.
Edit
The panel is shown when an user wants to send a request to another user in the network that needs to be accepted or refused. The principle is that the UI gets blocked until the other user says if he has accepted or refused the request. The request is send through a remote method invokation through a ring network. When I receive the response I want to close dismiss the panel.
To have a dialog that can't be dismissed by the user, but wait on a process, you will need to use dialog.show() as noted by James_D.
However, without buttons, the dialog actually will not close and you need to force it.
//for example
dialog.getButtonTypes().add(ButtonType.CANCEL);
dialog.hide();
dialog.getButtonTypes().remove(ButtonType.CANCEL);
This will allow you to close a dialog which has no buttons.

How to stop intent service after closing app

I have added Google Now search within my application. The way it works is, the user types something in the EditText object and oonce they press the button, this intent runs -
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
String term = searchInput.getText().toString();
intent.putExtra(SearchManager.QUERY, term);
startActivity(intent);
However I am having issues killing the service after the intent has been launched.
calling finish();after the intent will not work unless I am directing the intent to a different class itself.
Here's a video preview fo what I mean, you can clearly see I am pressing home to close the app but when I reopen it . It opens to the Google Now app. But when pressing back, it stops. How exactly can I get the intent to stop after I press the home key?
And my app is a dialogue type app so I have excluded it from the recent menu because of allot of reasons. Everywhere else the app kills itself as soon as the user exits it but here the service keeps running.
Video - Video Preview
I have also tried many questions answers, have researched but nothing has helped so far. I may be doing something wrong. Could someone kindly help me out here, thank you very much! :)
A started service must manage its own lifecycle. That is, the system does not stop or destroy the service unless it must recover system memory and the service continues to run after onStartCommand() returns. So, the service must stop itself by calling stopSelf() or another component can stop it by calling stopService().
use stopSelf() upon compleetion of task assigned to service.
And if you want to finishs ervice when application finishes, then in onDestroy(), call stopService() after checking whether its running or not, such method can be used:
public static boolean isMyServiceRunning(Class<?> serviceClass, Activity pActivity) {
ActivityManager manager = (ActivityManager) pActivity.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
to stop service from activity use below code:
stopService(new Intent(yourMainActivity.this,yourIntentService.class));
or you can write condition in service to stop itself like below:
if(check your condition here)
stopSelf();
You don't have to stop an IntentService - it will terminate itself when it has no more work to do.
It runs on separate worker thread.
Multiple intents are queued on the same worker thread.
So even if your Activity was destroyed from where this service was started it will continue performing its tasks until all the queued ones are complete and after completion it is automatically terminated.
Caution: It's important that your application stops its services when it's done working, to avoid wasting system resources and consuming battery power. If necessary, other components can stop the service by calling stopService(). Even if you enable binding for the service, you must always stop the service yourself if it ever received a call to onStartCommand().
stopService() : Request that a given application service be stopped. If the service is not running, nothing happens. Otherwise it is stopped. Note that calls to startService() are not counted -- this stops the service no matter how many times it was started.
Note: that if a stopped service still has ServiceConnection objects bound to it with the BIND_AUTO_CREATE set, it will not be destroyed until all of these bindings are removed.

update UI MapView from service

I have service and a mapActivity.
I wish to update mapview outfrom serivce (draw new oerlays lets say each 15 sec.)
I get error:
can't create handler inside thread that has not called looper.prepare();
my code:
private void startService() {
timer.scheduleAtFixedRate( new TimerTask() {
public void run() {
MyMap map = new MyMap(); // here is where app crashes...}
}
}
You can't have UI code in a background service. Anything running in a service runs on a background thread rather than on the app's UI thread, meaning that you cannot make UI changes from there. That's why you get the error.
If you sit back and think about it, architecturally there is no reason to have UI code in a background service. Drawing the map is a purely foreground operation and ceases to matter as soon as the map activity is dismissed. Hence no need to have background code stick around in a service -- all the overlay code belongs in the foreground map activity.

How to Send Message From AlertDialog to Activity

I'm trying to ask the user if he wants to retry a login process if the initial one has failed.
I do this using an AlertDialog. The problem is i'm going around in a circle. Let me explain:
I have the login method named Login which I call from the main activity. If this fails, I open an AlertDialog. The response from AlertDialog comes on the main thread. How do I make the AlertDialog dissapear before I call Login again?
Now I have something like this onCreate->Login->AlertDialog->ResponseHandler->Login. If I do this, the AlertDialog will never close because everything happens on the same thread.
Is there a way to send the response from the AlertDialog to the activity in a asynchronous way?
You can declare a Handler object as an anonymous inner class in your activity and on its reference call the sendEmptyMessage(0). in the handlerMessage() of the handler instance just dismiss the AlertDialog.
Here is a tutorial link which will help you more http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html

After making a call programatically, my android app crashes

The title explains all... I have this snippet of code in my application:
String url = createTelUrl("3112007315");
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(url));
context.startActivity(intent);
It does make a call, but once the call ends, my application crashes. I'd like to return to my application once the call has finished, but I read this post and it seems not to be possible. So... is there anyway to at least pause my application and resume it once the call has finished?
EDIT:
Thanks for the two answers I received, I feel I'm really close to my goal... I had already done some of the things you guys suggested. But, maybe I didn't explain some details of the application... I'm developing Who Wants To Be A Millonarie game, so I need to implement calls (I don't know how it's called in USA or other countrys, but here we call it "call to a friend").
Anyway... I've done too many changes to this app and now it's not crashing. But, the Canvas where I draw the UI is not been showed once the called has ended.
I have a SurfaceView that holds the UI. For that SurfaceView I created a thread that is meant to refresh the UI... this is basically what the thread does:
#Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
c = _surfaceHolder.lockCanvas(null);
// Check if should wait
synchronized (_surfaceHolder) {
_panel.onDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
But, once the call has ended I get a black screen. The surface is there (I know it because it still can receive some touch events), but it's not showing anything. Another thing to take in account is how I'm starting the Thread from the SurfaceView class:
public void surfaceCreated(SurfaceHolder holder) {
hilo.setRunning(true);
try{
hilo.start();
}catch(IllegalThreadStateException ite){
Log.e("wwtbam", "god dammed");
}
}
This worked nice before I start implementing phone-calls. The problem here is that once the call has ended and it executes again the start method which throws a IllegalThreadStateException because the thread has already been started. I've tried using some 'technics' to pause the UI thread while calling but I haven't been able to solve this problem. I tried doing something like:
// this in the UI thread class
if(haveToWait)
wait();
....
// this in the surface view class
if(callEnded)
hilo.notify();
But that didn't work. I also have tried some other 'tricks' like using sleep(50); instead of wait(); but it does not work either.
With all that information I provided... what could you suggest to me?
The problem here is the place you're using to start the thread. Once you start a new call, your main activity will be paused and the surfaceview will be destroyed. Though, the thread will keep running. So, once your app takes the control back, the surface is created again and the start method will be invoked. That causes a IllegalThreadStateException.
The way to go here is to manipulate the thread out of the SurfaceView class. That will give you the control of the thread from the main activity, and you will be able to decide when to start or pause your thread.
Take a look of this example: http://code.google.com/p/apps-for-android/source/browse/trunk/SpriteMethodTest/src/com/android/spritemethodtest/
This is possible using an android.telephony.PhoneStateListener.
First, we need to take care of the manifest of the app:
We need the permission to make calls (duh!) as well as the permission to watch the phone state. The latter is needed so the app can react to the ending of a call as well. So we add these lines to out application manifest:
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Also, we don't want Android to start a different instance of our activity when the call has ended, so we set the launchMode attribute of the activity to "singleInstance".
<activity android:name=".CallTest" android:label="Calling Test"
android:launchMode="singleInstance" />
Having prepared everything in the manifest, we can now look at the activity making the call:
public class CallTest extends Activity {
PhoneStateListener mListener;
TelephonyManager mTelMgr;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListener = new CallEndedListener();
mTelMgr = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
}
public void makecall(View v) {
// Register our listener to be notified of the beginning
// and ending of calls
mTelMgr.listen(mListener, PhoneStateListener.LISTEN_CALL_STATE);
// Start the call
Intent call = new Intent(Intent.ACTION_CALL);
call.setData(Uri.parse("tel:12345"));
startActivity(call);
}
class CallEndedListener extends PhoneStateListener {
boolean called = false;
#Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
// Don't fire before the call was made
if (state == TelephonyManager.CALL_STATE_OFFHOOK)
called = true;
// Call has ended -- now bring the activity back to front
if (called && state == TelephonyManager.CALL_STATE_IDLE) {
called = false;
mTelMgr.listen(this, PhoneStateListener.LISTEN_NONE);
startActivity(new Intent(CallTest.this, CallTest.class));
}
}
}
}
The only new thing in the makecall method, compared to the code snippet in the question, is the PhoneStateListener implementation added right before actually making the call. This listener then gets notified by Android when an outgoing call is dialed, an incoming call is ringing or when an active call is ended.
Our implementation waits for the latter CALL_STATE_IDLE event and starts our activity again, so that after the call has ended we're back in our app where we left it. It then deregisters itself, so our activity doesn't get restarted every time the user ends a call not initiated by our own activity.
However, when registering for the CALL_STATE-events with the TelephonyManager, Android instantly fires a notification with the current status -- so our listener would get triggered before the call had even started. Therefore our listener implementation first waits until an outgoing call was started (CALL_STATE_OFFHOOK) and only after that happened reacts to the CALL_STATE_IDLE notification.
HTH!
As for the crash - please post the log and put your debugger onStart/onResume to find out why you're crashing. It's possible that something is initialized in the wrong place and you might something as simple as nullpointer.
As for the call end thing - i've never try this, but i'd try to register receiver, catch
http://developer.android.com/reference/android/telephony/TelephonyManager.html#ACTION_PHONE_STATE_CHANGED
Evaluate the state of the phone and do what you need to do .
Also there is more info here
http://developer.android.com/reference/android/telephony/
http://developer.android.com/reference/android/telephony/PhoneStateListener.html
And finally you'll find examples of how to use that in the applications that are use that functionality in source.android.com

Categories

Resources