I'm developing an app that gets values from an Arduino in Real Time.
It gets the angle of a machine non stop.
I need my app to get the max value from all the values it received from the Arduino and when it reaches 0 degrees after the 1st movment start another function.
Or if you can, how do I call functions when that value hits a specific value?
for example, on the image, how can I make the app call another function when the pointer hits the green range.
Thnaks
I tried something like this but doesn't work.
if (x>y){
y=x;
}
test5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
x=x+5;
Log.d("valuey", Integer.toString(y));
Log.d("valuex", Integer.toString(x));
}
});
im going to try something like this
Thread t = new Thread() {
#Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(100);
runOnUiThread(new Runnable() {
#Override
public void run() {
if (x>y){
y=x;
}
test5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
x=x+5;
Log.d("valor2", Integer.toString(y));
Log.d("valor3", Integer.toString(x));
}
});
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
I find out another way that makes more sence in this case.
Set an SpeedChangeListener that calls the same function.
speedView.setOnSpeedChangeListener(new OnSpeedChangeListener() {
#Override
public void onSpeedChange(Gauge gauge, boolean isSpeedUp, boolean isByTremble) {
if ((i>=-1 && i < 2) || i==4 || i==7) {
calculo();
avaliacao();
}
}
});
Related
I'm writing an Android app which connects to an BLE device and shows the end result of the measurement. I'm using a library of the company for communicating with the device and the problem I have is that the callback functions (onMeasurementFinished,onMeasurementFailed, etc.) only get called if I set a breakpoint and run the app with a debugger attached.
Here is how I connect to the device:
#Override
public void onClick(View view) {
try {
if (view.getId() == R.id.btnConnect) {
deviceArm.scan(this, this);
}
} catch (Exception ex) {
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
}
}
This is the callback of the scan that is working:
#Override
public void onDeviceConnected() {
txtResults.setText("Connected");
deviceArm.startMeasurement(this);
}
These are the callbacks of startMeasurement that only work if I debug:
#Override
public void onMeasurementError(Error error) {
runOnUiThread(new Runnable() {
#Override
public void run() {
txtResults.setText("Error");
}
});
}
#Override
public void onMeasurementFinished(MeasurementType measurementType, final Object o) {
runOnUiThread(new Runnable() {
#Override
public void run() {
txtResults.setText("Finished"+o.toString());
}
});
}
#Override
public void onMeasurementStarted() {
}
#Override
public void onMeasurementProgress(final MeasurementType measurementType, final Object o) {
runOnUiThread(new Runnable() {
#Override
public void run() {
txtResults.setText(o.toString());
}
});
}
I know it's not much information but I can only hope that mabey some of you have an idea what the problem could be.
What's likely happening is the peripheral device is doing some extra setup after connection. So putting a breakpoints in onDeviceConnected gives it time to do this.
As you say, not much information to go on, but it's possible the peripheral is sending the measurement results via notification/indication. So the peripheral needs time to configure these before starting measurments.
Try adding a delay in onDeviceConnected before starting the measurment. Ideally your library will have a callback along the lines of onDeviceReady, that you could use instead.
I want my button to work when I click it once. However, it only works if I click it twice.
I think this problem is related with Jsoup or Thread because when I removed the Jsoup method, it worked properly.
Of course, I have added Jsoup Library.
Here is my code:
View.OnClickListener listener = new View.OnClickListener(){
#Override
public void onClick(View v) {
new Thread(){
public void run(){
String word = editText.getText().toString();
try {
doc = Jsoup.connect("https://endic.naver.com/search.nhn?sLn=kr&searchOption=all&query="+word).get();
} catch (IOException e) {
}
Elements el = doc.getElementsByClass("fnt_e30");
if(el.size() == 0){
s="There is no result.";
return;
}
s="OK";
}
}.start();
textView.setText(s);
}
};
btn_search.setOnClickListener(listener);
I'm guessing that by 'works' you mean to say that the TextView get's set with the text "OK". In which case, your code is running properly, you have the s="OK"; running on a separate thread. Only after that value get's set will textView.setText(s); run as you expect it to. You'll probably want to do something along the following lines instead.
View.OnClickListener listener = new View.OnClickListener(){
#Override
public void onClick(View v) {
new Thread(){
public void run(){
String word = editText.getText().toString();
try {
doc = Jsoup.connect("https://endic.naver.com/search.nhn?sLn=kr&searchOption=all&query="+word).get();
} catch (IOException e) {
}
Elements el = doc.getElementsByClass("fnt_e30");
if(el.size() == 0){
s="There is no result.";
return;
}
s="OK";
runOnUiThread(new Runnable() {
#Override
public void run() {
textView.setText(s);
}
});
}
}.start();
}
};
btn_search.setOnClickListener(listener);
P.S - As mentioned by ADM in the comments, it's an asynchronous call (since it's done on a thread), Jsoup.connect will take time to connect and get the doc. You should preferably deactivate the button after it's clicked once (basically indicate to the user that the request is being handled and ask them to wait for the result).
You are doing it on wrong way. You are listening response and set in variable as well but not setting into the textview.
Why its happening second time? When you click first time code of setText will be executed but that will not having any value.
Right Approach. Add this code after you setting in value in s variable.
runOnUiThread(new Runnable() {
#Override
public void run() {
textView.setText(s);
}
});
I am new to android studio and my problem might sound pretty simple but I do badly need a solution.
I need to check whether my static string's value is equal to 1 before executing a particular block of code. It updates every 500ms. It goes from 1 to 16 and then back to 1.
I tried countdowntimer but it didn't work. Please help.
Edit:This is what i am trying.
store=findViewById(R.id.store);
store.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for (int i = 1; i <= 16; i++){
String m=s;
if (m.equals("1")) {
store();
break;
} else {
new Timer().schedule(new TimerTask() {
#Override
public void run() {
// this code will be executed after 2 seconds
}
}, 500);
}
}
}
});
So essentially what you want to do is, you want to observe the value right?
What you can do instead is, every time you change the value, you invoke a callback, or already explicitly call your block of code.
// this gets called every 500 ms
public static void updateMyValue(String value){
MY_STATIC_STRING = value;
callMyBlockOfCode();
}
This one worked for me. Thank you everyone for your time.
store=findViewById(R.id.store);
store.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String m=s;
if (m.equals("1")) {
store();
} else {
int i=Integer.parseInt(m);
i=(17-i)*delay;
new CountDownTimer(i, 1) {
public void onFinish() {
store();
}
public void onTick(long millisUntilFinished) {
}
}.start();
}
}
});
I want to press volume button programatically. In Java it is possible using the robot class, but in Android there is no robot class.
I am wondering how to achieve this in Android.
I would suggest you to increase/decrease the volume programmatically which would be a tad bit easier, however if you want to use it for some other process then you can check the code below -
EDIT - The snippet I gave before doesn't work, but this one does. It uses a runnable so the try catch block is necessary.
new Thread(new Runnable() {
#Override
public void run() {
try {
Instrumentation inst = new Instrumentation();
//This is for Volume Down, change to
//KEYCODE_VOLUME_UP for Volume Up.
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_VOLUME_DOWN);
}catch(InterruptedException e){}
}
}).start();
how about this
private abstract class SimpleButton extends Button {
public SimpleButton(String text) {
super(TechDemoLauncher.this);
setText(text);
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onButtonPressed();
}
});
}
public abstract void onButtonPressed();
}
after that you can just implement the onButtonPressed() method like this
private void Example(String string) {
yourLayout.addView(new SimpleButton(string) {
#Override
public void onButtonPressed() {
//insert your code
}
});
}
I am developing a quiz application in which I am using runnable thread for seekbar functionality. seekbar shows how much time is remaining. Now I want to stop the seekbar when a answer button is click.
here is my code for runnable thread.
new Thread(new Runnable() {
public void run() {
while (seekbarStatus < 100) {
seekbarStatus = LoadingStatus();
// sleep 1 second to show the progress
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
seekbar.setProgress(seekbarStatus);
}
});
}
if (seekbarStatus >= 100) {
// sleep for 2 seconds, so that you can see the 100% of file download
runOnUiThread(new Runnable() {
#Override
public void run() {
if(!(Ans_1.getTag()=="clicked" ||Ans_2.getTag()=="clicked" || Ans_3.getTag()=="clicked" ))
{
Ans_1.setEnabled(false);
Ans_2.setEnabled(false);
Ans_3.setEnabled(false);
Ans_1.setBackgroundColor(Color.GREEN);
points.setText("0 Punkt");
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
showDialogView();
}
}, SPLASH_DISPLAY_LENGHT);
}
}
public void showDialogView() {
// TODO Auto-generated method stub
dialog.show();
}
});
}
}
}).start();
please help me. Any help would be appreciated.
I am not getting how to solve it.
Thanks in advance.
Just add an instance variable:
private boolean isCancelled = false;
Then in the button onClick:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
isCancelled = true;
}
});
Then change
while (seekbarStatus < 100) {
to
while (seekbarStatus < 100 && !isCancelled) {