I have two challenges in my android program whcih are listed below , Kindly help me.
Suppose I start a thread A in MainActivity , and based on whether a certain condition
is met in thread A , the whole application must be closed. ( as finish() is available only in Activity and not accessible from a thread )
2.In MainActivity I have an EditText. Every letter i press is setup to transfer over bluetooth Socket to reach PC over bluetooth StreamCOnnection.
Hence , I need to get every letter (single character) that I type in the EditText, stored in a char variable. (It should also take into account the backspace button which means that no letter was typed).
As and when user types a letter in EditText , a char variable (single letter) is initialized in MainActivity and a running thread "A" will fetch and send value of the char variable to socket outputstream and clear the char variable so that newly entered letter could be stored for subsequent socket streams sending.
Hence my question for the point 2 is , How to fetch the last typed character of an EditBox taking into consideration backspace key to be ignored.
I Googled lot and also referred similar posts (Link1) & Link2, but I couldn't successfully implement it.
( PS: I'm Sorry that I have written detailed, instead of pasting Snippets as my program is a bit big and could confuse what my correct question is)
1) finish() is a function on the Activity object. If the thread is a subclass of the activity, it can fall it. If the Thread has a reference to the Activity, it can call activityReference.finish(). Finish() is the only way to end an activity programmatically.
2)Use a TextWatcher and add it to the EditText. That will tell you whenever a character is typed. You can figure out what the new data is and process it.
My suggestions would be:
1) Use a Handler to make a callback to the UI thread if the condition is met. From there, you can call the finish() method.
2) I would implement on onKeyListener for the Edittext. In that listener, you can listen for any key press you want (character or other).
Hope that helps.
Related
Everytime the application loses focus (whether manually by hitting the menu button, or by going to idle ) an APP_CMD_SAVE_STATE command is sent.
In the example shown from the Android API documentation for native activity (https://developer.android.com/reference/android/app/NativeActivity.html) , when this particular command is sent they are saving some sort of "state" inside their android_app .
// (...)
switch (cmd) {
case APP_CMD_SAVE_STATE:
// The system has asked us to save our current state. Do so.
engine->app->savedState = malloc(sizeof(struct saved_state));
*((struct saved_state*)engine->app->savedState) = engine->state;
engine->app->savedStateSize = sizeof(struct saved_state);
break;
// (...)
If you look in their code, savedState is just a struct holding values. First I thought that we need to save every single value that matters for our app everytime we lose focus, because otherwise the values would all become corrupt or something like that (yeah scary!)
I run a simple test by removing the code snippet above, and fortunately nothing changed, the values of my struct stayed the same after regaining focus even without saving them.
So I was wondering what is the purpose of this command ? Is that something important to consider ?
The APP_CMD_SAVE_STATE command is sent when your app loses focus. The aim is to make it possible to not only suspend your app, but, if the system has to kill the app to get some resources (e.g. free RAM), the app can be restored seamlessly next time the user returns to it.
If you look in their code, savedState is just a struct holding values.
Yes, from the point of view of the Native Activity, it's just a struct. But this struct is passed to Android in ANativeActivity_onCreate() function that's called via JNI (and usually it's implemented in android_native_app_glue.c from the SDK, which you could alter or replace if you need). Thus, Android will take care of the data when managing apps.
I run a simple test by removing the code snippet above, and fortunately nothing changed, the values of my struct stayed the same after regaining focus even without saving them.
Your test was too soft :) Try opening the Recents screen and close your activity from there: tap the × button or Close All command. The effect will be to kill the app, and you should now find that your data have been wiped unless you use the save-restore mechanism.
Expanding the functionality of an under development app, I need to show to the user a progress notification dialog. Problem is, I cannot get it done right. Furthermore, I cannot dismiss this notifier properly. Have tried with a clock and a variable set to e.g. "5000ms" and then to "0", without any lack.
What I need to achieve is the following functionality:
a. Check if the tag "storeparsedData" is in a TinyDB, populated with the fetched JSON data. I have this done, following #Taifun advice in my relative question.
b. If the tag is not there (empty list), do a getWeb.gotText block to get the JSON data (this is done with procedure "getWebData". This functions right, but takes a while about 1'35'' or more, so need to show something to the user.
c. While fetching JSON data form web, need to show a "ShowProgressDialog" notifier to the user, so I can cope with the smartphone being seemingly freeze.
d. If the tag "storeparseData" is populated with fetched JSON data, dismiss the notifier.
Have tried the following coding, without relevant success:
Can someone help me out, to achieve this functionality in this app? A blocks code or something to follow and learn, will be awesome.
Thank you all in advance for your answers.
[Edit1]
After #Taifun suggestions, the functionality in question seems to be working, but there is a problem."ShowProgressDialog" block never fires, neither on device or companion. Also where should block "DismissProgressDialog" be attached to disable notifier upon JSON data received?
Here is the reviewed blocks code, for checking stored tags in TinyDB. "ShowProgressDialog" never fires as it should. Are there any suggestion for this issue?
Here is the blocks code for the getWeb function to get the JSON data:
Please advise, with a block code if applicable.Thank you all.
Your progressNotifier.AfterChoosing event never will fire, because that event only fires after choosing something from a Notifier.ShowChooseDialog block, but not for Notifier.ShowMessageDialog blocks. Therefore use a Notifier.ShowChooseDialog block instead and set the second button in that block to empty string.
Your while loop will freeeze your app, as you already realized... You do not need the Clock.Timer event at all to check if your data is there.
Just do it like this: after having received your data in the Web.GotText event and having stored the data in TinyDB, then dismiss the progress dialog and display the message "Database is ready".
Update: Instead of storing your list n times inside the for each in list loop, you should store it only once after the for each in list loop is finished... Same for the DismissProgressDialog and ShowAlert block...
What is the purpose of that join block? You might want to remove it...
Good Afternoon, I have a question please have look at this.
I am using Retrofit beta 2 for retrieving data from api. I have an EditText in which I want to search some names from server. I got the output also, but for example consider there are some names:
ABC,XYZ,PQR,STU,ETC. These are the names stored in the server and I am retrieving this names using Retrofit beta2.
When I searched for ABC or abc it will display the results and when I removed the string from the EditText then nothing is displayed.
Till here I Have done.
My question is when I type names fast then the result is something else.
So, can anyone tell me how to avoid this.
Thank You.
The Call object has a couple of utility methods you can use namely isExecuted and cancel to control your request.
http://square.github.io/retrofit/2.x/retrofit/retrofit2/Call.html
I am assuming you are using TextWatcher. Pseudo-code as follows:
public void afterTextChanged (Editable s) {
// Cancel the request first before sending it again; this way you won't have two separate calls
if(call != null && call.isExecuted()) {
call.cancel();
}
// reinitialize call and execute it again
}
Generally speaking, it is ill-advised to listen for immediate input from user to spawn an action: you should rethink your approach; it's better to put a "submit" or button to execute the API call; otherwise you'll be spamming the server with several HTTP requests per input.
Remember the search text e.g. "AB" on the search result. Before displaying the result, check if the currently entered text still matches the text of the search result and only use the result, if it does.
Searched for "AB", current text "ABC" -> don't show the result.
Searched for "ABC", current text "ABC" -> show the result.
Like this it won't even matter in what order the two requests return, it will always display only the correct result.
I think it is not safe to start a new thread for every character
This is How I did in my App:
You can use a Queue to store every string that user input. Every time the edittext changed, a new string will be inserted to the queue
At the same time, you can send the query async by retrofit. but if the retrofit is on its way querying, you do not send immediately, only put the string into the queue. when your callback function is called by retrofit, you can remove all strings from queues except the latest one, then use retrofit to query this newest query.
Too much thread, will affect the performance of any device, because it the overhead involve will consume lot of resource.
starting a new thread for each character is by no means advisable, to answer your question each thread should continually check for a change in characters, when you type fast, it mean tend to skip some result but will eventually display the result of the last string correctly.
Since I'm using scanner, after scanning the barcode into edittext I need to get text from the edittext automatically and check the record for that barcode from the local database.
I know that there is addTextChangedListener for getting the text from the edittext automatically. But there are issues with that like it is checking each character of barcode when scanned instead of checking the barcode string in the local database.
You may use Handler and its methods
postDelayed(Runnable r, long delayMillis) and
removeCallbacks(Runnable r).
In your TextWatcher invoke removeCallbacks(...) in order to remove any previous Runnable and invoke postDelayed(...) with a little delay and a Runnable that will check the barcode string.
So you may accomplish your search in the database in several milliseconds after the last digit was sent to your EditText.
Also your scanner may send a special symbol at the end of a barcode. So read its documentation. Probably you don't need Handler, but you need to wait for that special symbol
Once I have completed the form, I cannot see the the form's output in the ResultsLabel I created. Attached is a screenshot below, does it look OK?
All help appreciated!
Regarding your statement "I cannot see the form's output in the label", is it possible that it's hidden behind the keyboard? Press the 'Done' button to hide the keyboard when you're done typing.
Regarding your question "does it look OK":
Your first StoreValue event is saving the value as the tag, which probably isn't great practice but can work
However, your second StoreValue overwrites the first one, and the third StoreValue overwrites the second one because you're using the same tag for all three.
If you want to save the two thumbpositions for that particular name you should do this:
TinyDB.StoreValue
tag = {name.text}
value = {make a list
{cashrequired.thumbposition}
{period.thumbposition}