Android: run animation while loading stuff - java

I'm reading a lot of files from the network. This takes quite a long time. Because of this i want to run a little animation (like a progress animation). But the animation only starts when the other stuff is finished. How can i run the animation on the UI Thread (in a Dialog!)?
AnimationDrawable animation = (AnimationDrawable) ivAnimation.getBackground();
animation.start();

Your main thread cannot be simultaneously downloading data and running an animation, also you cannot modify the UI from a background thread. As a result, you cannot "run an animation in the background", your animations and all UI tasks must be executed on the main thread while your data is downloading on a background thread.
Android, fortunately, has AsyncTask just for this purpose...
new AsyncTask<String, Integer, List<MyData>>() {
#Override public void onPreExecute() {
startMyAnimation();
}
#Override public List<MyData> doInBackground(String... urls) {
List<MyData> data = new ArrayList<>();
int counter = 0;
publishProgress(counter);
for(String url : urls) {
data.add(getMyDataFromNetwork(url));
publishProgress(++counter);
}
return data;
}
#Override public void onPostExecute(List<MyData> result) {
stopMyAnimation();
updateMyUiWithData(result);
}
#Override public void onProgressUpdate(Integer filesDownloaded) {
updateUiWithFileCount(filesDownloaded);
}
}.execute(url1, url2, url3, etc);

Check out Volley framework for performing long running network related operation. It's easy to use and very good, built exactly for networking in the background.
A general rule in Android (or any other GUI applications) is to never run long running operation on the UI thread. In android this will get you ANR's and will kill your app.
In general here's what U should do:
Start the download using Volley or an AsyncTask or something else (Will run in background).
Create and show a progress dialog to display to user (On UI thread).
Once an answer/response is received, call dialog.dismiss() on the progress dialog.
Hope this helps

Use Asynchronous task for this. Asynchronous task in android
In onPreExecute() method of Asynchronous task show animation, progress bar or anything whatever you want to show. Used doInBackground for downloading the stuffs.

Related

A method besides onPause?

I have an app that has three activities, the user will be constantly tabbing between these three activities. Right before the user closes the app, my code downloads the current time from the internet and stores it. The problem is that i have my code for downloading the time in the onPause() method. This causes the data to be downloaded over and over each time the user switches activities. I tried using onDestroy() but the download would never start. is there a method that is called when the user minimizes or closes the app altogether instead of one that is called on an activity switch?
Thank you very much, any help is appreciated!
We can achieve this using the Application class. There we can implement the ActivityLifecycleCallbacks to identify when our app goes to the background and based on that result we can perform our required task. Here, a sample code:
public class MyApplication extends Application implements ActivityLifecycleCallbacks {
private Handler mHandler;
#Override
public void onCreate() {
super.onCreate();
mHandler = new Handler();
}
...
#Override
public void onActivityResumed(Activity activity) {
Log.i("Activity Resumed", activity.getLocalClassName());
// cancel scheduled download operation (if any)
mHandler.cancelCallbacksAndMessages(null);
}
#Override
public void onActivityPaused(Activity activity) {
Log.i("Activity Paused", activity.getLocalClassName());
// schedule a download operation after 5 seconds
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
performDownloadOperation()
}
}, 5000);
}
... // other lifecycle callbacks
private void performDownloadOperation() {
// perform download operation here
}
}
In the above code, we are basically giving the user [and the system] a grace time of 5 seconds to switch from one activity to another. Otherwise, we note that the app went to background and do our stuff there. So, even if the user switches to a different app for 4 seconds and then come back, we'd still not download the data, which should be fine in most cases.
Note that to use the above class, you need to provide the class reference to the android:name property as android:name=".MyApplication" under the <application> tag in the manifest file.
You can make use of Activity::isFinishing(). This will return false when you (1) launch another activity or when (2) the app is put on background, but it will return true when (3) the back button is pressed or if (4) somebody calls finish on the activity.
If it is important to distinguish between (1) and (2), then you can always keep a flag that you can switch every time you launch an activity and reset it in onPause.

