Skip to content

Instantly share code, notes, and snippets.

@JamesHovious
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesHovious/9181905 to your computer and use it in GitHub Desktop.
Save JamesHovious/9181905 to your computer and use it in GitHub Desktop.
/*
* This class is a simple example showing a real world example of using AsyncTask to run a Network Thread.
* Nonrelevant is cut out in order to highlight the stucture of this class.
*/
public class PlayNowActivity extends Activity {
RelativeLayout btnNewGame, btnLoad;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_now);
// Importing all assets like buttons, text fields
btnNewGame = (RelativeLayout) findViewById(R.id.btnNewGame);
btnLoad = (RelativeLayout) findViewById(R.id.btnLoadGame);
btnNewGame.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
new NetworkOperation().execute("");
}
});
// Link to load profile
btnLoad.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Some future stuff
}
});
}
private class NetworkOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... operation) {
GlobalMethods m = new GlobalMethods();
//check network connection
if (m.isConnectionAvailable()) {
UserFunctions uf = new UserFunctions();
DatabaseHandler db = new DatabaseHandler(PlayNowActivity.this);
//For first time registration initialize all the values
//... sode codes
//return success indicator codes
if(status.equals("SUCCESS")){
return ("SUCCESS");
}else{
return ("FAIL");
}
} else {
return ("network_error");
}
}
@Override
protected void onPostExecute(String result) {
if (result.equals("SUCCESS")) {
Intent i = new Intent(PlayNowActivity.this, FirstAugmon.class);
startActivity(i);
finish();
}else if (result.equals("network_error")) {
NotOnlineDialog dlg = new NotOnlineDialog(PlayNowActivity.this);
dlg.show();
} else if (result.equals("FAIL")){
//TODO display something relevant and log it
Toast.makeText(PlayNowActivity.this, "You might want to log this...", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment