Android AsyncTask error, attempting to assign weaker access privileges. - java

I'm stuck with this problem, I continue to receive the following error:
'processFinish(Boolean)' in 'Anonymous class derived from
MainActivity.AsyncGETRequestProcess.AsyncResponse' clashes with
'processFinish(Boolean)' in
MainActivity.AsyncGETRequestProcess.AsyncResponse'; attempting to
assign weaker access privileges ('packageLocal'); was public.
What I'm trying to accomplish is, run AsyncTask from inside MainActivity after I receive the data from an Intent.
Here is the onResume code where I receive the error, and where the Asynctask gets called:
#Override
protected void onResume(){
super.onResume();
IntentFilter intentFilter = new IntentFilter("cb.MainActivity.WebServiceReceiver.ReturnedReceivedDataFilePath");
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//Get message from WebServiceReceiver Intent
String ReceivedDataFilePath = intent.getStringExtra("WebServiceReceiver_ReturnedReceivedDataFilePath");
new AsyncGETRequestProcess(new AsyncGETRequestProcess.AsyncResponse(){
//THE ABOVE ERROR HAPPENS HERE... processFinish
#Override
void processFinish(boolean success){
if(success){
MainActivity.getInstance().txtStatus.setText("Loading screen...");
Intent RouteScreenActivityIntent = new Intent(MainActivity.getInstance(), RouteScreenActivity.class);
MainActivity.getInstance().startActivity(RouteScreenActivityIntent);
}else{
//ERROR MSG
}
//Stop the spinner in MainActivity
MainActivity.getInstance().MainActSpinner.setVisibility(View.GONE);
MainActivity.getInstance().txtStatus.setText("");
}
}).execute(ReceivedDataFilePath).get();
}
};
Here is my AsyncGETRequestProcess Class which extends AsyncTask:
//THE FOLLOWING CLASS RESIDES WITHIN MainActivity Class...
public static class AsyncGETRequestProcess extends AsyncTask<String, Void, Void> {
public interface AsyncResponse {
void processFinish(boolean success);
}
public AsyncResponse delegate = null;
public AsyncGETRequestProcess(AsyncResponse delegate){
this.delegate = delegate;
}
#Override
protected Void doInBackground(String... strings) {
//Read the temp file location and create model of 'GET' Data Received.
GetRequestRoot getRequestRootModel = Core.Helper.GetRootObjFromGetRequestTempFile(strings[0]);
Boolean responseResult = ServerResponse.ProcessGetRequestResultModel(getRequestRootModel);
if(responseResult)
{
Core.Data.ObjModel = Core.Helper.GetInputFileAsObject();
if(Core.Data.ObjModel == null){
delegate.processFinish(false);
}
else{
delegate.processFinish(true);
}
}else{
delegate.processFinish(false);
}
return null;
}
#Override
protected void onPreExecute(){
}
#Override
protected void onPostExecute(Void result){
}
}
I'm also getting the following compile time error..
Error:(108, 28) error: processFinish(boolean) in cannot implement processFinish(boolean) in
AsyncResponse attempting to assign weaker access privileges; was
public
How can I fix this? I don't understand why its happening.

You should make your method processFinish public:
new AsyncGETRequestProcess(new AsyncGETRequestProcess.AsyncResponse(){
//THE ABOVE ERROR HAPPENS HERE... processFinish
#Override
public void processFinish(boolean success){
^^^^^^
all interface methods are by default public

Related

set void to AsyncTask and execute it after doInBackground finishes

I'm trying to use AsyncTask to make HTTP GET requests in Android Studio. This AsyncTask class is used across many activities of the app. I want to assign a void function to the AsyncTask which will execute after the doInBackground has fetched the HTTP GET request. The HTTP returned response should be inserted into the void's parameter. I have researched a lot of times but cannot find a simple working solution. Need Help. Thanks.
Main.java containing the AsyncTask class:
package com.example.NAME;
import android.os.AsyncTask;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static class GetData extends AsyncTask<String, Void, Boolean> {
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... params) {
Boolean _return = false;
try {
String data = params[0];
// DO THE HTTP GET REQUEST AND EVALUATE THE BOOLEAN RETURN VALUE
_return = ???;
} catch (Exception e) {
e.printStackTrace();
}
return _return;
}
#Override
protected void onPostExecute(Boolean _return) {
super.onPostExecute(_return);
}
}
}
Activity1.java using the AsyncTask class:
package com.example.NAME;
import android.app.Activity;
import android.os.Bundle;
public class Activity1 extends Activity {
private final Main main = new Main();
#Override
protected synchronized void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity1);
}
public synchronized void GET_Request(View v) {
Main.GetData t = new Main.GetData();
t.execute("STRING");
/*
How to attach the `after_GET_Request` void that should be executed
after the HTTP GET Request happens and should receive the returned
Boolean value.
*/
}
public synchronized void after_GET_Request(Boolean b) {
// Use the Boolean data received from AsyncTask.
}
}
Add a result callback to your AsyncTask and process the outcome:
public static class GetData extends AsyncTask<String, Void, Boolean> {
private ResultCallback mCallback;
public GetData(ResultCallback callback) {
mCallback = callback;
}
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... params) {
Boolean _return = false;
try {
String data = params[0];
// DO THE HTTP GET REQUEST AND EVALUATE THE BOOLEAN RETURN VALUE
_return = ???;
} catch (Exception e) {
e.printStackTrace();
}
return _return;
}
#Override
protected void onPostExecute(Boolean _return) {
super.onPostExecute(_return);
mCallback.onResultReady(_return);
}
public interface ResultCallback {
void onResultReady(Boolean result);
}
}
And then in your activity call your function:
Main.GetData t = new Main.GetData(new ResultCallback() {
#Override
public void onResultReady(Boolean result) {
after_GET_Request(result);
}
});
t.execute("STRING");

A better way of getting a result from an Async call while passing a function [duplicate]

I have this two classes. My main Activity and the one that extends the AsyncTask, Now in my main Activity I need to get the result from the OnPostExecute() in the AsyncTask. How can I pass or get the result to my main Activity?
Here is the sample codes.
My main Activity.
public class MainActivity extends Activity{
AasyncTask asyncTask = new AasyncTask();
#Override
public void onCreate(Bundle aBundle) {
super.onCreate(aBundle);
//Calling the AsyncTask class to start to execute.
asyncTask.execute(a.targetServer);
//Creating a TextView.
TextView displayUI = asyncTask.dataDisplay;
displayUI = new TextView(this);
this.setContentView(tTextView);
}
}
This is the AsyncTask class
public class AasyncTask extends AsyncTask<String, Void, String> {
TextView dataDisplay; //store the data
String soapAction = "http://sample.com"; //SOAPAction header line.
String targetServer = "https://sampletargeturl.com"; //Target Server.
//SOAP Request.
String soapRequest = "<sample XML request>";
#Override
protected String doInBackground(String... string) {
String responseStorage = null; //storage of the response
try {
//Uses URL and HttpURLConnection for server connection.
URL targetURL = new URL(targetServer);
HttpURLConnection httpCon = (HttpURLConnection) targetURL.openConnection();
httpCon.setDoOutput(true);
httpCon.setDoInput(true);
httpCon.setUseCaches(false);
httpCon.setChunkedStreamingMode(0);
//properties of SOAPAction header
httpCon.addRequestProperty("SOAPAction", soapAction);
httpCon.addRequestProperty("Content-Type", "text/xml; charset=utf-8");
httpCon.addRequestProperty("Content-Length", "" + soapRequest.length());
httpCon.setRequestMethod(HttpPost.METHOD_NAME);
//sending request to the server.
OutputStream outputStream = httpCon.getOutputStream();
Writer writer = new OutputStreamWriter(outputStream);
writer.write(soapRequest);
writer.flush();
writer.close();
//getting the response from the server
InputStream inputStream = httpCon.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(50);
int intResponse = httpCon.getResponseCode();
while ((intResponse = bufferedReader.read()) != -1) {
byteArrayBuffer.append(intResponse);
}
responseStorage = new String(byteArrayBuffer.toByteArray());
} catch (Exception aException) {
responseStorage = aException.getMessage();
}
return responseStorage;
}
protected void onPostExecute(String result) {
aTextView.setText(result);
}
}
Easy:
Create interface class, where String output is optional, or can be whatever variables you want to return.
public interface AsyncResponse {
void processFinish(String output);
}
Go to your AsyncTask class, and declare interface AsyncResponse as a field :
public class MyAsyncTask extends AsyncTask<Void, Void, String> {
public AsyncResponse delegate = null;
#Override
protected void onPostExecute(String result) {
delegate.processFinish(result);
}
}
In your main Activity you need to implements interface AsyncResponse.
public class MainActivity implements AsyncResponse{
MyAsyncTask asyncTask =new MyAsyncTask();
#Override
public void onCreate(Bundle savedInstanceState) {
//this to set delegate/listener back to this class
asyncTask.delegate = this;
//execute the async task
asyncTask.execute();
}
//this override the implemented method from asyncTask
#Override
void processFinish(String output){
//Here you will receive the result fired from async class
//of onPostExecute(result) method.
}
}
UPDATE
I didn't know this is such a favourite to many of you. So here's the simple and convenience way to use interface.
still using same interface. FYI, you may combine this into AsyncTask class.
in AsyncTask class :
public class MyAsyncTask extends AsyncTask<Void, Void, String> {
// you may separate this or combined to caller class.
public interface AsyncResponse {
void processFinish(String output);
}
public AsyncResponse delegate = null;
public MyAsyncTask(AsyncResponse delegate){
this.delegate = delegate;
}
#Override
protected void onPostExecute(String result) {
delegate.processFinish(result);
}
}
do this in your Activity class
public class MainActivity extends Activity {
MyAsyncTask asyncTask = new MyAsyncTask(new AsyncResponse(){
#Override
void processFinish(String output){
//Here you will receive the result fired from async class
//of onPostExecute(result) method.
}
}).execute();
}
Or, implementing the interface on the Activity again
public class MainActivity extends Activity
implements AsyncResponse{
#Override
public void onCreate(Bundle savedInstanceState) {
//execute the async task
new MyAsyncTask(this).execute();
}
//this override the implemented method from AsyncResponse
#Override
void processFinish(String output){
//Here you will receive the result fired from async class
//of onPostExecute(result) method.
}
}
As you can see 2 solutions above, the first and third one, it needs to create method processFinish, the other one, the method is inside the caller parameter. The third is more neat because there is no nested anonymous class.
Tip: Change String output, String response, and String result to different matching types in order to get different objects.
There are a few options:
Nest the AsyncTask class within your Activity class. Assuming you don't use the same task in multiple activities, this is the easiest way. All your code stays the same, you just move the existing task class to be a nested class inside your activity's class.
public class MyActivity extends Activity {
// existing Activity code
...
private class MyAsyncTask extends AsyncTask<String, Void, String> {
// existing AsyncTask code
...
}
}
Create a custom constructor for your AsyncTask that takes a reference to your Activity. You would instantiate the task with something like new MyAsyncTask(this).execute(param1, param2).
public class MyAsyncTask extends AsyncTask<String, Void, String> {
private Activity activity;
public MyAsyncTask(Activity activity) {
this.activity = activity;
}
// existing AsyncTask code
...
}
You can try this code in your Main class.
That worked for me, but i have implemented methods in other way
try {
String receivedData = new AsyncTask().execute("http://yourdomain.com/yourscript.php").get();
}
catch (ExecutionException | InterruptedException ei) {
ei.printStackTrace();
}
I felt the below approach is very easy.
I have declared an interface for callback
public interface AsyncResponse {
void processFinish(Object output);
}
Then created asynchronous Task for responding all type of parallel requests
public class MyAsyncTask extends AsyncTask<Object, Object, Object> {
public AsyncResponse delegate = null;//Call back interface
public MyAsyncTask(AsyncResponse asyncResponse) {
delegate = asyncResponse;//Assigning call back interfacethrough constructor
}
#Override
protected Object doInBackground(Object... params) {
//My Background tasks are written here
return {resutl Object}
}
#Override
protected void onPostExecute(Object result) {
delegate.processFinish(result);
}
}
Then Called the asynchronous task when clicking a button in activity Class.
public class MainActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
Button mbtnPress = (Button) findViewById(R.id.btnPress);
mbtnPress.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MyAsyncTask asyncTask =new MyAsyncTask(new AsyncResponse() {
#Override
public void processFinish(Object output) {
Log.d("Response From Asynchronous task:", (String) output);
mbtnPress.setText((String) output);
}
});
asyncTask.execute(new Object[] { "Your request to aynchronous task class is giving here.." });
}
});
}
}
Thanks
This answer might be late but I would like to mention few things when your Activity dependent on AsyncTask. That would help you in prevent crashes and memory management. As already mentioned in above answers go with interface, we also say them callbacks. They will work as an informer, but never ever send strong reference of Activity or interface always use weak reference in those cases.
Please refer to below screenshot to findout how that can cause issues.
As you can see if we started AsyncTask with a strong reference then there is no guarantee that our Activity/Fragment will be alive till we get data, so it would be better to use WeakReference in those cases and that will also help in memory management as we will never hold the strong reference of our Activity then it will be eligible for garbage collection after its distortion.
Check below code snippet to find out how to use awesome WeakReference -
MyTaskInformer.java Interface which will work as an informer.
public interface MyTaskInformer {
void onTaskDone(String output);
}
MySmallAsyncTask.java AsyncTask to do long running task, which will use WeakReference.
public class MySmallAsyncTask extends AsyncTask<String, Void, String> {
// ***** Hold weak reference *****
private WeakReference<MyTaskInformer> mCallBack;
public MySmallAsyncTask(MyTaskInformer callback) {
this.mCallBack = new WeakReference<>(callback);
}
#Override
protected String doInBackground(String... params) {
// Here do whatever your task is like reading/writing file
// or read data from your server or any other heavy task
// Let us suppose here you get response, just return it
final String output = "Any out, mine is just demo output";
// Return it from here to post execute
return output;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
// Here you can't guarantee that Activity/Fragment is alive who started this AsyncTask
// Make sure your caller is active
final MyTaskInformer callBack = mCallBack.get();
if(callBack != null) {
callBack.onTaskDone(s);
}
}
}
MainActivity.java This class is used to start my AsyncTask implement interface on this class and override this mandatory method.
public class MainActivity extends Activity implements MyTaskInformer {
private TextView mMyTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMyTextView = (TextView) findViewById(R.id.tv_text_view);
// Start your AsyncTask and pass reference of MyTaskInformer in constructor
new MySmallAsyncTask(this).execute();
}
#Override
public void onTaskDone(String output) {
// Here you will receive output only if your Activity is alive.
// no need to add checks like if(!isFinishing())
mMyTextView.setText(output);
}
}
You can do it in a few lines, just override onPostExecute when you call your AsyncTask. Here is an example for you:
new AasyncTask()
{
#Override public void onPostExecute(String result)
{
// do whatever you want with result
}
}.execute(a.targetServer);
I hope it helped you, happy codding :)
in your Oncreate():
`
myTask.execute("url");
String result = "";
try {
result = myTask.get().toString();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}`
Why do people make it so hard.
This should be sufficient.
Do not implement the onPostExecute on the async task, rather implement it on the Activity:
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState) {
//execute the async task
MyAsyncTask task = new MyAsyncTask(){
protected void onPostExecute(String result) {
//Do your thing
}
}
task.execute("Param");
}
}
You can call the get() method of AsyncTask (or the overloaded get(long, TimeUnit)). This method will block until the AsyncTask has completed its work, at which point it will return you the Result.
It would be wise to be doing other work between the creation/start of your async task and calling the get method, otherwise you aren't utilizing the async task very efficiently.
You can write your own listener. It's same as HelmiB's answer but looks more natural:
Create listener interface:
public interface myAsyncTaskCompletedListener {
void onMyAsynTaskCompleted(int responseCode, String result);
}
Then write your asynchronous task:
public class myAsyncTask extends AsyncTask<String, Void, String> {
private myAsyncTaskCompletedListener listener;
private int responseCode = 0;
public myAsyncTask() {
}
public myAsyncTask(myAsyncTaskCompletedListener listener, int responseCode) {
this.listener = listener;
this.responseCode = responseCode;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(String... params) {
String result;
String param = (params.length == 0) ? null : params[0];
if (param != null) {
// Do some background jobs, like httprequest...
return result;
}
return null;
}
#Override
protected void onPostExecute(String finalResult) {
super.onPostExecute(finalResult);
if (!isCancelled()) {
if (listener != null) {
listener.onMyAsynTaskCompleted(responseCode, finalResult);
}
}
}
}
Finally implement listener in activity:
public class MainActivity extends AppCompatActivity implements myAsyncTaskCompletedListener {
#Override
public void onMyAsynTaskCompleted(int responseCode, String result) {
switch (responseCode) {
case TASK_CODE_ONE:
// Do something for CODE_ONE
break;
case TASK_CODE_TWO:
// Do something for CODE_TWO
break;
default:
// Show some error code
}
}
And this is how you can call asyncTask:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Some other codes...
new myAsyncTask(this,TASK_CODE_ONE).execute("Data for background job");
// And some another codes...
}
Hi you can make something like this:
Create class which implements AsyncTask
// TASK
public class SomeClass extends AsyncTask<Void, Void, String>>
{
private OnTaskExecutionFinished _task_finished_event;
public interface OnTaskExecutionFinished
{
public void OnTaskFihishedEvent(String Reslut);
}
public void setOnTaskFinishedEvent(OnTaskExecutionFinished _event)
{
if(_event != null)
{
this._task_finished_event = _event;
}
}
#Override
protected void onPreExecute()
{
super.onPreExecute();
}
#Override
protected String doInBackground(Void... params)
{
// do your background task here ...
return "Done!";
}
#Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
if(this._task_finished_event != null)
{
this._task_finished_event.OnTaskFihishedEvent(result);
}
else
{
Log.d("SomeClass", "task_finished even is null");
}
}
}
Add in Main Activity
// MAIN ACTIVITY
public class MyActivity extends ListActivity
{
...
SomeClass _some_class = new SomeClass();
_someclass.setOnTaskFinishedEvent(new _some_class.OnTaskExecutionFinished()
{
#Override
public void OnTaskFihishedEvent(String result)
{
Toast.makeText(getApplicationContext(),
"Phony thread finished: " + result,
Toast.LENGTH_SHORT).show();
}
});
_some_class.execute();
...
}
Create a static member in your Activity class. Then assign the value during the onPostExecute
For example, if the result of your AsyncTask is a String, create a public static string in your Activity
public static String dataFromAsyncTask;
Then, in the onPostExecute of the AsyncTask, simply make a static call to your main class and set the value.
MainActivity.dataFromAsyncTask = "result blah";
I make it work by using threading and handler/message.
Steps as follow:
Declare a progress Dialog
ProgressDialog loadingdialog;
Create a function to close dialog when operation is finished.
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
loadingdialog.dismiss();
}
};
Code your Execution details:
public void startUpload(String filepath) {
loadingdialog = ProgressDialog.show(MainActivity.this, "Uploading", "Uploading Please Wait", true);
final String _path = filepath;
new Thread() {
public void run() {
try {
UploadFile(_path, getHostName(), getPortNo());
handler.sendEmptyMessage(0);
} catch (Exception e) {
Log.e("threadmessage", e.getMessage());
}
}
}.start();
}
You need to use "protocols" to delegate or provide data to the AsynTask.
Delegates and Data Sources
A delegate is an object that acts on behalf of, or in coordination with, another object when that object encounters an event in a program. (Apple definition)
protocols are interfaces that define some methods to delegate some behaviors.
Here is a complete example!!!
try this:
public class SomAsyncTask extends AsyncTask<String, Integer, JSONObject> {
private CallBack callBack;
public interface CallBack {
void async( JSONObject jsonResult );
void sync( JSONObject jsonResult );
void progress( Integer... status );
void cancel();
}
public SomAsyncTask(CallBack callBack) {
this.callBack = callBack;
}
#Override
protected JSONObject doInBackground(String... strings) {
JSONObject dataJson = null;
//TODO query, get some dataJson
if(this.callBack != null)
this.callBack.async( dataJson );// asynchronize with MAIN LOOP THREAD
return dataJson;
}
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
if(this.callBack != null)
this.callBack.progress(values);// synchronize with MAIN LOOP THREAD
}
#Override
protected void onPostExecute(JSONObject jsonObject) {
super.onPostExecute(jsonObject);
if(this.callBack != null)
this.callBack.sync(jsonObject);// synchronize with MAIN LOOP THREAD
}
#Override
protected void onCancelled() {
super.onCancelled();
if(this.callBack != null)
this.callBack.cancel();
}
}
And usage example:
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Context _localContext = getContext();
SomeAsyncTask.CallBack someCallBack = new SomeAsyncTask.CallBack() {
#Override
public void async(JSONObject jsonResult) {//async thread
//some async process, e.g. send data to server...
}
#Override
public void sync(JSONObject jsonResult) {//sync thread
//get result...
//get some resource of Activity variable...
Resources resources = _localContext.getResources();
}
#Override
public void progress(Integer... status) {//sync thread
//e.g. change status progress bar...
}
#Override
public void cancel() {
}
};
new SomeAsyncTask( someCallBack )
.execute("someParams0", "someParams1", "someParams2");
}
Probably going overboard a bit but i provided call backs for both the execution code and the results. obviously for thread safety you want to be careful what you access in your execution callback.
The AsyncTask implementation:
public class AsyncDbCall<ExecuteType,ResultType> extends AsyncTask<ExecuteType, Void,
ResultType>
{
public interface ExecuteCallback<E, R>
{
public R execute(E executeInput);
}
public interface PostExecuteCallback<R>
{
public void finish(R result);
}
private PostExecuteCallback<ResultType> _resultCallback = null;
private ExecuteCallback<ExecuteType,ResultType> _executeCallback = null;
AsyncDbCall(ExecuteCallback<ExecuteType,ResultType> executeCallback, PostExecuteCallback<ResultType> postExecuteCallback)
{
_resultCallback = postExecuteCallback;
_executeCallback = executeCallback;
}
AsyncDbCall(ExecuteCallback<ExecuteType,ResultType> executeCallback)
{
_executeCallback = executeCallback;
}
#Override
protected ResultType doInBackground(final ExecuteType... params)
{
return _executeCallback.execute(params[0]);
}
#Override
protected void onPostExecute(ResultType result)
{
if(_resultCallback != null)
_resultCallback.finish(result);
}
}
A callback:
AsyncDbCall.ExecuteCallback<Device, Device> updateDeviceCallback = new
AsyncDbCall.ExecuteCallback<Device, Device>()
{
#Override
public Device execute(Device device)
{
deviceDao.updateDevice(device);
return device;
}
};
And finally execution of the async task:
new AsyncDbCall<>(addDeviceCallback, resultCallback).execute(device);
Hope you been through this , if not please read.
https://developer.android.com/reference/android/os/AsyncTask
Depending on the nature of result data, you should choose best possible option you can think of.
It is a great choice to use an Interface
some other options would be..
If the AsyncTask class is defined inside the very class you want to
use the result in.Use a static global variable or get() , use it from
outer class (volatile variable if necessary). but should be aware of the AsyncTask progress or should at least make sure that it have finished the task and result is
available through global variable / get() method. you may use
polling, onProgressUpdate(Progress...), synchronization or interfaces (Which ever suits best for you)
If the Result is compatible to be a sharedPreference entry or it is okay to be saved as a file in the memory you could save it even from
the background task itself and could use the onPostExecute() method
to get notified when the result is available in the memory.
If the string is small enough, and is to be used with start of an
activity. it is possible to use intents (putExtra()) within
onPostExecute() , but remember that static contexts aren't that safe
to deal with.
If possible, you can call a static method from the
onPostExecute() method, with the result being your parameter

