Android - is SignalStrength an Event Class? - java

I have a little problem with a SignalStrength Android class. Is there a way to use methods from this class without referencing an object from this class in a onSignalStrengthsChanged() method?
From all the examples I saw, it seems to me that this is an Event Class and that object is created when an event (change of gsm signal strength) occurs and that this is the only way to call methods from this class. Am I right?
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
String power= String.valueOf(signalStrength.getGsmSignalStrength());
gsmStat.setText("GSM cinr: " +power);
}
What if I want to call methods from this class (and read gsm signal properties) in some other case, for example when I click a button object?
Thank you very much for any help, I'm really stuck with this.
BR,
Z
Thank you for clearing this up. I tried to use getNeighbouringCellInfo but for some reason this return 0. And yes I've searched and tried different examples.
Tel = ( TelephonyManager )getSystemService(Context.TELEPHONY_SERVICE);
public void onClick(View v) {
List<NeighboringCellInfo> neighCell = null;
neighCell = Tel.getNeighboringCellInfo();
NeighboringCellInfo thisCell = neighCell.get(1);
int thisNeighRSSI = thisCell.getRssi();
String rssi=Integer.toString(thisNeighRSSI);
tvRSSI.setText("Test: " +rssi);
}

You can't. The only way to get a relevant SignalStrength object is in the onSignalStrengthChanged method.
You can, however, use the TelephonyManager.getNeighbouringCellInfo method to list surrounding cells and get the signal RSSI power (getRssi).

See: getNeighboringCellInfo() returning null list
getNeighboringCellInfo()
This doesn't seem to work on Samsung devices.
Sorry to put this as an answer, but I cannot add a comment to an existing answer.
As for my answer, you can track the RSSI yourself in a service so that when you press the button, the last recorded RSSI value is returned. Having said that, I've found that on the Samsung Galaxy I work with, the RSSI value is actually the number of antenna bars (ie. I only get 4 values) and not the actual RSSI.

Related

Android , What is the difference between these codes?

i was watching thenewboston's tutorial about fragments and i came across this line of code..
#Override
public void sendtex(String top, String bottom) {
BottomFregment_class bottomFregment = (BottomFregment_class) getSupportFragmentManager().findFragmentById(R.id.Main);
bottomFregment.finale(top,bottom);
}
this was to change TextView by getting text from another fragment! and "sendtext is implemented method from that fragment"
i replaced
BottomFregment_class bottomFregment = (BottomFregment_class) getSupportFragmentManager().findFragmentById(R.id.Main);
bottomFregment.finale(top,bottom);
with
BottomFregment_class bottomFregmentClass = new BottomFregment_class();
bottomFregmentClass.finale(top,bottom);
and everything worked fine!
i want to know that is there any difference in between these two codes?
or will this cause any performance issues?
In the first, you fetch an existing fragment, maybe with some data in there, on the other one you create an empty one.

Combobox doesn't react to ".setValue()" nor ".select()"

