Instance variable in another class in Android - java

I seems having an issue where an instance variable is not updated in MainAtivity. the text still show "testing" instead of the message received by onMessage()
public class MainActivity extends AppCompatActivity {
pacioWebSocketListener myWS = new pacioWebSocketListener();
private String pacioMsg = myWS.getPacioMsg();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
TextView tv = findViewById(R.id.textView);
tv.setText("Message: " + pacioMsg);
And here is the pacioWebsocketListener.java
public class pacioWebSocketListener extends WebSocketListener {
private static final int NORMAL_CLOSURE_STATUS = 1000;
WebSocket ws;
public String pacioMsg = "testing";
public void setPacioMsg(String paciomsg){
pacioMsg = paciomsg;
}
public String getPacioMsg(){
return pacioMsg;
}
#Override
public void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response);
Log.v(TAG,"onOpen");
}
#Override
public void onMessage(WebSocket webSocket, String message) {
super.onMessage(webSocket, message);
setPacioMsg(message);

In your activity, you are getting the value of pacioMsg before its value is changed by the method onMessage. onMessage is not called when you instantiate pacioWebSocketListener, it's only called in the event of a new message
So, If you want your activity to know every time a message arrives and update its textview, you need to find a way to make your activity and your class pacioWebSocketListener communicate.
You can do it by creating an Interface Listener like this:
in your class pacioWebSocketListener, create an interface
public class pacioWebSocketListener extends WebSocketListener {
MessageListener msgListener;
private static final int NORMAL_CLOSURE_STATUS = 1000;
WebSocket ws;
public pacioWebSocketListener(MessageListener listener){
this.msgListener = listener;
}
// interface
public interface MessageListener{
void onMessageReceived(String message)
}
#Override
public void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response);
Log.v(TAG,"onOpen");
}
#Override
public void onMessage(WebSocket webSocket, String message) {
super.onMessage(webSocket, message);
// When a new Message arrives , call the MessageListener.onMessageReceived
msgListener.onMessageReceived(message);
}
Now, in your activity implements the interface created in the pacioWebSocketListener class and override the method onMessageReceived, it goes like this
public class MainActivity extends AppCompatActivity implements pacioWebSocketListener.MessageListener {
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// Pass "this" in the constructor, to show that this activity is
// Listening for changes
pacioWebSocketListener myWS = new pacioWebSocketListener(this);
tv = findViewById(R.id.textView);
}
// Override the method in MessageListener Inteface
public void onMessageReceived(String message){
// Then from here you can update your UI
tv.setText("Message: " + message);
}
Now Every time a new message arrives, your TextView will be updated with the text

Related

How to pend flutter method channel callback (android native java code) for an android listener

I have a flutter app that run a java code in some situations.
I wrote java code in method call handler in MethodCahnnel.
In this callback I call another method that is communicate with a serial port using usb-serial-for-android and wait for data in onNewData listener. I want to send back this data to flutter. and I am using a class variable for it and fill it in onNewData method. and use result.success conditionaly when the variable is not empty! but result.success never called and if I delete the if statement, result.success called when the variable is empty.
Here is the section of my code:
public class MainActivity extends FlutterActivity implements SerialInputOutputManager.Listener {
private static final String CHANNEL = "channel";
private UsbSerialPort connectionPort;
private String response = "";
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void configureFlutterEngine(#NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler(
(call, result) -> {
select();
if (!response.isEmpty()) result.success(response);
}
);
}
#Override
protected void onNewIntent(Intent intent) {
if (intent.getAction().equals("android.hardware.usb.action.USB_DEVICE_ATTACHED")) {
Toast.makeText(this, "new usb device detected!", Toast.LENGTH_SHORT).show();
}
super.onNewIntent(intent);
}
private void select() {
connectionPort.write(...);
}
#Override
public void onNewData(byte[] data) {
response = Utils.byteArrayToHexString(data);
}
#Override
public void onRunError(Exception e) {
status("error onRunError" + e.getMessage());
}
}

how to send a string between classes?