Populate TextView on main activity form another class

I have a problem updating a TextView on my MainActivity form a class which is Async I have seen that I need to set the Context of the second class to that of the MainActivity but I don't know how to achieve this in this scenario. My app looks like this.
class RestOperation extends AsyncTask<String, Void, Void> {
#Override
protected Void doInBackground(String... params) {
//Java.net Http transaction happens here
}
#Override
protected void onPostExecute(Void result) {
//I wish to set the value of the TextView with the result here!
}
It'll be good if you will create interface which will be call at onPostExecute and at Main activity implement that interface and get your value and set value with textview.
2.newrestOperation(YourActivity.this,textviewobject).execute("yours","params","here");
So, your AsyncTask class should look like this :
Textview mtxt;
class RestOperation extends AsyncTask<String, Void, Void> {
Context _activityContext;
public RestOperation(Context activityContext,Textview txt){
this._activityContext = activityContext;
this.mtxt = txt;
}
#Override
protected String doInBackground(String... params) {
//Java.net Http transaction happens here
}
#Override
protected void onPostExecute(String result) {enter code here
mtxt .setText(yourText);
}
}
But #1 is better way.
You can do it like this :
#1 : Make a constructor for your RestOperation class to get the Context from your Activity like this :
public RestOperation(MainActivity activityContext){
this._activityContext = activityContext;
}
#2 : Pass the Activity context while initializing the AsyncTask class like this :
new RestOperation(MainActivity.this).execute("yours","params","here");
So, your AsyncTask class should look like this :
class RestOperation extends AsyncTask<String, Void, String> {
MainActivity _activityContext;
public RestOperation(MainActivity activityContext){
this._activityContext = activityContext;
}
#Override
protected String doInBackground(String... params) {
//Java.net Http transaction happens here
}
#Override
protected void onPostExecute(String result) {
((TextView)_activityContext.findViewById(R.id.yourTextViewId)).setText(yourText);
}
}
If this is in another class, you should get a reference to MainActivity through the constructor (note that I use WeakReference here so that when the activity is destroyed the onPostExecute won't be executed (because it does not need to) and the AsyncTask won't leak memory)
//first you have to change the param (the last one) to String so your doInBackground would return String
class RestOperation extends AsyncTask<String, Void, String> {
private WeakReference<MainActivity> weakRef;
public RestOperation(MainActivity activity) {
weakRef = new WeakReference<MainActivity>(activity);
}
#Override
protected String doInBackground(String... params) {
//Java.net Http transaction happens here
//You need to return some value here to set to the text view later
return "Something";
}
#Override
protected void onPostExecute(String result) {
MainActivity activity = weakRef.get();
if (activity != null) {
//activity is still alive, updates the text view
activity.updateText(result);
}
}
In your MainActivity:
new RestOperation(this).execute("Your input param");
also make another method in MainActivity:
public void updateText(String result) {
textView.setText(result);
}
Simply use:
#Override
protected void onPostExecute(String result)
{
textView.setText(result);
}
Change your code to:
class RestOperation extends AsyncTask<String, Void, Void> {
Context context;
add public RestOperation (Contex cntx) {
this.context = cntx;
}
#Override
protected Void doInBackground(String... params) { //Java.net Http transaction happens here }
#Override
protected void onPostExecute(Void result) {
//I wish to set the value of the TextView with the result here!
}
And when you cat this task
new ResrOperation(getApplicationContext()).excute();
Then you can access to items from main activity
Another choise
You can Overide onPostExecute method in your main activiry and then you will have acces to activity elemets too (In this case no context nedded)
new ResrOperation(){
#Overide
void onPostExecute(Long result) {
// access elemets from activity
}
}.excute();
First write an interface in the Async class
CompleteListener completeListener;
public void setListener(CompleteListener cmpltListnr) {
this.completeListener = cmpltListnr;
}
public interface CompleteListener {
void OnCompleteListener(String result);
}
and onPostExecute
#Override
protected void onPostExecute(Void result) {
if ( completeListener != null ) {
completeListener.OnCompleteListener(""+result);
}
}
and implement the listener in the Activity you want. and override the function
and assign this to the interface by
yourAsynClassObject.setListener(this);
#Override
public void OnCompleteListener(String result) {
textview.setText("" + result);
}

Communication between Activity and GCMListenerService Android

I am working on an android application with push notification feature using GCM. I have created a class called PushNotificationService which extends GCMListenerService. Inside the onMessageReceived(String from, Bundle data) I am able to get the message in the push notification.
Now, I want to access a method inside my MainActivity class whenever a particular message is received in the push.
Below is my code :-
PushNotificationService.java
public class PushNotificationService extends GcmListenerService {
#Override
public void onMessageReceived(String from, Bundle data) {
// TODO Auto-generated method stub
super.onMessageReceived(from, data);
String message = data.getString("message");
if(message.equalsIgnoreCase("Begin Task"))
{
//call method from MainActivity.class
}
}
}
MainActivty.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void beginTask()
{
Log.d("GCM","Message Received from Server");
finish();
}
}
I want the beginTask() method to execute whenever the message "Begin Task" is received.
I know one approach is via Service->Interface->Activity architecture but I am not able to use this as I never create an object of PushNotificationService.
Please help.
UPDATE :-
I am now using Otto Library and below is my code.
Added new MyBus.java
public class MyBus extends Bus {
private static Bus bus;
//isRegistered is used to track the current registration status
private static boolean isRegistered;
private Handler handler = new Handler(Looper.getMainLooper());
public MyBus() {
if (bus == null) {
//ANY will allow event bus to run even with services
//and broadcast receivers
bus = new Bus(ThreadEnforcer.ANY);
}
}
#Override
public void register(Object obj) {
//The bus is registered when an activity starts
bus.register(obj);
isRegistered = true;
}
#Override
public void unregister(Object obj) {
//The bus is unregistered when an activity goes to background
bus.unregister(obj);
isRegistered = false;
}
#Override
public void post(final Object event) {
if (Looper.myLooper() == Looper.getMainLooper()) {
//post the event in main thread or background thread
bus.post(event);
} else {
handler.post(new Runnable() {
#Override
public void run() {
bus.post(event);
}
});
}
}
public boolean isRegistered(){
return isRegistered;
}
}
PushNotificationService.java
public class PushNotificationService extends GcmListenerService {
#Override
public void onMessageReceived(String from, Bundle data) {
// TODO Auto-generated method stub
super.onMessageReceived(from, data);
MyBus myBus = new MyBus();
myBus.register(myBus);
String message = data.getString("message");
if(message.equalsIgnoreCase("Begin Task"))
{
myBus.post(message);
}
}
}
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Subscribe
public void beginTask()
{
Log.d("GCM","Message Received from Server");
}
}
The problem is still not solved. The beginTask() inside MainActivity.java is still not getting called.
Use eventBus libraries to facilitate this process...
I use Otto for this process
http://square.github.io/otto/
Here is an another eventBus library https://greenrobot.github.io/EventBus/
Steps:
1.Create an event from the service
2.Add a listener in the activity
3.If the activity is running the method will be executed
**EDIT 1 : **
I have abstracted the otto bus like this.
package com.mypackage.eventBus;
import android.os.Handler;
import android.os.Looper;
import com.squareup.otto.Bus;
import com.squareup.otto.ThreadEnforcer;
/**
* Created by gowtham on 10/6/15.
*/
public class MyBus extends Bus {
private static Bus bus;
//isRegistered is used to track the current registration status
private static boolean isRegistered;
private Handler handler = new Handler(Looper.getMainLooper());
public MyBus() {
if (bus == null) {
//ANY will allow event bus to run even with services
//and broadcast receivers
bus = new Bus(ThreadEnforcer.ANY);
}
}
#Override
public void register(Object obj) {
//The bus is registered when an activity starts
bus.register(obj);
isRegistered = true;
}
#Override
public void unregister(Object obj) {
//The bus is unregistered when an activity goes to background
bus.unregister(obj);
isRegistered = false;
}
#Override
public void post(final Object event) {
if (Looper.myLooper() == Looper.getMainLooper()) {
//post the event in main thread or background thread
bus.post(event);
} else {
handler.post(new Runnable() {
#Override
public void run() {
bus.post(event);
}
});
}
}
public boolean isRegistered(){
return isRegistered;
}
}
create an instance of the above object and try posting event
EDIT 2 for Jcarlo's comment
Follow these steps to find the state of the activity.
In your activity's onResume call MyBus.getInstance().register(this).
In your activity's onPause call MyBus.getInstance().unregister(this).
In your GCM IntentService before posting the message
if(MyBus.getInstance().isRegistered()){
//app is alive
//post data
}else{
//show notification
}
Hope this helps
You can use LocalBroadcastManager. Create a LocalBroadcastManager object mBroadcaster = LocalBroadcastManager.getInstance(this); on onCreate of your GCMListener and send broadcast with
#Override
public void onCreate() {
super.onCreate();
mBroadcaster = LocalBroadcastManager.getInstance(this);
}
#Override
public void onMessageReceived(String from, Bundle data) {
super.onMessageReceived(from, data);
String message = data.getString("message");
if(message.equalsIgnoreCase("Begin Task")) {
Intent i = new Intent();
i.setAction("yourPackageName");
i.putExtra("DATA", yourData);
mBroadcaster.send(i);
}
}
Then you can receive message in MainActivity using a BroadcastReceiver.
BroadCastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
beginTask();
}
};
Also you need to register and unregister the receiver in onStart and onStop of your activity
#Override
protected void onStart() {
super.onStart();
IntentFilter filter = new IntentFilter();
filter.addAction("yourPackageName);
LocalBroadcastManager.getInstance(this).registerReceiver((mBroadcastReceiver), filter);
}
#Override
protected void onStop() {
super.onStop();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver);
}

