Android Development: Having an AsyncTask in a separate class file - java

I have been playing around with various examples trying to familiarize myself with AsyncTask. So far all the examples I have seen have had the AsyncTask included into the onCreate method of the main activity. Which I don't like very much, so I was wanting to see how hard it would be to separate it into its own class. So far I have this:
the main activity
package com.example.asynctaskactivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.example.asynctaskactivity.ShowDialogAsyncTask;
public class AsyncTaskActivity extends Activity {
Button btn_start;
ProgressBar progressBar;
TextView txt_percentage;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_start = (Button) findViewById(R.id.btn_start);
progressBar = (ProgressBar) findViewById(R.id.progress);
txt_percentage= (TextView) findViewById(R.id.txt_percentage);
Log.v("onCreate","Attempt set up button OnClickListener");
btn_start.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
btn_start.setEnabled(false);
new ShowDialogAsyncTask().execute();
}
});
Log.v("onCreate","Success!");
}
}
the new seperate AsyncTask class
package com.example.asynctaskactivity;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class ShowDialogAsyncTask extends AsyncTask<Void, Integer, Void>{
int progress_status;
#Override
protected void onPreExecute() {
// update the UI immediately after the task is executed
Log.v("onPreExecute","1");
super.onPreExecute();
Log.v("onPreExecute","2");
//Toast.makeText(AsyncTaskActivity.this,"Invoke onPreExecute()", Toast.LENGTH_SHORT).show();
progress_status = 0;
Log.v("onPreExecute","3");
txt_percentage.setText("downloading 0%");
Log.v("onPreExecute","4");
}
#Override
protected Void doInBackground(Void... params) {
Log.v("doInBackground","1");
while(progress_status<100){
progress_status += 2;
publishProgress(progress_status);
SystemClock.sleep(300);
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progressBar.setProgress(values[0]);
txt_percentage.setText("downloading " +values[0]+"%");
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//Toast.makeText(AsyncTaskActivity.this,"Invoke onPostExecute()", Toast.LENGTH_SHORT).show();
txt_percentage.setText("download complete");
btn_start.setEnabled(true);
}
}
Originally this was all in the main activity, hence the mentions to the elements that the asynctask should in theory update. Obviously at present this is causing runtime errors, which then got me thinking. How can I have the file seperate but still update the UI thread.
Sorry if this is a stupid question, quite new to android development and background threads in particular.

How can I have the file seperate but still update the UI thread.
Okey. So at first you know that main advantage of AsyncTask added in Activity as inner class is that you have direct access to all UI elements and it makes possible pretty "lightweight" UI updates.
But if you decided to make AsyncTask separated from Activity(which also have some benefits e.q. code is more clean and app logic is separated from appearance) class you can:
You can pass UI elements via constructor of class
You can create various setters
You can create some interface that will hold callbacks. Look at Android AsyncTask sending Callbacks to UI
This is all what you need i guess.

Add a callback interface, and let your Activity implement it.
public interface MyAsyncTaskCallback{
public void onAsyncTaskComplete();
}
In the postexecute:
myAsyncTaskCallback.onAsyncTaskComplete();
In the constructor of your AsyncTask you could pass the instance of MyAsyncTaskCallback (your Activity).

Your best way of handling this is via a Handler. Instantiate one in the activity and override the method handleMessage(). When you create ShowDialogAsyncTask class just pass in the handler and maintain a reference to it. On postExecute you can construct a message and send it via the handler method sendMessage().
A previous answer mentioned using an interface and a callback paradigm. This will work, however, there is a chance that the activity can be destroyed and won't be present when the postExecute method is executed so you would need to test for this.

Related

Android: Overriding onCreate method of main activity

I have a Java activity like so.
package com.xxx.yyy.overrideoncreate;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("DEBUG","Original oncreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I wish to split some of the onCreate instructions to a separate class, so I created this too.
package com.xxx.yyy.overrideoncreate;
import android.os.Bundle;
import android.util.Log;
/**
* Created by oox on 26/6/17.
*/
public class SubClass extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("DEBUG","Overridden");
super.onCreate(savedInstanceState);
}
}
I intend for subClass.onCreate to override MainActivity.onCreate -- both Overridden and original oncreate messages should be displayed. However, that did not seem to happen, the overridden message did not appear in the Logcat.
Any idea what I did wrong?
Thanks in advance.
In order for SubClass's onCreate to be called, an instance of SubClass should be created instead of an instance of MainActivity when the application is launched.
For that to happen, SubClass should become the actual main activity of your application (i.e. the activity class registered in the AndroidManifest.xml which gets launched when the application is launched).