hello guys I would like to sending string between java class in android studio.
I have class CreateToken.java and MainActivity.java, how can I send String yourToken to MainActivity.java and how can i receive string yourToken in MainActivity.java, and the result of yourToken is com.example.user.application.CreateToken#yourToken but yourToken is not full token , its just 7 charecter.
this is one of my function in CreateToken.java :
public class CreateToken {
private ICreateToken listener;
public CreateToken(ICreateToken listener) {
this.listener = listener;
}
public Call<Token> api(final Context ctx){
ApiInterface api = ApiClient.getClient().create(ApiInterface.class);
String usernameApi = "web";
String passwordApi = "123";
Call<Token> getToken = api.postWebService(usernameApi,passwordApi);
getToken.enqueue(new Callback<Token>() {
#Override
public void onResponse(Call<Token> call, Response<Token> response) {
String error = response.body().getError();
if (error.equals("false")){
Toast.makeText(ctx, response.body().getToken(),Toast.LENGTH_SHORT).show();
Log.d("Smart","Response : Token Show");
String yourToken = response.body().getToken();
listener.onTokenGenerated(yourToken);
}else {
Toast.makeText(ctx, response.body().getMessage(),Toast.LENGTH_SHORT).show();
Log.d("Smart","Response : Token NUll");
}
}
#Override
public void onFailure(Call<Token> call, Throwable t) {
Log.d("Smart","Response : Token Null");
}
});
return getToken;
}
public interface ICreateToken {
void onTokenGenerated(String token);
}
}
and this is my MainActivity.java :
public class MainActivity extends AppCompatActivity implements CreateToken.ICreateToken {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
CreateToken token = new CreateToken(MainActivity.this);
textView.setText(token.toString());
}
#Override
public void onTokenGenerated(String token) {
}
}
I think that you could use the AsyncTask class from Android framework:
https://developer.android.com/reference/android/os/AsyncTask
Then use the methods doInBackground to call the webservice and onPostExecute use the response from that call:
public ActivityExample extends AsyncTask <clazz1,clazz2,clazz3> {
doInBackGround(clazz1 clazz){
return result;
}
onPostExecute(clazz2 result){
}
}
create an interface ICreateToken within CreateToken class as below
public interface ICreateToken {
void onTokenGenerated(String token);
}
Also declare Interface field in CreateToken class
private ICreateToken listener;
and from your MainActivity pass context in CreateToken class like this
CreateToken token = new CreateToken(MainActivity.this);
then initialise the listener in CreateToken constructor
public CreateToken(ICreateToken listener) {
this.listener = listner;
}
finally from onResponse you can return token via
listener.onTokenGenerated(yourToken)
Last and most important
MainActivity extends AppCompatActivity implements ICreateToken
implement ICreateToken in MainActivity which will ask to implement onTokenGenerated in MainActivity there you'll receive your token.

AsyncTask data communication with nested Classes

