in the onCreate of activit-main i make an inistance of a class named XMPPConectio at the constructor of the XMMPPConnection i take inistance of the activity-main and therefor i execute an Asychtask. in the progressUpdate i call triger function of activity-main. i the triger function i want to send inistance of XMPPconnectio to other activity by intent.putExtra but i get the error
Parcelable encountered IOException writing serializable object
the aim of doing all these is to have connection (when it is connected) to other activity.
please give me some sample code thank you
XMPPConectio class which implements Serializable:
public class XMPPConnectio implements Serializable {
XMPPTCPConnection connection;
String connectionMessages="";
connectionXMPP connectionXMPPAsynch;
MainActivity mainActivity;
public XMPPTCPConnection getXMPPConnectio ()
{
return connection;
}
public XMPPConnectio(MainActivity mainActivity)
{
this.mainActivity=mainActivity;
try
{
connectionXMPPAsynch =new connectionXMPP();
connectionXMPPAsynch.execute();
}
catch (Exception e)
{
}
}
class connectionXMPP extends AsyncTask<String,Void,String>
{
@Override
protected String doInBackground(String... params) {
connection = new XMPPTCPConnection(XMPPTCPConnectionConfiguration.builder()
.setServiceName("192.168.1.6")
.setUsernameAndPassword("ehsan", "123")
.setPort(9090)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setDebuggerEnabled(true)
.setCompressionEnabled(false).build());
connection.setUseStreamManagement(true);
connection.addConnectionListener(new ConnectionListener()
{
@Override
public void connected(XMPPConnection connection) {
Log.d("connected", "yes connected successfully : ");
publishProgress();
}
@Override
public void authenticated(XMPPConnection connection, boolean resumed) {
Log.d("connected","yes authenticated successfully : ");
}
@Override
public void connectionClosed() {
Log.d("connected","yes connectionClosed successfully : ");
}
@Override
public void connectionClosedOnError(Exception e) {
Log.d("connected","yes connectionClosedOnError : ");
}
@Override
public void reconnectionSuccessful() {
Log.d("connected","yes reconnection successfully : ");
}
@Override
public void reconnectingIn(int seconds) {
Log.d("connected","yes reconnectingIn : ");
}
@Override
public void reconnectionFailed(Exception e) {
Log.d("connected","yes reconnectionFailed : ");
}
});
connect();
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
mainActivity.triger();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("","onPostExecute");
}
private void connect()
{
try {
connection.connect();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
}
}
}
the Activity which make instance of XMPPConectio and cause Asychtask execution
package passargad.ehsan;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.Socket;
public class MainActivity extends ActionBarActivity implements Serializable {
private Socket socket;
private String serverIpAddress = "192.168.1.6";
XMPPConnectio xmppConnectio;
XMPPTCPConnection connection;
private static final int REDIRECTED_SERVERPORT = 6789;
FastFood fastFood;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent=new Intent(this,SocketService.class);
startService(intent);
fastFood =new FastFood(this);
xmppConnectio=new XMPPConnectio(this);
}
// this is the function which is called (when connection is done ) by //onProgressUpdate of Asychtask
public void triger()
{
try {
Intent intent= new Intent(this,chat.class);
intent.putExtra("XMPP",xmppConnectio);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
this is the activity which i want to have XMPPConnectio in there but the execution never reach to this
package passargad.ehsan;
import android.app.Application;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import java.io.IOException;
public class chat extends ActionBarActivity {
XMPPConnectio xmppConnectio;
XMPPTCPConnection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
xmppConnectio=(XMPPConnectio)getIntent().getSerializableExtra("XMPP");
Log.d("","done");
}
}
i tried to make the question crystal clear by giving all the code. as i said the goal is to have the connection in all activities when connection is connected. thank you .