My Junit test doesn't run

I'm new to Java, Android and JUnit. I want to learn how to write JUnit tests for an Android application. To that end, I have a very simple Android app (2 activities, 2 buttons, each button goes to the other activity). I want to test the button. This app runs fine on my phone when it's plugged in. I've been looking at the samples provided in the SDK, and I am trying to emulate them.
My problem is that when I right-click on my test project, and choose Run As -> Android JUnit test, nothing happens. I don't know why.
My test code.
package com.example.twoactivities.test;
import android.app.Instrumentation.ActivityMonitor;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.SmallTest;
import android.widget.Button;
import com.example.twoactivities.MainActivity;
import com.example.twoactivities.MainActivity2;
public class ClickButton extends ActivityInstrumentationTestCase2<MainActivity> {
private Button mButton2;
private long TIMEOUT_IN_MS = 100000;
public ClickButton() {
super(MainActivity.class);
}
#Override
protected void setUp() throws Exception {
super.setUp();
final MainActivity a = getActivity();
// ensure a valid handle to the activity has been returned
assertNotNull(a);
}
#SmallTest
public void click(){
// Set up an ActivityMonitor
ActivityMonitor activityMonitor = getInstrumentation().addMonitor(MainActivity2.class.getName(), null, false);
//check if button is enabled
assertTrue("button is enabled", mButton2.isEnabled());
//click button
mButton2.performClick();
MainActivity2 MainActivity2 = (MainActivity2) activityMonitor.waitForActivityWithTimeout(TIMEOUT_IN_MS );
assertNotNull("MainActivity2 is null", MainActivity2);
// assertEquals("Monitor for MainActivity2 has not been called", 1, activityMonitor.getHits());
// assertEquals("Activity is of wrong type", MainActivity2.class, MainActivity2.getClass());
// Remove the ActivityMonitor
getInstrumentation().removeMonitor(activityMonitor);
}
// public void tearDown() {
// }
}
(I know it's really simple, but I'm just trying to get the basics down.)
My application.
package com.example.twoactivities;
import com.example.twoactivities.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void activity2(View view){
Intent intent = new Intent(this,com.example.twoactivities.MainActivity2.class);
startActivity(intent);
}
}
Activity 2 of my application.
package com.example.twoactivities;
import com.example.twoactivities.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void activity1(View view){
Intent intent = new Intent(this,com.example.twoactivities.MainActivity.class);
startActivity(intent);
}
}
Any ideas why my test class doesn't run?
Thanks,
Stephanie
You have to right click on the test itself, not the project.
According to documentation Android testing API supports JUnit 3 code style, but not JUnit 4. Try naming your test method testClick

Activity Changer

I am writing a code for going to the next page after clicking a button so I have written the code that I have mentioned below i just want to confirn that it is correct as i cant check it now, I know this is silly but I need help
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class DetailsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
btnNextScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent nextScreen = new Intent(getApplicationContext(), JewelInfo.class);
// TODO Auto-generated method stub
startActivity(nextScreen);
}
});
}
}
Assuming that your JewelInfo extends from an Activty it looks fine.
I think you should use Intent nextScreen = new Intent(DetailsActivity.this, JewelInfo.class); instead of getApplicationContext()
The correctness of code is never checked just by looking at a bit of code. Syntactically, it seems to not contain an error. Semantically, how should we know? You need to define what you want to do, how you want it to look, etc. Basically, what are the requirements?
We can then validate the code against the requirements but not assess the correctness as that is only discerned by executing the code and evaluating the result of the execution on the device it is executed against the requirements.
use DetailsActivity.this instead of getApplicationContext(), and declare your JewelInfo activity on the manifest file : <activity android:name=".JewelInfo" />.Check this tutorial about how to switch between activities and pass data between them