I have a specific scenario and I need your help.
I'm trying to build an App in Android that involves network communication.
I am using AsyncTask for the http POST requests.
I have another class called Proxy (not a good one.. will be changed) which holds different kinds of functionalities (registerUser, setUserName, getUserPermission...)
And Of course, I have an Activity.
My Activity holds an instance of Proxy class.
My goal, is to push a button in the activity, it will call a method from Proxy class, which in its turn calls the AsyncTask's execute() method that actually run the http POST.
I was wondering how to get the data from AsyncTask's onPostExecute to my activity.
What I have in mind is to have an interface in AsyncTask, which will be implemented in Proxy class, and another interface in Proxy class which will be implemented in my Activity class.
Roll the data all the way to my Activity.
I want to hear your thoughts about whether this is the way to go, or another approach is preffered.
Thanks a lot for your help.
Adding some code
public class RegisterActivity extends FragmentActivity implements Proxy.OnProxyHttpPostResponseListener {
private Proxy proxy;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
this.proxy = new Proxy();
this.proxy.setHttpPostResponseListener(this);
}
#Override
public void onProxyHttpPostResponse(String response) {
//Do something when http post returns
}
}
public class Proxy {
public interface OnProxyHttpPostResponseListener {
void onProxyHttpPostResponse(String response);
}
private OnProxyHttpPostResponseListener httpPostResponseListener;
public void setHttpPostResponseListener(OnProxyHttpPostResponseListener listener) {
this.httpPostResponseListener = listener;
}
private class HttpPostAsync extends AsyncTask<Pair<String, ArrayList<Pair<String, String>>>, Void, String> {
#Override
protected String doInBackground(Pair<String, ArrayList<Pair<String, String>>>... params) {
return this.httpPost(params[0].first, params[0].second);
}
protected void onPostExecute(String response) {
httpPostResponseListener.onProxyHttpPostResponse(response);
}
}
If you're just needing HTTP POST functionality then an AsyncTask might not be the best choice. AsyncTask really shines if you need to get progress updates as the task is executing (with onProgressUpdate(Progress... progress)). If you'd like to use AsyncTask nonetheless, iroiroys' reply should help.
A bit more simply, you could just use a Handler thread straight up. Something like this:
public class HandlerExampleActivity extends Activity {
private Button postButton;
private Button getButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_handler_example);
backgroundThread = new BackgroundThread();
backgroundThread.start();
postButton = (Button) findViewById(R.id.button_post);
postbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
backgroundThread.post("DATA_HERE");
}
});
getButton = (Button) findViewById(R.id.button_get);
getbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
backgroundThread.get("URL_HERE");
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
backgroundThread.exit();
}
private class BackgroundThread extends Thread {
private Handler backgroundHandler;
public void run() {
Looper.prepare();
backgroundHandler = new Handler();
Looper.loop();
}
public void post(DataType data) {
backgroundHandler.post(new Runnable() {
#Override
public void run() {
// pull data and do the POST
uiMsg = uiHandler.obtainMessage(POST_COMPLETE, whatever_data_passing_back, 0, null);
uiHandler.sendMessage(uiMsg);
}
});
}
public void get(URL data) {
backgroundHandler.post(new Runnable() {
#Override
public void run() {
// GET data
uiMsg = uiHandler.obtainMessage(GET_COMPLETE, whatever_data_passing_back, 0, null);
uiHandler.sendMessage(uiMsg);
}
});
}
public void exit() {
backgroundHandler.getLooper().quit();
}
}
private final Handler uiHandler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what) {
case POST_COMPLETE:
// handle it
break;
case GET_COMPLETE:
// handle it
break
case MESSAGE_BACK_TO_UI_THREAD:
// do something
break;
case OPERATION_FAIL:
// oh no!
break;
case OPERATION_SUCCESS:
// yay!
break;
}
}
};
}
I suggest you try Handler and Handler.Callback.
Below I made it simple example..
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
public class MainActivity extends Activity implements Callback {
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler(this);
Proxy proxy = new Proxy(handler);
proxy.foo();
}
private class Proxy {
Handler handler;
public Proxy(Handler handler) {
this.handler = handler;
}
private void foo() {
new myAsync().execute();
}
private class myAsync extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Message msg = handler.obtainMessage();
msg.obj = result;
handler.sendMessage(msg);
}
}
}
#Override
public boolean handleMessage(Message msg) {
// Handle Message here!
return false;
}
}

Calling activity from inside a background thread

I have an acitivity with three fragments is attched on it and there is a class that is responsible for my tcp connection in activity. TCP class works as async. Inside onCreate method of main activity I am starting the tcp connection. Then when I click a button I am starting a new activity and getting the current tcp connection in new activity using a singleton class. I can send messages from the new activity to server using the available tcp. However it is an asyc task so I can't do changes in new activity according to the message that is received from server.
How can I change the layout of new activity from async task?
//Activity code:
public class MainScreen extends FragmentActivity implements ActionBar.TabListener {
TCPClient mTcpClient;
connectTask cnnTask;
///The class is responsible for tcp connection
public class connectTask extends AsyncTask<String, String, TCPClient> {
#Override
public TCPClient doInBackground(String... message) {
//we create a TCPClient object
mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
#Override
//here the messageReceived method is implemented
public void messageReceived(String message) {
//do process
}
}
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences preferences = null;
super.onCreate(savedInstanceState);
try {
cnnTask = new connectTask();
preferences = this.getSharedPreferences("MyPreferences",
Context.MODE_PRIVATE);
mTcpClient.SERVERIP = preferences.getString("IPAddress", "0");
mTcpClient.SERVERPORT = Integer.parseInt(preferences.getString("Port", "13759"));
cnnTask.execute("");
} catch (Exception e) {
}
//..
SingletonTCP .getInstance().setmTCPClient(mTcpClient);
}
//Fragment code:
public class FragmentDesign extends Fragment implements View.OnClickListener {
public void onClick(View v) {
String name = v.getTag().toString();
Intent intent = new Intent(getActivity(),NewActivity.class);
intent.putExtra("Name", this._name);
startActivity(intent);
}
}
//Singleton class to set and get the current TCPClient.
public class SingletonTCP {
private TCPClient mTCPClient;
public TCPClient getmTCPClient() { return mTCPClient; }
public void setmTCPClient(TCPClient mTCPClient) {this.mTCPClient = mTCPClient;}
private static final SingletonTCP holder = new SingletonTCP ();
public static SingletonTCP getInstance() {return holder;}
}
enter code here
//New activity code.
public class NewActivity extends Activity implements View.OnClickListener {
public class TCP extends AsyncTask<String, String, TCPClient> {
#Override
protected TCPClient doInBackground(String... params) {
mTCPClient = new TCPClient(new TCPClient.OnMessageReceived() {
#Override
public void messageReceived(String message) {
mes = message;
}
});
return null;
}
}
#Override
public void onClick(View v) {
String command = "<message>";
this.mTCPClient.sendMessage(command);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
mTCPClient = SingletonTCP .getInstance().getmTCPClient();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_activity);
Bundle extras = getIntent().getExtras();
departmanAdi = extras.getString("Name");
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
tcp = new TCP();
tcp.execute("");
}
}
You could call publishProgress() from inside doInBackground(), which will call the onProgressUpdate() on the UI thread. In onProgessUpdate(), you can access and make changes to your activity.
This is described in the documentation here.