This is my code:
comboBoxInstance.setInputPrompt("Something...");
comboBoxInstance.setNullSelectionAllowed(false);
Cookie comboCookie = getCookieByName("combo");
comboBoxInstance.select((comboCookie != null) ? comboCookie.getValue() : null);
final TextField textFieldInstance = new TextField("Textfield");
textFieldInstance.setInputPrompt("Something...");
Cookie tfCookie = getCookieByName("tf");
textFieldInstance.setValue((tfCookie != null) ? tfCookie.getValue() : null);
The problem is that the textfield works pretty well with the "Cookie setup". Only the combobox is refusing to work like it should.
The output is like this:
I've tried to use .setValue() instead of .select() but this has pretty much the same effect. I've also made sure that both the Cookie itself and the correct value are provided.
It may help to have a look at the part where the cookie is generated:
Cookie comboCookie = new Cookie("combo", comboBoxInstance.getValue().toString());
cookieProcessing(costcentreCookie); //<- sets maxage and vaadin related stuff (like adding the cookie)
Edit:
A few points to the data flow.
I'm generating a ComboBox with a SimpleJDBCConnectionPool's SQLContainer as the data container (coming from a TableQuery). Here's the initialization (executed in the constructor) in the combobox class:
private void init() throws SQLException {
this.setContainerDataSource(generateContainer());
this.setItemCaptionPropertyId("something");
}
The private method generateContainer() returns the SQLContainer of course.
This happens if I click on a particular button which opens up a dialog. This dialog is the fragment shown in the picture above. The combobox - of course - is part of it.
What one is supposed to do now is setting his data (get an item of the ComboBox) and hit save. The save button executes the routine to store the cookies. It's the code already mentioned above (Cookie comboCookie = new Cookie(...).
Okay, now the user is going to open up the dialog again. It's not important whether he reloads the application or just reopens the dialog (or does something else). It's basically the same in the app.
The dialog opens up and initializes the combobox (and the textfield) once again. However, this time it's supposed to gather the data out of the stored cookies. This is were the issue happens. This works well for the textfields (there are two but I've omitted one for shortening reasons) but not for the combobox, even tough it should've the exact same data as before. Hold in mind that it's the exact same class with the exact same initialization as when we stored the cookies in the first place.
I've the vague presumption, that it has to do something how the code is stacked. Maybe it hasn't finished loading the datacontainer while trying to set the appropriated value which then can't be found.
Edit2:
I've finally managed to reveal something. The ComboBox is indeed empty when the ".select()" is executed. However, this means, that the ComboBox is left untouched (it's only kind of "linked" to the datacontainer) until someone drops down the items. As soon as this happens, the items are there and I can possibly select them.
Is it supposed to work like this? O.o Am I able to fully initialize the combobox before I do something else? Something like:
private void init() throws SQLException {
this.setContainerDataSource(generateContainer());
this.setItemCaptionPropertyId("something");
this.gatherTheItems();
}
Edit3 - Test with ".setImmediate(true)"
I've changed the init to:
private void init() throws SQLException {
this.setContainerDataSource(generateContainer());
this.setItemCaptionPropertyId("SOMETHING");
this.setImmediate(true);
}
This didn't change anything. The combobox is still empty:
Finally! At first I've found a workaround which was like this:
for (Iterator it_IDS = combobox.getItemIds().iterator(); it_IDS.hasNext();) {
Object id = (Object) it_IDS.next();
if(id.toString().equals(cookie.getValue().toString())){
combo2.select(id);
break;
}
}
However, I couldn't believe that this was working since it doesn't change anything at the core problem. So I've investigated, that the RowID is built via a BigDecimal and voilĂ :
if(cookie != null) {
combobox.select(new RowId(new BigDecimal(cookie.getValue())));
}
I'm so happy right now :) Thanks for your patience kukis.
In case you came here because you're experiencing the same issue using a BeanItemContainer as datasource, bear in mind that you must implement both equals()and hashCode() methods on the underlying class for ComboBox's select() or setValue() methods to work.
You have plenty examples on Vaadin Forum on how to implement these methods:
ComboBox select value problem
Select or ComboBox does not Show Selected Property
Combobox select/setValue

JavaFX - How to synchronize CustomTab (extending Tab) and TabPane

I am developing an IDE. I created a CustomTab.java class by extending the default JavaFX Tab. I wanted to add a color transition animation, but I ran into a problem. The method in TabPane named getTabs(), which returns ObservableList<Tab>, is final. This means I can't override it to return ObservableList<CustomTab>. It seems that constructions like this:
for (Tab tab : tabPane.getTabs()) {
((CustomTab) tab).stopFlash();
}
used in Controller.java are neck breaking and wrong.
What is the right way to do this?
I think my reply is quiete late, but I think there is a way to do so.
Use the function
tab.setUserData(Object c);
In that Object you can store all the informations you'll need to proceed.
so for example create an Object holding all the objects you need.
E.g. you can hold the reference to your webView.
Also if you need to have different informations you could use:
tab.getProperties().put(Object key, Object data);
// receive the value with
tab.getProperties().get(key);
Here a link to the documentation I found
Maybe the Javadoc is better, but I found this one.
Here is a little example that I wrote:
public SQLEditorTab getNewTab(){
SQLEditorTab tab = new SQLEditorTab("new Tab " + number);
tab.setId("newTab" + number);
tab.setOnCloseRequest(this);
tab.setUserData(tab.tabContent);
number ++;
return tab;
} // just to create the tab
In my listener I receive the result simply by:
sqlEditorPane.getTabs().forEach((Tab t) -> {
if(t.getUserData() != null){
System.out.print(t.getUserData().getClass());
}
});

can I control the refresh rate of a Java listener?

