Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ecv.socketexample;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.ecv.socketexample.socket.ConnectionService;
- import org.json.JSONObject;
- public class MainActivity extends AppCompatActivity {
- /**
- * Listener
- */
- ConnectionService.SocketListener mListener = new ConnectionService.SocketListener() {
- @Override
- public void onDisconnected() {
- startReconnectionIfNeed();
- }
- @Override
- public void onReceivedData(String data) {
- if(data.length() > 0) {
- try {
- Log.d("LogTest", "Received data: " + data);
- final JSONObject json = new JSONObject(data);
- Log.d("LogTest", "Received JSON:" + json);
- if(!json.isNull("messageType")) {
- Log.d("LogTest", "JSON have messgae type: " + json.getString("messageType"));
- final String type = json.getString("messageType");
- if(type.equals("logout")) {
- Log.d("LogTest", "JSON messgae type is logout");
- final String message = json.getString("message");
- //
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- logout(message);
- }
- });
- }
- }
- } catch (Exception e) {
- }
- }
- }
- };
- /**
- * Service connection
- */
- ServiceConnection mServiceConnection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
- mServiceBound = true;
- mBinder = (ConnectionService.ConnectionBinder) iBinder;
- mBinder.getService().setSocketListener(mListener);
- startReconnectionIfNeed();
- }
- @Override
- public void onServiceDisconnected(ComponentName componentName) {
- mServiceBound = false;
- }
- };
- //
- private boolean mServiceBound = false;
- private ConnectionService.ConnectionBinder mBinder;
- private TextView mTextViewState;
- /**
- * In Create
- *
- * @param savedInstanceState
- */
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //
- mTextViewState = (TextView) findViewById(R.id.txt_state);
- }
- /**
- * Start login if need
- */
- void startReconnectionIfNeed() {
- // Not connected on service
- if(mBinder == null) return;
- //
- if(!mBinder.getService().isConnected()) {
- SharedPreferences prefs = getSharedPreferences("login", MODE_PRIVATE);
- String id = prefs.getString("accountId", "");
- String imei = prefs.getString("accountIMEI", "");
- String pass = prefs.getString("accountPass", "");
- //
- mTextViewState.setText("Socket State: Connecting");
- //
- mBinder.getService().startReconnection(id, imei, pass, new ConnectionService.RequestResult() {
- /**
- * On result
- *
- * @param result
- * @param message
- */
- @Override
- public void onResult(final int result, final String message) {
- if(result == -1) {
- logout("The section has expired.");
- } else {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- if(result == 1) {
- mTextViewState.setText("Socket State: Connected");
- } else {
- mTextViewState.setText("Socket State: Error");
- Log.d("LogTest", "Error on login in MainActivity: " + message);
- }
- }
- });
- }
- }
- });
- } else {
- mTextViewState.setText("Socket State: Connected");
- }
- }
- /**
- * Logout
- *
- */
- void logout(final String message) {
- if(mBinder != null) {
- //
- Log.d("LogTest", "Logout");
- //
- mBinder.getService().logout();
- mBinder.getService().setSocketListener(null);
- // Save logout info
- SharedPreferences.Editor prefs = getSharedPreferences("login", MODE_PRIVATE).edit();
- prefs.putBoolean("account", false);
- prefs.commit();
- //
- Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
- startActivity(new Intent(this, LoginActivity.class));
- finish();
- }
- }
- /**
- * On Start
- */
- @Override
- protected void onStart() {
- super.onStart();
- // Bind to service
- Intent intent = new Intent(this, ConnectionService.class);
- bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
- }
- /**
- * On Stop
- */
- @Override
- protected void onStop() {
- super.onStop();
- // Unbind from the service
- if (mServiceBound) {
- unbindService(mServiceConnection);
- mServiceBound = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement