Function into fragment stop refreshing after change tab - java

I have an app with 2 tabs where the first include somes values which are refreshed during the time. I take these values from a PhoneStateListener activity and I transfer them into the fragment with the function below. The problem is, when I move to 2nd tab and after that move back to first the values are not refresed anymore.
public void test(int LteSignalStrength,int LteRsrp,int LteRsrq,int LteRssnr,int LteCqi){
if(ntype=="\nNetwork Type: LTE\n"){
tv2.setText("\nLteSignalStrength:"+dbm+" dbm"+
"\nLteRsrp: "+LteRsrp+
"\nLteRsrq: "+LteRsrq+
"\nLteRssnr: "+LteRssnr);
}
}
PhonestateListener Activity
public Tab1Signal(Tab1Values fragment){
mcontext = fragment.getContext();
main=fragment;
}
public void onSignalStrengthsChanged(SignalStrength signalStrength){
super.onSignalStrengthsChanged(signalStrength);
try {
Method[] methods = android.telephony.SignalStrength.class.getMethods();
for (Method mthd : methods) {
if (mthd.getName().equals("getLteSignalStrength")){
//val1=mthd.getName() ;
LteSignalStrength=(Integer)mthd.invoke(signalStrength);
//main.test(val2);
}
if (mthd.getName().equals("getLteRsrp")){
LteRsrp=(Integer)mthd.invoke(signalStrength);
}
if (mthd.getName().equals("getLteRsrq")){
LteRsrq=(Integer)mthd.invoke(signalStrength);
}
if (mthd.getName().equals("getLteRssnr")){
LteRssnr=(Integer)mthd.invoke(signalStrength);
}
if (mthd.getName().equals("getLteCqi")){
LteCqi=(Integer)mthd.invoke(signalStrength);
}
main.test(LteSignalStrength,LteRsrp,LteRsrq,LteRssnr,LteCqi);
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
Does anyone know how can I solve it?

You can pass listener in method onTabChanged when tab contain Tab1Signal

Related

Jlabel.setText() only update at the end of the code

I have a problem, when I try to set the text of my JLabel it only changes after the code ends...
Here's my code:
public static void start(String username, String aToken, String uuid) {
SUpdate su = new SUpdate(...));
// Some parameters here...
LauncherPanel.title.setText("Mise à jour du jeu...");
try {
su.start();
} catch (BadServerResponseException e) {
e.printStackTrace();
} catch (ServerDisabledException e) {
e.printStackTrace();
} catch (BadServerVersionException e) {
e.printStackTrace();
} catch (ServerMissingSomethingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
If you need more infos, just ask me
You need to call the repaint() method of your JLabel, like LauncherPanel.title.repaint().

unable to get metadate title on hebrew (get it on gibberish)

I'm developing android applications
When doing a code to get streaming title "now loading" i unable to recieve the title on hebrew
but i recieved him on gibberish
if someone can help me with this i will be a greatful
enter image description here
#Override
protected IcyStreamMeta doInBackground(URL... urls)
{
try
{
streamMeta.refreshMeta();
Log.e("Retrieving MetaData","Refreshed Metadata");
}
catch (IOException e)
{
Log.e(MetadataTask2.class.toString(), e.getMessage());
}
return streamMeta;
}
#Override
protected void onPostExecute(IcyStreamMeta result)
{
try
{
title_artist=streamMeta.getTitle();
Log.e("Retrieved title_artist", title_artist);
if(title_artist.length()>0)
{
textView.setText(title_artist);
}
}
catch (IOException e)
{
Log.e(MetadataTask2.class.toString(), e.getMessage());
}
}
}
class MyTimerTask extends TimerTask {
public void run() {
try {
streamMeta.refreshMeta();
} catch (IOException e) {
e.printStackTrace();
}
try {
String title_artist=streamMeta.getTitle();
Log.i("ARTIST TITLE", title_artist);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
It looks like IcyMetaData simply casts raw bytes to char (effectively doing ISO-8859-1 encoding instead of using detecting whatever the server sends) at line 149:
metaData.append((char) b);
I don't see a way to fix this without patching/fixing the IcyMetaData class.

Refactoring duplication with two different return types in java

Say I have these lines of code in all of my controllers:
public View ControllerClass() {
// ...
// some code in controller
SomeClass someClass;
try {
someClass = Util.getParam(
context.getEncryptedParam(), soemthignElse.getSomething());
} catch (SomeException ex) {
log.error(ex);
return viewBuilderFactory.view1.view();
} catch (AnotherException ex) {
return viewBuilderFactory.view2.view();
} catch (etc ...) {}
// use someClass
// ...
return viewBuilderFactory.view3.view();
}
In this case I'd have two different return types (void and view) if I want to move the duplication to its own method. What'd be a good approach here?
Your code is best restructured as follows:
public View ControllerClass() {
ViewBuilderFactoryView viewBuilderFactoryView;
try {
SomeClass someClass = Util.getParam(
context.getEncryptedParam(), soemthignElse.getSomething());
// use someClass
// ...
viewBuilderFactoryView = viewBuilderFactory.view3;
} catch (SomeException ex) {
log.error(ex);
viewBuilderFactoryView = viewBuilderFactory.view1;
} catch (AnotherException ex) {
viewBuilderFactoryView = viewBuilderFactory.view2;
} catch (etc ...) {}
return viewBuilderFactoryView.view();
}
In other words, if you successfully obtain a SomeClass, go ahead and use it, and afterwards return some View. If you do not successfully obtain a SomeClass, then just return some View.

How to execute a method in onDestroy?

I'm writing an android app using a server. When the user exit the activity (remove it from recent activities) I want to close the connection and do some modifications in the server side before destroying the activity. So I read about the activity life cycle and I found out that I need to write the last call for closing the connections in onDestroy(). So that what I did:
Main activity:
#Override
public void onDestroy()
{
super.onDestroy();
try {
ConnectionHandler.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Connection Handler:
public static void close() throws IOException {
try {
JSONObject json = new JSONObject();
json.put("client", "close");
mConnectionHandler.new AsyncSendToServer().execute(json.toString());
} catch (JSONException e) {
e.printStackTrace();
}
socket.close();
}
The message transfer is working, but the activity does not execute the ConnectionHandler.close() method. What should I do to execute this method when the user close the activity?
You have to write the super.onDestroy after your code.
Try:
#Override
public void onDestroy()
{
try {
ConnectionHandler.close();
} catch (IOException e) {
e.printStackTrace();
}
super.onDestroy();
}

How to prevent JFrame from closing

I have a Java GUI application from which another java GUI application is invoked using reflection and loading. It works fine the only problem faced is, on closing the JFrame of invoked application the Main GUI application frame also closes. How can I prevent the main application (frame) from closing??
I cannot change the defaultCloseOperation of the invoked application, However a change to the main application can be made. Does it have any thing to do with threads??
This is my applications code that executes a target application
public class ClassExecutor{
private ClassLoaderOfExtClass classLoader;
private byte[][] ArrayOfClasses;
private String[] ArrayOfBinaryNames;
#SuppressWarnings("rawtypes")
private ArrayList<Class> loadedClasses;
private ArrayList<String> loadedClasesNames;
private Object[] parameters;
#SuppressWarnings("rawtypes")
public ClassExecutor() {
classLoader = new ClassLoaderOfExtClass();
new ArrayList<Class>();
loadedClasses = new ArrayList<Class>();
loadedClasesNames = new ArrayList<String>();
}
#SuppressWarnings("unchecked")
public void execute(File[] file, String[] binaryPaths) {
Object[] actuals = { new String[] { "" } };
Method m = null;
try {
Field classesx=ClassLoaderOfExtClass.class.getDeclaredField("classes");
classesx.setAccessible(true);
} catch (SecurityException e1) {
e1.printStackTrace();
} catch (NoSuchFieldException e1) {
e1.printStackTrace();
}
/*for (int i = 0; i < file.length; i++) {
for (int j = 0; j < file.length; j++) {
try {
#SuppressWarnings("rawtypes")
Class c = classLoader.loadClassCustom(file[i], binaryPaths[i]);
//Fied classex=classLoader.getResource("classes");
}catch(Exception e){
}
}
}
Class<?>[]classesxx= getLoadedClasses(classLoader);
System.out.println("Loaded classes have size "+ classesxx.length);*/
for (int i = 0; i < file.length; i++) {
try {
#SuppressWarnings("rawtypes")
Class c = classLoader.loadClassCustom(file[i], binaryPaths[i]);
try {
if (c.getMethod("main", new Class[] { String[].class }) != null) {
m = c.getMethod("main", new Class[] { String[].class });
} else {
System.out.println("This class does not contain main");
continue;
}
} catch (NoSuchMethodException e) {
// System.out.println("Main not found!!!");
// System.out.println("M here");
// e.printStackTrace(); // not printing stack trace
} catch (SecurityException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
System.out.println("No such class definition exist!!");
// TODO Auto-generated catch block
// e.printStackTrace();
}
}
try {
m.invoke(null, actuals);
// CallStack.print();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#SuppressWarnings({ "unchecked", "rawtypes" })
public void execute(ArrayList<byte[]> stuffedFiles,
ArrayList<String> binaryPaths) {
convertToArray(stuffedFiles, binaryPaths);
loadAllClasses(ArrayOfClasses, ArrayOfBinaryNames);
Object[] actuals = { new String[] { "" } };
Method m = null;
/*
* Method[] m1= new Method[10]; for (Class c : loadedClasses) {
* m1=c.getMethods(); } for(Method m2: m1){
* System.out.println(m2.getName()); }
*/
/* System.out.println(loadedClasses.size()); */
for (Class c : loadedClasses) {
/*
* System.out.println(c.toString());
* System.out.println(c.getConstructors());
*/
// for (int i = 1; i < file.size(); i++) {
/*
* for(Method meth : c.getMethods()){ meth.setAccessible(true);
*
* }
*/
try {
if (c.getMethod("main", new Class[] { String[].class }) != null) {
m = c.getMethod("main", new Class[] { String[].class });
break;
} else {
// System.out.println("This class does not contain main");
continue;
}
} catch (NoSuchMethodException e) {
System.out.println("Program does not contain main");
} catch (SecurityException e) {
e.printStackTrace();
}
}
try {
if(parameters==null){
m.invoke(null, actuals);
}
else{
try {
System.out.println("It Fails Here");
m.invoke(null, parameters);
} catch (Exception e) {
System.out.println("Illegal arguments");
}
}
// CallStack.print();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You'll want to use the DISPOSE_ON_CLOSE operation, so it would be setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
EXIT_ON_CLOSE would be the option that closes all windows which I believe is what you are currently experiencing.
You have the following options for the defaultCloseOperation:
DO_NOTHING_ON_CLOSE - The do-nothing default window close operation;
HIDE_ON_CLOSE - The hide-window default window close operation;
DISPOSE_ON_CLOSE - The dispose-window default window close operation.
EXIT_ON_CLOSE - The exit application default window close operation. Attempting to set this on Windows that support this, such as JFrame, may throw a SecurityException based on the SecurityManager. It is recommended you only use this in an application.
The Option DISPOSE_ON_CLOSE could be used in order to avoid to close all windows, closing just the one you want.
If you don't have direct access to JFrame object as you have with the last posted code, you could use Window.getWindows() in order to receive all windows instance (as JFrame is a Window too it will be listed too). And then set the defaultCloseOperation on that.
Possibly you will need to use threads because the defaultCloseOperation needs to be set after invoke main method.
Theoretically it works, so I think this is a good shot ;)
I am not allowed to make changes to the application being invoked.
That was a comment in reply to #JeffLaJoie just to clarify, it would not require any changes to the code of the other app., just an extra method call or two by your app. at run-time to set the close operation of the 3rd party frame.
Failing that, the best solution I can think of is to start the new frame in a separate Process that starts a new JVM, when the user closes the other app., it and the 2nd JVM will end, while leaving the original app. on-screen.

Categories

Resources