I have a helper (Not an activity) class that makes a query to an API which has a public function called run() and runs on a new thread (As per Android specifications). My MainActivity creates a new MakeQuery object and runs its run() function:
MakeQuery q = new MakeQuery();
q.run();
However, I need to access a variable from within the thread. Below is a short code sample:
public class MakeQuery implements Runnable {
private void setNewString(String localThreadString){
//NewString comes out null...
NewString = localThreadString;
}
public void run() {
new Thread(new Runnable() {
public void run() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
API Api = new API(keys and tokens);
//queryAPI() returns string
setNewString(queryAPI(Api, term1, term2));
//Don't know how to send intent from here (No context),
//I would like:
Intent i = new Intent(MainActivity.class, AnotherActivity.class)
}
}).start();
}
ASYNCTASK
//This runs in background thread
#Override
protected Void doInBackground(Void... params) {
API Api = new API(keys and tokens);
setNewString(queryAPI(Api, string1, string2));
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
Intent intent = new Intent(mContext, Activity2.class);
intent.putExtra("NewString", NewString);
Log.d(MYTAG, NewString);
}
MainActivity
public void buttonPressed(View view) {
Intent intent = new Intent(this, Activity2.class);
...
MakeQuery task = new MakeQuery(this);
task.execute();
startActivity(intent);
}
I have tried to look online for hours. I have tried to do AsyncTask, but I am not sure how to implement that with what I have. Furthermore, using localThread<String> did not help.
TLDR: I would like to know if it's possible to get NewString so I can pass it through an Intent to another Activity.
The Solution
Do not use Runnable, create a new AsyncTask as shown below. Also, make sure that the StartActivity is in the helper class and not in the MainActivity. This was because I was not waiting for the task to finish before starting the new activity.
Here is an implementation using async task:
public class MakeQueryTask extends AsyncTask<Void, Void, Void> {
private Context mContext;
private String newString;
public MakeQueryTask(Context context) {
mContext = context;
}
//This runs on a background thread
#Override
protected Void doInBackground(Void... params) {
API Api = new API(keys and tokens);
//queryAPI() returns string
setNewString(queryAPI(Api, term1, term2));
//You should start your activity on main thread. Do it in onPostExecute() which will be invoked after the background thread is done
//Intent i = new Intent(mContext, AnotherActivity.class);
//mContext.startActivity(intent);
return null;
}
private void setNewString(String localThreadString){
newString = localThreadString;
}
//This will run on UI thread
#Override
protected void onPostExecute(Void aVoid) {
Intent intent = new Intent(mContext, AnotherActivity.class);
mContext.startActivity(intent);
}
}
And you would execute like this:
MakeQueryTask task = new MakeQueryTask(this); //Here this is an activity (context)
task.execute();
If you are calling this runnable from an activity or service, you could pass in a context in the constructor. And just start the activity with the intent in the run() function.
public class MakeQuery implements Runnable {
private Context context;
public MakeQuery(Context context) {
this.context = context;
}
private void setNewString(String localThreadString){
//NewString comes out null...
NewString = localThreadString;
}
public void run() {
new Thread(new Runnable() {
public void run() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
API Api = new API(keys and tokens);
//queryAPI() returns string
setNewString(queryAPI(Api, term1, term2));
Intent intent = new Intent(MainActivity.class, AnotherActivity.class)
context.startActivity(intent);
}
}).start();
}
Related
I am having troubles understanding how to find a solution for my problems with threads in Android. So basically the current (simplified) code below is running on the main thread and that's causing me some issues because methods calculateMeanMagnitude() and predict() are slow and hence block the UI as expected.
What I would like to do is to compute those two functions in a separate thread and once I am done call updateData() in my UI thread.
I am really not sure how to do this both from a syntax point of view but also how to avoid busy waiting before updateData() since that would also block the UI thread.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
}
private double calculateMeanMagnitude(ArrayList<SensorReading> accReadings, boolean isAcc) {
return 0.0;
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Bundle data = intent.getExtras();
if (data == null) return;
if (data.containsKey(Constants.WindowBroadcastExtraName)) {
ScanResult scan = (ScanResult) data.getSerializable(Constants.WindowBroadcastExtraName);
if (scan != null) {
double meanMagnitude = calculateMeanMagnitude(scan.getAccReadings(), true);
float[] predictions = predict(meanMagnitude);
updateData(isStill, predictions, scan.getLocationScans());
}
}
}
};
}
Call your calculateMeanMagnitude() and predict() method in separate thread and once the both of them finished send s broadcast from that thread for updating your UI
register this receiver in on resume of your Activity class
IntentFilter = new IntentFilter();
mIntentFilter.addAction(any action string);
registerReceiver(mReceiver, mIntentFilter);
Asyntask for executing your methods on separate thread
private class Find extends AsyncTask<Void, Void, YourReusltfromthesemethods> {
#Override
protected YourReusltfromthesemethods doInBackground(Void... voids) {
calculateMeanMagnitude();
predict();
}
#Override
protected void onPostExecute(YourReusltfromthesemethods result) {
super.onPostExecute(aVoid);
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("any action string");
broadcastIntent.putExtra("Data", "Broadcast Data");
sendBroadcast(broadcastIntent);
}
}
}
And in onReceiver method of you receiver update your ui
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("any action string")){
Bundle data = intent.getExtras();
if (data == null) return;
if (data.containsKey(Constants.WindowBroadcastExtraName)) {
ScanResult scan = (ScanResult) data.getSerializable(Constants.WindowBroadcastExtraName);
if (scan != null) {
double meanMagnitude = calculateMeanMagnitude(scan.getAccReadings(), true);
float[] predictions = predict(meanMagnitude);
updateData(isStill, predictions, scan.getLocationScans());
}
}
}
}
};
Please guide me in this. Appreciate all your help.
My background service is toasting ABC
//-------------------String displayingText = "ABC";-----------------
And I have two strings, ABC and DEF declared in mainactivity.java
How do I pass the value displayingText from main activity to this service.
How do I change the displayingText to DEF after the toast ABC finished.
MyService.Java
public class MyService extends Service {
public static final long INTERVAL=3000;//variable to execute services every 5 second
private Handler mHandler=new Handler(); // run on another Thread to avoid crash
private Timer mTimer=null; // timer handling
//the get intent dont work. where or how should i put it?
Intent myIntent = getIntent();
if (myIntent !=null && myIntent.getExtras()!=null)
String value = myIntent.getExtras().getString(PassToService);
#Nullable
#Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("unsupported Operation");
}
#Override
public void onCreate() {
// cancel if service is already existed
if(mTimer!=null)
mTimer.cancel();
else
mTimer=new Timer(); // recreate new timer
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(),0,INTERVAL);// schedule task
}
#Override
public void onTaskRemoved(Intent rootIntent) {
stopSelf();///its will stop service
super.onTaskRemoved(rootIntent);
}
#Override
public void onDestroy() {
Toast.makeText(this, "In Destroy", Toast.LENGTH_SHORT).show();//display toast when method called
mTimer.cancel();//cancel the timer
super.onDestroy();
}
//inner class of TimeDisplayTimerTask
private class TimeDisplayTimerTask extends TimerTask {
#Override
public void run() {
// run on another thread
mHandler.post(new Runnable() {
#Override
public void run() {
// display toast at every 10 second
//String displayingText = "ABC";
String displayingText = myIntent.getStringExtra("PassToService");
final Toast Notify = Toast.makeText(getApplicationContext(), displayingText, Toast.LENGTH_SHORT);
Notify.setGravity(Gravity.CENTER, 0, 0);
Notify.show();
Handler cancelToast = new Handler();
cancelToast.postDelayed(new Runnable() {
#Override
public void run() {
Notify.cancel();
}
}, 1000);
}
});
}
}
}
You can do it by passing value from activity to service-
startService(new Intent(YourActivity.Service.class).putExtra("key","value"));
I have an android application where I'm using GCM. I'm following the tutorials and using an InstanceIDListenerService class which I'm attempting to fire off as an IntentService after a "subscription" page where the user enters some information. There is also some preliminary code firing off prior to this subscription page on a splash screen behind the scenes. The InstanceIDListenerService constructor is being called (and subsequently, the onHandleIntent) in the SplashScreen activity before I even get to the SubscriptionActivity. Why is it doing this? Is it possible for an intent service to start on it's own?
I do have the service registered in the AndroidManifest.xml file, and when I comment out the following lines, it does not trigger the instance to get created automatically, the app works as intended (until I need to use the instance of course...)
<service
android:name=".service.receiver.InstanceIDListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
SplashScreen.java
public class SplashScreen extends Activity {
private BroadcastReceiver dbInsertReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(bundle != null) {
// Handle results and move to next activity, should
// be the subscribe activity where I want the instance
// id listener to start.
}
}
};
private BroadcastReceiver providerXMLReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(bundle != null) {
// Handle results and start the next service
}
}
}
};
/** Called when the activity is first created */
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_splash_screen);
// Kick off the service download to update the provider data
Intent intent = new Intent(this, ProviderDataService.class);
startService(intent);
}
#Override
protected void onStart() {
super.onStart();
registerReceiver(providerXMLReceiver, new IntentFilter(ProviderDataService.CHANNEL));
registerReceiver(dbInsertReceiver, new IntentFilter(InternalDBService.NOTIFICATION));
}
#Override
protected void onResume() {
super.onResume();
registerReceiver(providerXMLReceiver, new IntentFilter(ProviderDataService.CHANNEL));
registerReceiver(dbInsertReceiver, new IntentFilter(InternalDBService.NOTIFICATION));
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(providerXMLReceiver);
unregisterReceiver(dbInsertReceiver);
}
private void moveToNextActivity(int subscriptionStatus) {
if(subscriptionStatus == DBSchemaHelper.IS_SUBSCRIBED_NOT_RESPONDED) {
Intent subscribeIntent = new Intent(SplashScreen.this, SubscribeActivity.class);
startActivity(subscribeIntent);
} else {
// Create an Intent that will start the Menu-Activity.
Intent mainIntent = new Intent(SplashScreen.this, MainActivity.class);
startActivity(mainIntent);
}
this.finish();
}
SubscribeActivity.java
public class SubscribeActivity extends CustomActionBarActivity {
public static final int NO_SUBSCRIPTION_STATUS = -99;
private DBMetaDataSource metaDao;
private int subscribeResult;
public SubscribeActivity() {
metaDao = new DBMetaDataSource(this);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_subscribe);
}
#Override
protected void onStart() {
super.onStart();
registerReceiver(tokenResponseReceiver, new IntentFilter(InstanceIDListenerService.TAG));
}
#Override
protected void onResume() {
super.onResume();
registerReceiver(tokenResponseReceiver, new IntentFilter(InstanceIDListenerService.TAG));
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(tokenResponseReceiver);
}
public void subscribeUser(View v) {
EditText emailTextView = (EditText) findViewById(R.id.subscriptionUserEmail);
String email = emailTextView.getText().toString();
// This is the only place I am manually starting this service.
// I have set a breakpoint here, but I never hit it and the service
// starts on its own and I hit the breakpoints in the service's
// onHandleIntent method.
Intent i = new Intent(this, InstanceIDListenerService.class);
i.putExtra("email", email);
startService(i);
}
public void goToNextActivity(View v) {
// They pressed the button to NOT subscribe, so we are calling this from the
// view rather than the intent receiver, meaning the view will not be null.
if(v != null) {
markUnsubscribed();
}
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(SubscribeActivity.this, MainActivity.class);
mainIntent.putExtra(MainActivity.SUBSCRIBE_STATUS_KEY, subscribeResult);
startActivity(mainIntent);
}
private void markUnsubscribed() {
metaDao.open(this);
DBMeta metaData = metaDao.get();
metaDao.update(Long.valueOf(metaData.getVersion()), metaData.getLastRunInMillis(), DBSchemaHelper.IS_SUBSCRIBED_RESPONDED_NO, null);
metaDao.close();
}
private BroadcastReceiver tokenResponseReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
subscribeResult = intent.getIntExtra(InstanceIDListenerService.RESPONSE_KEY, NO_SUBSCRIPTION_STATUS);
goToNextActivity(null);
}
};
}
You shouldn't be starting a InstanceIDListenerService yourself - that is for the system to call you when you need to refresh your Instance ID tokens via a callback to onTokenRefresh() - if you haven't created any instance id tokens yet, then you'd just have no work to do on that first call.
If you have other work to do, you should use your own service.
I have 2 classes which are GcmMessageHandler and Control (its an activity class, shows some graphics). When i handle the gcm message, i want to refresh control class (but if its front)
public class GcmMessageHandler extends IntentService {
String mes;
private Handler handler;
public GcmMessageHandler() {
super("GcmMessageHandler");
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
handler = new Handler();
}
#Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
mes = extras.getString("title");
showToast();
Log.i("GCM", "Received : (" +messageType+") "+extras.getString("title"));
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
public void showToast(){
handler.post(new Runnable() {
public void run() {
if(mes.equals("Control")){
}else{
Toast.makeText(getApplicationContext(),mes , Toast.LENGTH_LONG).show();
}
}
});
}
}
In this part:
if(mes.equals("Control")){ }
if the control activity class is resume, i want to refresh it. How can i do this?
You can use a BroadcastReceiver in order to notify your activity about any changes. So register a BroadcastReceiver in your activity first:
public class MainActivity extends Activity {
public static String REFRESH_ACTIVITY = "com.domain.action.REFRESH_UI"
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// do UI updates
}
};
#Override
public void onResume() {
super.onResume();
// do UI updates
IntentFilter filter = new IntentFilter();
filter.addAction(REFRESH_ACTIVITY);
this.registerReceiver(broadcastReceiver, filter);
}
#Override
public void onPause() {
super.onPause();
this.unregisterReceiver(broadcastReceiver);
}
...
}
Then send the broadcast to perform the UI update from any location:
if (mes.equals("Control")) {
Intent intent = new Intent();
intent.setAction(MainActivity.REFRESH_ACTIVITY);
sendBroadcast(intent);
}
Maybe you could use an observer design pattern.
Let the GcmMessageHandler hold the Control activity as an observer and then notify it when needed.
public class GcmMessageHandler extends IntentService {
String mes;
private Handler handler;
private Control mObserver
public GcmMessageHandler() {
super("GcmMessageHandler");
}
public void attachObserver(Control ctrl) {
mObserver = ctrl;
}
Then you just add a method to the Control class that can be called from the GcmMessageHandler class.
if(mes.equals("Control")){
mObserver.update(); // example
}else ...
It would be more slick if you first defined an observer interface:
public interface IObserver {
public abstract void update();
}
and had your Control class implement that. This way your GcmMessageHandler class could have a list of observers:
public class GcmMessageHandler extends IntentService {
String mes;
private Handler handler;
private List<IObserver> mObservers;
public GcmMessageHandler() {
super("GcmMessageHandler");
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
handler = new Handler();
mObservers = new ArrayList<IObserver>();
}
public void attachObserver(Control ctrl) {
mObservers.add(ctrl);
}
private void notify() {
for(IObserver observer : mObservers)
observer.update();
}
And of course if the Control class is the one holding the GcmMessageHandler object your just call the attach method from Control like this:
myGcmMessageHandler.attachObserver(this);
I push a web service call in my activity to a thread (shown below). The first time I do this in the activity it works fine (gets the text from my edittext and loads the service to get lat/lng data)
But when I click the back button (emulator) and try to fire off this thread a second time it blows up after the .start(); in my click handler. What might I be doing wrong here? thanks
private Thread getLocationByZip = new Thread() {
public void run() {
try {
EditText filterText = (EditText) findViewById(R.id.zipcode);
Editable zip = filterText.getText();
LocationLookupService locationLookupService = new LocationLookupService();
selectedLocation = locationLookupService.getLocationByZip(zip.toString());
locationHandler.post(launchFindWithLocationInfo);
} catch (Exception e) {
}
}
};
private Runnable launchFindWithLocationInfo = new Runnable() {
#Override
public void run() {
try {
Intent abc = new Intent(LocationLookup.this, FindWithLocation.class);
startActivity(abc);
} catch (Exception e) {
}
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location);
locationHandler = new Handler();
findViewById(R.id.findbyzip).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getLocationByZip.start();
}
});
}
Update
After the great advice I went with an AsyncTask so if anyone finds this going forward the above thread/handler model looks something like the below as an asynctask
private class LocationLookupTask extends AsyncTask<String, Void, Location> {
private ProgressDialog dialog;
#Override
protected void onPreExecute() {
this.dialog = ProgressDialog.show(LocationLookup.this, "", "Loading...");
}
#Override
protected Location doInBackground(String... zips) {
Location selectedLocation = null;
for (String zip : zips) {
LocationLookupService locationLookupService = new LocationLookupService();
selectedLocation = locationLookupService.getLocationByZip(zip);
}
return selectedLocation;
}
#Override
protected void onPostExecute(Location location) {
this.dialog.dismiss();
((AppDelegate) getApplicationContext()).setSelectedLocation(location);
Intent abc = new Intent(LocationLookup.this, FindWithLocation.class);
startActivity(abc);
}
}
Now to call this in the onclick you would do this
findViewById(R.id.findbyzip).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText filterText = (EditText) findViewById(R.id.zipcode);
Editable zip = filterText.getText();
LocationLookupTask task = new LocationLookupTask();
task.execute(new String[]{zip.toString()});
}
});
You can't start a thread twice:
It is never legal to start a thread more than once.
Taken from Thread.start().
So, you need to create a new thread and start that one.
You can not call twice the start method of the Thread class, I suggest you also control the logic within the method onCreate since according to the life cycle of an Activity that method may be called by Android lifecycle Activity Manager.
Furthermore i suggest you to avoid this approach and consider to use the AsyncTask provided by the Android SDK.
http://developer.android.com/reference/android/os/AsyncTask.html
If you really want to do this without creating a new class or using AsyncTask, you could just make a method to get a new Thread on each call:
private Thread getLocationByZip;
private void getLocation() {
getLocationByZip = new Thread() {
public void run() {
try {
EditText filterText = (EditText) findViewById(R.id.zipcode);
Editable zip = filterText.getText();
LocationLookupService locationLookupService = new LocationLookupService();
selectedLocation = locationLookupService.getLocationByZip(zip.toString());
locationHandler.post(launchFindWithLocationInfo);
} catch (Exception e) {
}
}
};
getLocationByZip.start();
}
Then replace getLocationByZip.start() in your code with getLocation(). However, I agree that an AsyncTask would be a better way to go, though this would work for you.