Method Undefined

Edit: My original question is below the line. I decided to go with a much simpler approach to setting up a button and assigning a click function. I found it at the following link. He does a good job of explaining the difference between the 2 approaches...
Android User Interface Design: Basic Buttons
I realize this is a popular question, but in all of the examples I've looked at the problem seems to be a simple detail that's been overlooked, and the detail is never the same. I'm sure this is basic. I'm just starting out with programming for Android and this is a modification of existing code.
The app has one button on a blank page, and I want the button click to send an int to my Arduino via the Amarino API. Here is my MainActivity code
package com.example.buttontest1;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import at.abraxas.amarino.Amarino;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
public class MainActivity extends Activity{
private Button button;
private static final String DEVICE_ADDRESS = "00:06:66:4B:E4:23";
public Context foo1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Amarino.connect(this, DEVICE_ADDRESS);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
//Select a specific button to bundle it with the action you want
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'j', 1);
}
});
}
protected void onStop() {
super.onStop();
// stop Amarino's background service, we don't need it any more
Amarino.disconnect(this, DEVICE_ADDRESS);
}
}
The error I see is this, referring to line 38:
The method sendDataToArduino(Context, String, char, int) in the type
Amarino is not applicable for the arguments (new
View.OnClickListener(){}, String, char, int)
So there's a problem with the context and the method?
Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'j', 1);
this here refers toView.OnClickListener's current instance. The compilation error basically says, sendDataToArduino() expects the first argument as Context but you are passing a OnClickListener
sendDataToArduino expects its first argument to be of type Context. You are passing it a View.onClickListener. Instead of passing this as the first argument, try setting up a context as mentioned here and pass that as the first argument.
Try adding the following in your onCreate method after the super call:
MainActivity.context = getApplicationContext();
Also add the following method after onCreate:
public static Context getAppContext() {
return MainActivity.context;
}
Now call the method with:
Amarino.sendDataToArduino(getApppContext(), DEVICE_ADDRESS, 'j', 1);
Teh api expects the object of Context but you pass this to:
Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'j', 1);
this is not an object of Context instead it is OnClickListener object.

Android Development: "Cannot be resolved"

Hoping to get into android app development so I'm doing some basic tutorials just now.
Just trying to get comfortable with the basics at the moment, one of which is using the Typeface class.
package org.me.myandroidstuff;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class HelloWorldActivity extends Activity implements OnClickListener
{
private View mainView;
private TextView tbox1;
private Button exitButton;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainView=(View)findViewById(R.id.mainView);
mainView.setBackgroundColor(getResources().getColor(R.color.silver));
tbox1 = (TextView)findViewById(R.id.textBox1);
tbox1.setTypeface(Typeface.MONOSPACE);
}
}
The line
tbox1 = (TextView)findViewById(R.id.textBox1);
Has a red cross beside it (I'm using eclipse) with the error
tbox1 cannot be resolved
Its been a while since i have used java, but as i aware the following code
create a new TextView object called tbox1
Assigns the tbox1 object the id specified in the xml for the TextView tag in an external main.xml
Then tbox1 executes the setTypeFace() method on itself?
Obviously I'm going wrong somewhere, any ideas? Something really simple no doubt...
You can't inform us about one error and neglect the others. Look at your code.
Besides what user370305 said, you have other problems. Namely, your Activity, according to the contract, implements OnClickListener but does not override the necessary onClick(View v) method. You must add it for the contract to be met.
So your code should look like:
package org.me.myandroidstuff;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class HelloWorldActivity extends Activity implements OnClickListener {
private View mainView;
private TextView tbox1;
private Button exitButton;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainView=(View)findViewById(R.id.mainView);
mainView.setBackgroundColor(getResources().getColor(R.color.silver));
tbox1 = (TextView)findViewById(R.id.textBox1);
tbox1.setTypeface(Typeface.MONOSPACE);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Remember, you can't talk about errors until you fix every other that might cause other errors to be falsely reported.
First try to set setContentView(R.layout.yourlayoutfilename); in onCreate().
1.) Delete line super.onCreate(savedInstanceState);
2.) Retype super.onCreate(savedInstanceState);
3.) Clean the Project
4.) Build the Project

Categories