I'm struggling with this issue since some days ago and I'm not able to find a solution.
I have a listener which receives market data (orders at bid and ask). If market is quiet (pre-market or post-market (low volatility)) everything works fine. But once the market is open the listener receives events too fast. So after a couple of minutes my app freezes.
Right now the listener only assigns the received data to a var.
orderBookBid.getBuyOrders().addListener(new ObservableListModelListener<Order>() {
#Override
public void modelChanged(final Change<? extends Order> change) {
System.out.println("bid event");
bidChange = change.getSource();
}
});
The program only freezes when uses real data. When market is closed and uses test data from a local file works fine.
Is there any way to set the maximum number of events per second? Or any way to ignore events for a short time period?
Any idea on how can I handle this would be very appreciated.
Thanks.
You could put a load balancer in your application, that way it will create a queue and will not freeze the application.
If you want to let go some events, in the logic of your listener, you should have something that check if it's been X time since the last time you managed the event.
private long timeSinceLastEventManaged = 0;
private final static long MINIMUM_TIME = 2000; //2 seconds
In your listener
public void modelChanged(final Change<? extends Order> change) {
long timeSystem = System.currentTimeMillis();
if(timeSystem - timeSinceLastEventManaged > MINIMUM_TIME){
//do your stuff
timeSinceLastEventManaged = timeSystem;
}
}
First of all you should get rid of the println as it is really slow
The rest depends on what you are doing. Right now it seems that you are just getting the value and writing it to a variable. You will only see the latest change that way and if that is what you want the solution #TyMarc suggested will work fine.
If what you showed us is just an example and you really need every change things get a bit more complicated. Your modelChanged method should be changed to add the current value to a queue (e.g a LinkedList or Stack).
public void modelChanged(final Change<? extends Order> change)
{
syncronized(syncObject)
{
//add to your preffered queue
syncObject.notifyAll()
}
}
This frees your listener from the real work and it can keep collecting data.
I added a syncronized as someone has to do the work. For this you can use a Thread that runs something like this:
Order current;
while(keeprunning)
{
syncronized(syncObject)
{
if(queue.hasNext())
{
current = queue.getNext()
}
else
{
Thread.wait()
}
}
//do the real work here
}
Now someone else has the problem. Literally. If the Thread can't handle the inflow of data the queue will grow in size until you run out of memory or hit some other limit. But that's another story.
And yes, nothing of this will compile as I only wanted to show an example

Why do app gets stopped unexpectedly everytime using any code

My apologies
Hi, I am a newbie to the advance level programming. I have been working around with the basics and the syntax of the language.
I have searched enough
The problem I am having is, that each time, I write the code to handle the events in my App, it gets stopped. I have tried following step by step coding of the video from Treehouse tutorial of using the onClick and I have tried to follow the Android's official developer documents too. I have also looked into the codes at some sites (including and specially Stack Overflow).
All in vain
But each time I run the code, app works and when it has to execute the methods and functions, it gives me an alert saying App (Number Converter) stopped Unexpectedly. I am creating an app to convert the numbers from and to the 4 number systems, decimal, binary, octal and hexadecimal.
But, I am not able to get to the point why and why every time I write the code that is same, doesn't work and app stops.
Here is the code I am trying:
Button frmDeciToOct = (Button) findViewById(R.id.fromDecToOct);
/* here is the method that is the onClick */
frmDeciToBin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/* getting the edit text view */
EditText edtTxt = (EditText) findViewById(R.id.decimalValue);
/* to string... */
String value = edtTxt.getText().toString();
/* conversion to int, I saw this method on SO's answer */
int value2 = Integer.valueOf(value);
/* createBinOfDeci is a method, I will post its code after this */
String result = createBinOfDeci(value2);
/* get the text view */
TextView txtView = (TextView) findViewById(R.id.fromDeciRes);
/* set the text as a result */
txtView.setText(result);
}
});
Here is the code for my method createBinOfDeci():
public String createBinOfDeci (int deci) {
String result = "";
result = Integer.toBinaryString(deci);
return result;
}
The only thing that I am getting is that, I am using Windows and it gets crashed due to less RAM amount here! I am truly out of guesses and answers. Any help would be appreciated.
As #CommonsWare suggested, to check the LogCat, and I saw that it showed me a few errors.
Which was a clear sign of saying: The values are not correctly matching up.
Then I came up to the point, that the values weren't actually filled. They were empty, and I was running the code on a null hehe.
I just added the value and it worked!
Thanks guys (for the LogCat suggestion).

Categories

Resources