Perform remaining task of AsyncTask that was started in previous instance of the same activity

I am using AsyncTask to post some status on social media.
private class PostOnSocialMedia extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
pDialog.setTitle(R.string.posting_status_on_social_media);
Log.d(TAG, "onPreExecuteCalled.");
}
#Override
protected String doInBackground(String... params) {
Log.d(TAG, "doInBackground.");
currentActivity.this.twitter.tweet(params[0]);
currentActivity.this.facebook.post(params[0]);
currentActivity.this.googlePlus.share(params[0]);
return null;
}
#Override
protected void onPostExecute(String result) {
if (pDialog.isShowing()) {
pDialog.dismiss();
}
// Start next Activity when posted on all social media.
Intent intent = new Intent(currentActivity.this,
nextActivity.class);
intent.putExtra("userMatches", userMatches);
startActivity(intent);
Log.d(TAG, "onPostExecute.");
}
}
When user is not logged in to, lets say, Twitter I have to start a new Intent to show twitter page(so that user can log in). This causes the flow to return back to AsyncTask and calls a onPostExecute() method. And new Activity is started.
But I don't want this. What I want is that user login to Twitter and then it will come back to application (when user clicks login flow comes back to current activity. But the activity is created as a new one not the one from which I started a call to social medial post). When it comes back to application, I call a method of Twitter class to proceed to complete the uncompleted posting task. And once it is done, then only I want to go to next activity.
So, is there anyway to handle this situation?
EDIT :
Is it possible to solve this by using onSaveInstanceState(Bundle savedInstanceState) and onRestoreInstanceState(Bundle savedInstanceState)?
before calling asynctask make sure your user is logged in on both platforms, then call asynctask, because now in this case there are lots of combinations of a user can do, so my suggestion is first make sure user is logged in on both facbook, tiwtter etc. then call this asynctask so you dont have to make the asynctask wait
AsyncTask used for background Process.
So you have to check All the condition before calling asynctask and then used.
Generally, AsyncTasks are to do stuff in background without blocking the UI-Thread. If you want your Main-Thread (= UI-Thread) to wait for your AsyncTask, you could just do it without an AsyncTask.
If you want to wait for it though, try yourTask.get();, that should make your Main-Thread wait for the result of your Task. Look here for more info about get().
I solved my problem by using a static variable which is set to true just before recreating an Activity and when Activity execution is in onCreate I use this variable to decide whether this activity is recreated and if yes, then call method to perform remaining task.
When posting is done I call a method of activity, which calls a method to post on social media, from Twitter class where post is successfully done.
Method in activity :
public static void postDone() {
// Go to next activity
}
Call from Twitter class :
MyActivity.postDone();

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.

Running application while idle for Android

I was wondering if there is a way to keep running my application (which basically just refreshes a URL every X seconds) even when the phone idles/locks? (when the screen turns off). I'm currently using a WebView to load the URL, and a Runnable to reload it.
When I tried using a service, it keeps giving me a problem - webview cannot be resolved. I looked it up, and I couldn't get it fixed.
I would also like to know if there is a way to browse a URL in the background. Basically, browsing a URL without showing the browser :D
Any help would be much appreciated!!!
Sorry if these questions are dumb, I've only started Android programming for a few days.
You do not need a webview to poke web server, unless you like to display results imeediately.
You can use built in http client for this purpose ( works from service and does not vae user interfce)
I think Timer task will solve your problem.
Timer timer=new Timer();
timer.schedule(new DelayedTask(),1, 1000);// it will repeat within 1000ms
private class DelayedTask extends TimerTask
{
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
webview.loadUrl(url);
}
});
}
}
#Override
public void onDestroy()
{
super.onDestroy();
timer.cancel();
}

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