My interface when triggered get NullPointerException

I try a Toast Message interface. If app not connection internet, I want show a Toast Message and I'm wanting java interfaces.
This is MotherActivity.java. This file implement ToastMessagges.ToastMessaggeCallback
public class MotherActivity extends ActionBarActivity implements ToastMessagges.ToastMessaggeCallback {
ToastMessagges toastMessagges;
#Override
protected void onStart() {
super.onStart();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mother);
toastMessagges = new ToastMessagges();
AppStarter();
}
private void AppStarter(){
boolean checkinternet = InternetControl.checkInternetConnection( getApplicationContext() );
if( checkinternet ) {
toastMessagges.show_toast_messagge();
}
else {
}
}
#Override
public void LongToastMessagge() {
Toast.makeText(getApplicationContext(), "Hello World", Toast.LENGTH_LONG).show();
}
}
This is my ToastMessagges.java file.
public class ToastMessagges {
ToastMessaggeCallback toastMessaggeCallback;
public void show_toast_messagge(){
toastMessaggeCallback.LongToastMessagge();
}
public static interface ToastMessaggeCallback {
public void LongToastMessagge();
}
}
When the start this app. I get NullPointerException error.
Caused by: java.lang.NullPointerException
at com.medyasef.bulenttirasnewapp.bulenttiras.functions.ToastMessagges.show_toast_messagge(ToastMessagges.java:22)
at com.medyasef.bulenttirasnewapp.bulenttiras.MotherActivity.AppStarter(MotherActivity.java:36)
at com.medyasef.bulenttirasnewapp.bulenttiras.MotherActivity.onCreate(MotherActivity.java:29)
ToastMessagges.java:22
toastMessaggeCallback.LongToastMessagge();
Sorry bad english.
Please help.
Thank you.
You haven't initialized you ToastMessaggeCallback toastMessaggeCallback.
To do this, write
ToastMessaggeCallback toastMessaggeCallback = new ToastMessaggeCallback(){
public void LongToastMessagge(){
// add some toasting code here
}
};
This will make an object implementing your interface (called "anonymous class"). Of course, your ToastMessaggeCallback should do something in the method LongToastMessagge, so add the desired code there.
I will recommend you to create a Util class instead of Interface. I'm here giving you an example of Util class.
public class Util {
public static void showToast(Context context, String text) {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
}
}
Then call the showToast() method from your activity as follows...
Util.showToast(YourActivity.this, "text");
Update:
Declare your Interface as a individual, not inside a class as below...
public interface ToastMessaggeCallback {
public void showLongToastMessagge(String text);
}
Then implement the Interface as follows...
public class MotherActivity extends ActionBarActivity implements ToastMessaggeCallback {
#Override
protected void onStart() {
super.onStart();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mother);
AppStarter();
}
private void AppStarter(){
boolean checkinternet = InternetControl.checkInternetConnection( getApplicationContext() );
if( checkinternet ) {
showLongToastMessagge("Hello World");
}
else {
}
}
#Override
public void showLongToastMessagge(String text) {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
}
}
Your ToastMessagges class needs to provide a method to register the callback. Then, your Activity needs to call this method to register itself as the callback, right after you construct the ToastMessages object.

Categories

Resources