Returning from Asynch Task to MainActivity

I read a lot about it and tried many things, without having succes. But it doesn't seem hard at all, so I guess I am missing a little thing.
I got 2 classes, a MainActivity and an asynch task class.
the doInBackgroundtask is working perfectly.
But when it is done, I want to program to continue at a certain point in my MainActivity
protected Integer doInBackground(Void... params) {
try {
Log.d("control", "ZipHelper.unzip() - File: " + _archive);
ZipFile zipfile = new ZipFile(_archive);
for (Enumeration<? extends ZipEntry> e = zipfile.entries(); e
.hasMoreElements();) {
ZipEntry entry = (ZipEntry) e.nextElement();
unzipEntry(zipfile, entry, _outputDir);
}
} catch (Exception e) {
Log.d("control", "ZipHelper.unzip() - Error extracting file "
+ _archive + ": " + e);
setZipError(true);
}
return null;
}
protected void onPostExecute(Integer... result) {
//Here something like MainActivity.showPicture();
}
I know I must do something with onPostExecute, but I don't know what exactly.
So let's say, I want to show a Toast in my MainActivity after asynch-task is done?
Use a Listener interface.
Example :
Listener Interface
public interface AsyncTaskListener
{
public void onTaskComplete();
}
ZipHelper Class
public class ZipHelper extends AsyncTask<Void, Void, Integer>
{
private String filename;
private AsyncTaskListener listener;
private File file;
public ZipHelper(String filename, File file, AsyncTaskListener listener)
{
this.filename = filename;
this.file = file;
this.listener = listener;
}
#Override
protected void onPreExecute()
{
//stuff here
}
#override
protected Integer doInBackground(Void... params)
{
//Background stuff here
}
#Override
protected void onPostExecute(Integer... result)
{
listener.onTaskComplete();
}
}
MainActivity
public class MainActivity implements AsyncTaskListener
{
public void onCreate(Bundle savedInstanceState)
{
super(savedInstanceState);
setContentView(R.layout.main_activity);
//Your stuff
new ZipHelper(zip[0].mZipFileName, file, MainActivity.this).execute();
}
public void onTaskComplete()
{
//AsyncTask post stuff
}
}
Without calling a certain method in your MainActivity you can't feasibly start at a certain point in your MainActivity. The point of AsyncTask is to allow your calling Activity to keep going and not hold up the UI. What you can do is pass a context to your AsyncTask and show the Toast in onPostExecute()
public class MyTask extends AsyncTask<Void, Void ,Void> { // use whatever params you need here
private Context context;
public MyTask(Context c) {
context = c;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Toast.makeText(context, "You did it!". Toast.LENGTH_SHORT).show();
}
and call it by passing your Context
Mytask task = new MyTask(this); //or MyActivity.this depending on where you are
task.execute(); // pass params if you need
I suggest using Activity context instead of Application context for Toast

Categories