1

I have a MainActivity class in Android that loads first screen. When starting, I need to run another method in another thread and then either show message or go to another screen. For UI not to hang. How to do this? In which place of code?

// UPDATED CODE

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;

import android.view.Menu;
import android.widget.Toast;


public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Context context=getApplicationContext();
    String Internet=String.valueOf(isNetworkAvailable(this));
    Toast toast = Toast.makeText(this, Internet, Toast.LENGTH_SHORT);
    toast.show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public static boolean isNetworkAvailable(Context context) 
{
    return ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo() != null;
}

}

user2543953
  • 123
  • 1
  • 3
  • 11
  • 3
    AsyncTask might help you: http://developer.android.com/reference/android/os/AsyncTask.html – DigCamara Jul 05 '13 at 19:09
  • 1
    you can use `thread` or `asynctask`. you need to be more specific. – Raghunandan Jul 05 '13 at 19:09
  • @Raghunandan - no, they don't need to be more specific as either solution will work fine. – Chris Stratton Jul 05 '13 at 19:18
  • @Raghunandan - a service is not by itself a solution as a service does not automatically imply a different thread (though of course a service can be explicitly written so that it will utilize a background thread). Using a handler for the UI update has already been mentioned in Romiox's answer, and the runOnUiThread alternative by DigCamara. – Chris Stratton Jul 05 '13 at 19:24
  • @ChrisStratton yes. you will have a create thread for the service. i deleted my previous comment as i just realized about what you said. – Raghunandan Jul 05 '13 at 19:26
  • Thanks. This case is rather simple. But what if I must run music after loading UI? Must it be in a separate thread? – user2543953 Jul 05 '13 at 19:27
  • @user2543953 - probably, unless you can use some set source and forget type API. And if you want the music to span across various Activities you may want to put that thread in a service. – Chris Stratton Jul 05 '13 at 19:30
  • @user2543953 in that case use service . http://marakana.com/forums/android/examples/60.html. Note that services, like other application objects, run in the main thread of their hosting process. – Raghunandan Jul 05 '13 at 19:31

1 Answers1

5

You can go the Java route and utilize a normal Thread. Note, however, that you will need a Handler (class located in the Android SDK) to propagate your changes to the UI.
The more Android-kind-of way would be an AsyncTask. It is designed specifically for tasks that have to run in parallel to the UI and provides a mechanism to call back to the UI, so you don't have to implement that yourself (exactly what you would do with the handler if you decide to choose the furst option).

Janis F
  • 2,637
  • 1
  • 25
  • 36
  • 5
    you can use runOnUiThread to propagate your changes to the UI as well. – DigCamara Jul 05 '13 at 19:16
  • Now I'm trying firstly to Toast the result of checking the connection in onCreate method: String Internet=String.valueOf(this.isNetworkConnected()); Toast toast = Toast.makeText(getApplicationContext(), Internet, Toast.LENGTH_SHORT); toast.show(); But it stops the app. How I can right call isNetworkConnected() method here? – user2543953 Jul 05 '13 at 19:33
  • Now code is updated. I've updated Internet checking method name and implementation. – user2543953 Jul 05 '13 at 19:42
  • @user2543953 do you this `` in manifest?. – Raghunandan Jul 05 '13 at 19:45
  • I haven't used it recently, but I believe you have to pass `this` or `MainActivity.this` as Context to the Toast. (But I am really not too sure about it, sorry) – Janis F Jul 05 '13 at 19:46
  • 1
    @user2543953 also check this http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context – Raghunandan Jul 05 '13 at 19:49
  • I remember to have added this to manifest, but now when adding and saving, this disappears some way. – user2543953 Jul 05 '13 at 19:52
  • The changes in SDK are not visible in FAR Manager. Also when exporting to .apk, this permissions disappear %) – user2543953 Jul 05 '13 at 19:57
  • Thanks! Now seems to be working after adding directives and changing context to this. Need to test without Internet, would it write false. And then do another thread... but Internet checks rather fast... The bug is why permissions are not saved properly sometimes. – user2543953 Jul 05 '13 at 20:07
  • @user2543953 that has nothing to do with your code may be the editor causing the problem. i am talking about permissions not saving. – Raghunandan Jul 05 '13 at 20:15
  • Yes, I understand. But the problem was I added this permission earlier, but later it disappeared. After adding one more time it works. Before it didn't either ask about them when installing on the phone. Thanks. – user2543953 Jul 05 '13 at 20:20
  • So if the Internet check consumes little time - there is no reason to add it to another thread? Or in some situations the check can be durable? – user2543953 Jul 05 '13 at 20:22