Not sure how should I phrase it, but In my app sometimes a single button click generates multiple events and hence it ends up sending multiple similar transactions to the server which is causing data integrity problem. Here is the code flow, Do note that this code runs on users phone which may have weaker connections at times, hence I store the data in SQLite and there is a separate sync service which sends data (fetching from SQLite tables) to Server when Internet is connected.
This is where the click is captured and event is posted for Main Activity (Landing Page)
#OnClick(R.id.btn_add_attempt)
public void onAttemptClick() {
try{
btnAddAttempt.setEnabled(false);
EventBus.getDefault().post(new ExampleActionEvent());
moveToHomePage();
} catch (Exception e){
e.printStackTrace();
Crashlytics.logException(e);
}
}
Here is the Event subscription from my main Activity.
#Subscribe()
public void onExampleAction(ExampleActionEvent exampleActionEvent)
{
if(application == null) application = (ExApplication) getApplication();
final Thread thread = new Thread(new Runnable() {
#Override
public void run() {
ArrayList<Something> somethings = application.getArray().getsomethings();
if (!ExUtils.validatesomethings(somethings)){
runOnUiThread(new Runnable() {
#Override
public void run() {
RiderUtils.showSimplePopup(
MainLandingActivity.this,
getString(R.string.title_alert),
getString(R.string.alert_message),
getString(R.string.done),
null,
false,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
showPendingSomething();
ExUtils.moveToPending(MainLandingActivity.this);
}
},
null,
false);
}
});
return;
}
status = application.getArray().getStatus();
imageLocalPath = application.getArray().getsomethings().get(0).getPhotoPath();
Log.d(Const.TAG, "image path is: "+imageLocalPath);
try {
ExUtils.pushExampleActionEvent(application.getArray(), MainLandingActivity.this, xyz, abc);
} catch (Exception e) {
e.printStackTrace();
}
FileWriteEvent fileWriteEvent = null;
String currentTimestamp = application.getArray().getTimestamp();
switch (status) {
case Const.UPDATE_SUCCESS:
fileWriteEvent = new FileWriteEvent(Const.EVENT_CSV_FILE_SUCCESS_VALUE, application.getArray(), "",
cachedLocation, currentTimestamp, null);
break;
case Const.UPDATE_ATTEMPTED:
fileWriteEvent = new FileWriteEvent(Const.EVENT_CSV_FILE_ATTEMPTED_VALUE, riderApplication.getBulkArray(), "",
cachedLocation, currentTimestamp, null);
ExUtils.showSnackbar(holder, getString(R.string.Example), Snackbar.LENGTH_LONG);
break;
}
if(fileWriteEvent != null)
{
EventBus.getDefault().post(fileWriteEvent);
}
createAll(); //SQLite DB call for Inserting some data
runOnUiThread(new Runnable() {
#Override
public void run() {
try {
switch (status) {
case Const.UPDATE_SUCCESS:
RiderUtils.showSnackbar(holder, getString(R.string.Success), Snackbar.LENGTH_LONG);
break;
case Const.UPDATE_ATTEMPTED:
RiderUtils.showSnackbar(holder, getString(R.string.Failed), Snackbar.LENGTH_LONG);
break;
}
}catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
thread.start();
}
I am not able to figure out why is it misbehaving sometimes randomly otherwise it runs smoothly, Hence I am not able to regenrate the issue while debugging.
So after sometimes I looked at the problem again and found out the developer who worked before me had not declared the Main Activity as SingleInstance and hence Every time if the app was opened from Notification Drawer as we were showing it in Notification drawer if app is running, then it was creating a new Activity hence registering on EventBus which was causing the issue by creating duplicate events.
Related
my app is fairly simple and always works IF my IOT device is up.
i need to load a popup and show the ReScan button on the toolbar if the device cannot be found.
the app preloads IPaddress="-" and loads 2 asyncTask(s)
one uses NsdManager.DiscoveryListener to find the mDNS name and loads the IP into IPaddress
this task watches to see IPaddress change and gets the presets from the device by JSON and sets up the UI or pops up the error dialog with instructions if not found.
MY PROBLEM:
when counter >= 15 , i show the "Rescan" Button on the toolbar with setMenuVisible() then popup the error dialog but when the button in the dialog is pressed to close the dialog, the "Rescan" Button disappears again.
Also times out in about 5 seconds.
how do i get the "Rescan" Button to stay?
.
private class getSettingsFromClock extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
String mlooper = IPaddress;
Log.i(TAG, "LOG getSettingsFromClock doInBackground started ");
int counter = 0;
while ( mlooper.equals("-") ) {
mlooper = IPaddress;
try {
Thread.sleep(600);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter++;
if (counter >= 15) // in normal operation counter never goes above 3
{
Log.i(TAG, "LOG getSettingsFromClock - NO IP Found, count= " + counter );
runOnUiThread(new Runnable() {
#Override
public void run() {
setMenuVisible( true, R.id.action_rescan); // show rescan button on toolbar
try { // delay is debugging only
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
//scanning failed Popup Dialog
final Dialog dialog = new Dialog(context );
dialog.setContentView(R.layout.popup);
dialog.setTitle("Scan Error");
Button button = dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
Toast.makeText(getApplicationContext(),
"Could Not get presets from clock. \n check Clock is on and on WiFi\n and reload app.",
Toast.LENGTH_LONG).show();
}
});
break;
}
}
if( IPaddress != "-" )
{
// gets JSON here
} else
{
// add popup - IOT Not found
}
// JSON starts here
if (JSON_return != null) {
try {
// loads presets from JSON to UI here
} catch (final JSONException e) {
Log.e(TAG, "LOG, JSON parsing error: " + e.getMessage());
}
} else
{
Log.e(TAG, "LOG, Could Not get JSON from Clock.");
}
return null;
}
} // end asyncTask class
// remember to run on main thread
// NOTE; private Menu option_Menu; declared in MainActivity
// ie; setMenuVisible( true, R.id.action_rescan);
public void setMenuVisible(boolean visible, int id) {
if (option_Menu != null) {
option_Menu.findItem(id).setVisible(visible);
}
}
Mike M. had it, Thank You Mike
added onPrepareOptionsMenu()
added showRescan = visible; and invalidateOptionsMenu(); to setMenuVisible()
all work perfectly now.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
try {
if( showRescan )
{
option_Menu.findItem(R.id.action_rescan).setVisible( true );
} else
{
option_Menu.findItem(R.id.action_rescan).setVisible( false );
}
}
catch(Exception e) {
Log.e(TAG, "onPrepareOptionsMenu error");
}
return true;
}
// when task is completed you can show your menu just by calling this method
// remember to run on main thread
// ie; setMenuVisible( true, R.id.action_rescan);
public void setMenuVisible(boolean visible, int id) {
if (option_Menu != null) {
option_Menu.findItem(id).setVisible(visible);
showRescan = visible;
invalidateOptionsMenu();
}
}
Here is my code:
automaticCountryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
progressBar.setVisibility(View.VISIBLE);
if (ContextCompat.checkSelfPermission(HomeActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED){
setUpLocationPermission();
return;
}
Log.d(TAG, String.valueOf(gps.canGetLocation()));
Log.d(TAG, String.valueOf(gps.getLocation()));
Log.d(TAG, String.valueOf(gps.getLatitude()));
Log.d(TAG, String.valueOf(gps.getLongitude()));
Geocoder myLocation = new Geocoder(HomeActivity.this);
try
{
myList = myLocation.getFromLocation(gps.getLatitude(), gps.getLongitude(), 1);
}
catch (Exception e)
{
Log.d(TAG, "unable");
e.printStackTrace();
}
if(myList != null) {
try {
String country = myList.get(0).getCountryName();
Log.d(TAG, country);
findCountryInArrayList(country);
}
catch (Exception e)
{
Toast.makeText(HomeActivity.this, "Didn't manage to automatically detect location.", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
});
}
I want that immediately after the view is clicked the progressbar will become visible. However, it donesn't become visable until all the code is finished, which is against the whole point.
WHy is this not happening right at the beginning of the click? I have put progressBar.setVisibility(View.VISIBLE) at the top, why is it only executed after all the code is done, which sometimes takes a few seconds.
Thanks very much.
This is because you are trying to do your work on the UI thread - the UI will not actually be updated at all until this method finishes.
Try changing up your call to this:
public void onClick(View view) {
progressBar.setVisibility(View.VISIBLE);
progressBar.post( new Runnable() {
public void run() {
// long running code that has UI interactions
}
});
}
This will show the view immediately, and submit the runnable - long running task - to the message queue; this task will be run on a background thread that can still manipulate the UI, but will not cause it to hang.
i am trying to display a Toast on the screen and when Toast fades off then move to the next question. I have tried with Thread but cannot seem to manage.
My code:
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getUserSelection()){
position = position + 3;
if (position < questionsArray.size()) {
curName = questionsArray.get(position).getName();
curArray = questionsArray.get(position).getAnswers();
curIscorrect = questionsArray.get(position).getIscorrect();
setupQuestionView(curName, curArray, curIscorrect);
} else {
StringGenerator.showToast(QuestionsActivity.this, "Your score : " + score + "/" + (questionsArray.size() / 3));
}
}else {
StringGenerator.showToast(QuestionsActivity.this, getString(R.string.noanswerselected));
}
}
});
and the getUserSelectionMethod:
private boolean getUserSelection() {
correct = (RadioButton)findViewById(group.getCheckedRadioButtonId());
if (correct == null){
return false;
}else {
correctAnswerText = correct.getText().toString();
if (map.get(correctAnswerText).equals(Constants.CORRECTANSWER)) {
score++;
setCorrectMessage();
return true;
} else {
setWrongMessage();
return true;
}
}
}
private void setCorrectMessage() {
correctToast = new Toast(QuestionsActivity.this);
correctToastView = getLayoutInflater().inflate(R.layout.correct, (ViewGroup) findViewById(R.id.correctRootLayout));
correctText = (TextView)correctToastView.findViewById(R.id.correctTextView);
correctText.setText(getString(R.string.correctAnswer));
correctToast.setDuration(Toast.LENGTH_SHORT);
correctToast.setView(correctToastView);
correctToast.show();
correctThread = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
correctToast.cancel();
}
});
correctThread.start();
}
private void setWrongMessage() {
wrongToast = new Toast(QuestionsActivity.this);
wrongToastView = getLayoutInflater().inflate(R.layout.wrong, (ViewGroup) findViewById(R.id.wrongRootLayout));
wrongText = (TextView)wrongToastView.findViewById(R.id.wrongTextView);
wrongText.setText(getString(R.string.wrongAnswer));
wrongToast.setDuration(Toast.LENGTH_SHORT);
wrongToast.setView(wrongToastView);
wrongToast.show();
wrongThread = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
wrongToast.cancel();
}
});
wrongThread.start();
}
Any suggestion on how to do this?
You can determine the toast visibility:
toast.getView().getWindowToken()
If the result is null, than your toast isn't visible anymore, and than you can run any code you want.
as stated in this answer you can start a thread that waits the duration of the Toast:
Thread thread = new Thread(){
#Override
public void run() {
try {
Thread.sleep(3500); // 3.5seconds!
// Do the stuff you want to be done after the Toast disappeared
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Toast.LENGTH_SHORT and Toast.LENGTH_LONG are only flags so you have to either hard code the duration or keep them in a constant. The durations are 3.5s (long) and 2s (short).
If you want to manipulate some of your views, you cannot do this in another thread than the "main" UI thread. So you have to implement a kind of callback/polling mechanism to get notified when the SleepThread has finished.
Check this answer to read about a couple of ways to do this. Probably the easiest of them to understand and implement is this:
After you started your Thread you can check if it is still alive and running by calling thread.isAlive(). In this way you can do a while loop that runs while the thread is running:
// start your thread
while(thread.isAlive()){}
// continue the work. The other thread has finished.
Please note that this is NOT the most elegant way to do this! Check the other possibilities in the answer I've mentioned above for more elegant solutions (especially the last one with the listeners is very interesting and worth reading!)
That's because the Thread class is purely executed in the background and you need to manipulate the view in the Main thread. To solve your issue just replace the Thread with AsynTask.
AsyncTask<Void,Void,Void> a = new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
correctToast.cancel();
}
};
a.execute();
If you look at my code you can see my onPostExecute, this method is called in the Main Thread.
My Error was because i was trying to acess UI Elements through another Thread so modifying the code like this:
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(500);
QuestionsActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
moveToNextQuestion();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
did the trick. I hope my answer helps someone!!!
in my code I have a code inside a thread that first tries to update something from Database and if the update is not successful then an insert is tried. My problem is that this code is called twice (it's not ok I know but let's ignore this for now) and because of this the insert is called twice because the update is not finished (at least this is what I think). I searched on google I thought to use thread.join(). My question is if it's ok to use thread.join() in my case.
Thanks
public void persistChangesToDatabase() {
final Thread backThread = new Thread(new Runnable() {
#Override
public void run() {
try {
openDataBase();
<-- SOME CODE -->
//First, an UPDATE is tried
int rowsAffected = updateTable();
// If update fails an INSERT is tried
if (rowsAffected == 0) {
//Insert
insert();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
});
backThread.start();
try {
backThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
I have Singleton class which holds all the data (visitors of some site) and the data is updated by a service. I have an interface, which is implemented by an (list) activity (which shows visitors), so now as I get the data updated, I simply call the interface method so that the list activity can refresh it.
Now I need to maintain the time visitors are on site (at client end). I want to make a thread in Singleton class which will run a loop after every second, but I am not able to call any method on Main thread, using Handlers.
Here's the code of thread:
void startHeavyDutyStuff() {
Thread t = new Thread() {
public void run() {
try {
while(true) {
sleep(1000);
ArrayList<VisitorMC> data = SharedAppManager.appManager().visitorsData;
boolean doReload = false;
for (VisitorMC item: data) {
item.secsOnSite++;
if(item.secsOnSite == 60) {
item.secsOnSite = 0;
item.minsOnSite++;
doReload = true;
}
}
if(doReload) {
messageHandler.sendEmptyMessage(1);
} else {
messageHandler.sendEmptyMessage(0);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t.start();
}
And here's the code where I am making the Handler on Main Thread (in Singleton class):
private SharedAppManager() {
//Initialization of the data.
Looper.prepare();
messageHandler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what) {
case 0:
Log.d("THREAD", "after every second");
break;
case 1:
if(visitorsDelegate != null) {
visitorsDelegate.updateVisitorsTime();
}
break;
default:
}
}
};
startHeavyDutyStuff();
}
What am I doing wrong here?
Edit:
I need to update the UI after each second, that's why I am running a separate thread which could change the Data and call update on UI.