Receive Incoming Calls Within One Hour Only Using Twilio

I implemented Twilio in my Android app for outgoing and incoming calls. But I ran into a problem while receiving an incoming call with a background service. The problem is that I only receive calls in the first 30-40 minutes. After a while, the phone stops receiving incoming calls. I've tried so much. Please answer me soon. I also use the code with you.

I get a token from a background service that generates a token after a period of time.

IncomingCallService.java

 public class IncomingCallService extends Service implements LoginListener, BasicConnectionListener, BasicDeviceListener, View.OnClickListener, CompoundButton.OnCheckedChangeListener, RadioGroup.OnCheckedChangeListener { private static Handler handler, handler_login; public IncomingPhone phone; SharedPreferences login_details; Vibrator vibrator; Ringtone r; Uri notification; public static final String LOGIN_DETAILS = "XXXXXXXX"; AudioManager am; Intent intent; public static String DEFAULT_CLIENT_NAME = "developer"; static String Twilio_id = "", INCOMING_AUTH_PHP_SCRIPT = MenuItems.INCOMING_AUTH_PHP_SCRIPT + MenuItems.Twilio_id; Runnable newrun; Activity context; Context ctx; static String op_id = ""; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { login_details = getSharedPreferences(LOGIN_DETAILS, Context.MODE_PRIVATE); if (login_details.contains("twilio_Id")) { Twilio_id = login_details.getString("twilio_Id", ""); } handler_login = new Handler(); handler_login.postDelayed(new Runnable() { @Override public void run() { Login(); handler_login.postDelayed(this, 20000); } }, 1000); } public void Login() { phone = IncomingPhone.getInstance(getApplicationContext()); phone.setListeners(this, this, this); phone.login(DEFAULT_CLIENT_NAME, true, true); } @Override public int onStartCommand(Intent intent, int flags, int startId) { this.intent = intent; // I know getIntent always return NULL in service if (intent != null) { op_id = intent.getStringExtra("operator_id"); onCallHandler(); } return START_STICKY; } public void onCallHandler() { handler = new Handler(); newrun = new Runnable() { @Override public void run() { handler.removeCallbacks(newrun); new IncomingTokenTask().execute(); if (phone.handleIncomingIntent(intent)) { } handler.postDelayed(this, 2000000); } }; handler.postDelayed(newrun, 8000000); } class IncomingTokenTask extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { } @Override protected Void doInBackground(Void... params) { IncomingPhone.capabilityToken = EntityUtils .toString(entity); } } 

BasicPhone twilio Class

 public class IncomingPhone implements DeviceListener, ConnectionListener { private static final String TAG = "IncomingPhone"; static String decodedString = ""; static String capabilityToken = ""; // TODO: change this to point to the script on your public server private static final String AUTH_PHP_SCRIPT = MenuItems.INCOMING_AUTH_PHP_SCRIPT+MenuItems.Twilio_id; public interface LoginListener { public void onLoginStarted(); public void onLoginFinished(); public void onLoginError(Exception error); } public interface BasicConnectionListener { public void onIncomingConnectionDisconnected(); public void onConnectionConnecting(); public void onConnectionConnected(); public void onConnectionFailedConnecting(Exception error); public void onConnectionDisconnecting(); public void onConnectionDisconnected(); public void onConnectionFailed(Exception error); } public interface BasicDeviceListener { public void onDeviceStartedListening(); public void onDeviceStoppedListening(Exception error); } private static IncomingPhone instance; public static final IncomingPhone getInstance(Context context) { if (instance == null) instance = new IncomingPhone(context); return instance; } private final Context context; private LoginListener loginListener; private BasicConnectionListener basicConnectionListener; private BasicDeviceListener basicDeviceListener; private static boolean twilioSdkInited; private static boolean twilioSdkInitInProgress; private boolean queuedConnect; private Device device; private Connection pendingIncomingConnection; private Connection connection; private boolean speakerEnabled; private String lastClientName; private boolean lastAllowOutgoing; private boolean lastAllowIncoming; private IncomingPhone(Context context) { this.context = context; } public void setListeners(LoginListener loginListener, BasicConnectionListener basicConnectionListener, BasicDeviceListener basicDeviceListener) { this.loginListener = loginListener; this.basicConnectionListener = basicConnectionListener; this.basicDeviceListener = basicDeviceListener; } private void obtainCapabilityToken(String clientName, boolean allowOutgoing, boolean allowIncoming) { StringBuilder url = new StringBuilder(); HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER; DefaultHttpClient httpclient = new DefaultHttpClient(); SchemeRegistry registry = new SchemeRegistry(); SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory(); socketFactory .setHostnameVerifier((X509HostnameVerifier) hostnameVerifier); registry.register(new Scheme("https", socketFactory, 443)); SingleClientConnManager mgr = new SingleClientConnManager( httpclient.getParams(), registry); @SuppressWarnings("unused") DefaultHttpClient httpClient = new DefaultHttpClient(mgr, httpclient.getParams()); // Set verifier HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier); url.append(AUTH_PHP_SCRIPT); // This runs asynchronously! new GetAuthTokenAsyncTask().execute(url.toString()); } private boolean isCapabilityTokenValid() { if (device == null || device.getCapabilities() == null) return false; long expTime = (Long) device.getCapabilities().get( Capability.EXPIRATION); return expTime - System.currentTimeMillis() / 1000 > 0; } // private void updateAudioRoute() { AudioManager audioManager = (AudioManager) context .getSystemService(Context.AUDIO_SERVICE); audioManager.setSpeakerphoneOn(speakerEnabled); } public void login(final String clientName, final boolean allowOutgoing, final boolean allowIncoming) { if (loginListener != null) loginListener.onLoginStarted(); this.lastClientName = clientName; this.lastAllowOutgoing = allowOutgoing; this.lastAllowIncoming = allowIncoming; if (!twilioSdkInited) { if (twilioSdkInitInProgress) return; twilioSdkInitInProgress = true; Twilio.setLogLevel(Log.DEBUG); Twilio.initialize(context, new Twilio.InitListener() { @Override public void onInitialized() { twilioSdkInited = true; twilioSdkInitInProgress = false; obtainCapabilityToken(clientName, allowOutgoing, allowIncoming); } @Override public void onError(Exception error) { twilioSdkInitInProgress = false; if (loginListener != null) loginListener.onLoginError(error); } }); } else { obtainCapabilityToken(clientName, allowOutgoing, allowIncoming); } } private void reallyLogin(final String capabilityToken) { try { if (device == null) { device = Twilio.createDevice(capabilityToken, this); Intent intent = new Intent(context, IncomingPhoneActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent.getActivity( context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); device.setIncomingIntent(pendingIntent); } else device.updateCapabilityToken(capabilityToken); if (loginListener != null) loginListener.onLoginFinished(); if (queuedConnect) { // If someone called connect() before we finished initializing // the SDK, let take care of that here. connect(null); queuedConnect = false; } } catch (Exception e) { if (device != null) device.release(); device = null; if (loginListener != null) loginListener.onLoginError(e); } } public void setSpeakerEnabled(boolean speakerEnabled) { if (speakerEnabled != this.speakerEnabled) { this.speakerEnabled = speakerEnabled; updateAudioRoute(); } } public void connect(Map<String, String> inParams) { if (twilioSdkInitInProgress) { // If someone calls connect() before the SDK is initialized, we'll // remember // that fact and try to connect later. queuedConnect = true; return; } if (!isCapabilityTokenValid()) login(lastClientName, lastAllowOutgoing, lastAllowIncoming); if (device == null) return; if (canMakeOutgoing()) { disconnect(); connection = device.connect(inParams, this); if (connection == null && basicConnectionListener != null) basicConnectionListener .onConnectionFailedConnecting(new Exception( "Couldn't create new connection")); } } public void disconnect() { IncomingPhoneActivity.incomingAlert = null; if (connection != null) { connection.disconnect(); // will null out in onDisconnected() if (basicConnectionListener != null) basicConnectionListener.onConnectionDisconnecting(); } } public void acceptConnection() { if (pendingIncomingConnection != null) { if (connection != null) disconnect(); pendingIncomingConnection.accept(); connection = pendingIncomingConnection; pendingIncomingConnection = null; } } public void connecta(String phoneNumber) { Toast.makeText(context, "Calling...", Toast.LENGTH_SHORT).show(); Map<String, String> parameters = new HashMap<String, String>(); parameters.put("group_id", "11"); // String capabilityToken; try { device = Twilio .createDevice(decodedString, this /* DeviceListener */); } catch (Exception e1) { e1.printStackTrace(); } try { device.disconnectAll(); } catch (Exception e) { e.printStackTrace(); } connection = device.connect(parameters, this); if (connection == null) { Log.w(TAG, "Failed to create new connection"); } } public void ignoreIncomingConnection() { if (pendingIncomingConnection != null) { pendingIncomingConnection.ignore(); } } public boolean isConnected() { return connection != null && connection.getState() == Connection.State.CONNECTED; } public Connection.State getConnectionState() { return connection != null ? connection.getState() : Connection.State.DISCONNECTED; } public boolean hasPendingConnection() { return pendingIncomingConnection != null; } public boolean handleIncomingIntent(Intent intent) { Device inDevice = intent.getParcelableExtra(Device.EXTRA_DEVICE); Connection inConnection = intent .getParcelableExtra(Device.EXTRA_CONNECTION); if (inDevice == null && inConnection == null) return false; intent.removeExtra(Device.EXTRA_DEVICE); intent.removeExtra(Device.EXTRA_CONNECTION); if (pendingIncomingConnection != null) { Log.i(TAG, "A pending connection already exists"); inConnection.ignore(); return false; } pendingIncomingConnection = inConnection; pendingIncomingConnection.setConnectionListener(this); return true; } public boolean canMakeOutgoing() { if (device == null) return false; Map<Capability, Object> caps = device.getCapabilities(); return caps.containsKey(Capability.OUTGOING) && (Boolean) caps.get(Capability.OUTGOING); } public boolean canAcceptIncoming() { if (device == null) return false; Map<Capability, Object> caps = device.getCapabilities(); return caps.containsKey(Capability.INCOMING) && (Boolean) caps.get(Capability.INCOMING); } public void setCallMuted(boolean isMuted) { if (connection != null) { connection.setMuted(isMuted); } } @Override /* DeviceListener */ public void onStartListening(Device inDevice) { if (basicDeviceListener != null) basicDeviceListener.onDeviceStartedListening(); } @Override /* DeviceListener */ public void onStopListening(Device inDevice) { if (basicDeviceListener != null) basicDeviceListener.onDeviceStoppedListening(null); } @Override /* DeviceListener */ public void onStopListening(Device inDevice, int inErrorCode, String inErrorMessage) { if (basicDeviceListener != null) basicDeviceListener.onDeviceStoppedListening(new Exception( inErrorMessage)); } @Override /* DeviceListener */ public boolean receivePresenceEvents(Device inDevice) { return false; } @Override /* DeviceListener */ public void onPresenceChanged(Device inDevice, PresenceEvent inPresenceEvent) { } @Override /* ConnectionListener */ public void onConnecting(Connection inConnection) { if (basicConnectionListener != null) basicConnectionListener.onConnectionConnecting(); } @Override /* ConnectionListener */ public void onConnected(Connection inConnection) { updateAudioRoute(); if (basicConnectionListener != null) basicConnectionListener.onConnectionConnected(); } @Override /* ConnectionListener */ public void onDisconnected(Connection inConnection) { if (inConnection == connection) { connection = null; if (basicConnectionListener != null) basicConnectionListener.onConnectionDisconnected(); } else if (inConnection == pendingIncomingConnection) { pendingIncomingConnection = null; if (basicConnectionListener != null) basicConnectionListener.onIncomingConnectionDisconnected(); } } @Override /* ConnectionListener */ public void onDisconnected(Connection inConnection, int inErrorCode, String inErrorMessage) { if (inConnection == connection) { connection = null; if (basicConnectionListener != null) basicConnectionListener .onConnectionFailedConnecting(new Exception( inErrorMessage)); } } private class GetAuthTokenAsyncTask extends AsyncTask<String, Void, String> { @Override protected void onPostExecute(String result) { super.onPostExecute(result); IncomingPhone.this.reallyLogin(result); } @Override protected String doInBackground(String... params) { try { capabilityToken = HttpHelper.httpGet(params[0]); decodedString = capabilityToken.replace("\"", ""); } catch (Exception e) { e.printStackTrace(); } return decodedString; } } } 

And the activity that opens after receiving an incoming call through the service class.

 public class IncomingPhoneActivity extends Activity implements LoginListener, BasicConnectionListener, BasicDeviceListener, View.OnClickListener, CompoundButton.OnCheckedChangeListener, RadioGroup.OnCheckedChangeListener { private static final Handler handler = new Handler(); public IncomingPhone phone; SharedPreferences login_details; Vibrator vibrator; private LinearLayout disconnect_btn; private LinearLayout mainButton; private ToggleButton speakerButton; private ToggleButton muteButton; private EditText logTextBox; static AlertDialog incomingAlert; private EditText outgoingTextBox; private EditText clientNameTextBox; private Button capabilitesButton; private CheckBox incomingCheckBox, outgoingCheckBox; Button call_btn, dis_call_btn, updateButton; public static String AUTH_PHP_SCRIPT, rating1, rating2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.call_screen); Intent intent = getIntent(); Operator_id = intent.getStringExtra("operator_id"); gps = new GPSTracker(IncomingPhoneActivity.this); if (gps.canGetLocation()) { latitude = gps.getLatitude(); longitude = gps.getLongitude(); } else { // gps.showSettingsAlert(); latitude = 0.00; longitude = 0.00; } login_details = getSharedPreferences(LOGIN_DETAILS, Context.MODE_PRIVATE); if (login_details.contains("twilio_Id")) { Twilio_id = login_details.getString("twilio_Id", ""); } AUTH_PHP_SCRIPT = "http://xxxxxxxxxxxxxx/getGenToken?group_id=" + Operator_id + "&twilio_id=" + Twilio_id + "&latitude=" + latitude + "&longitude=" + longitude; disconnect_btn = (LinearLayout) findViewById(R.id.d_call); mainButton = (LinearLayout) findViewById(R.id.call); call_btn = (Button) findViewById(R.id.call_btn); dis_call_btn = (Button) findViewById(R.id.d_call_btn); mainButton.setOnClickListener(this); call_btn.setOnClickListener(this); dis_call_btn.setOnClickListener(this); disconnect_btn.setOnClickListener(this); mainButton.setEnabled(false); call_btn.setEnabled(false); speakerButton = (ToggleButton) findViewById(R.id.speaker_btn); speakerButton.setOnCheckedChangeListener(this); muteButton = (ToggleButton) findViewById(R.id.mute_btn); muteButton.setOnCheckedChangeListener(this); logTextBox = (EditText) findViewById(R.id.log_text_box); outgoingTextBox = (EditText) findViewById(R.id.outgoing_client); clientNameTextBox = (EditText) findViewById(R.id.client_name); clientNameTextBox.setText(DEFAULT_CLIENT_NAME); capabilitesButton = (Button) findViewById(R.id.capabilites_button); capabilitesButton.setOnClickListener(this); outgoingCheckBox = (CheckBox) findViewById(R.id.outgoing); incomingCheckBox = (CheckBox) findViewById(R.id.incoming); vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); if (MenuItems.Speaker == true) { speakerButton.setVisibility(View.VISIBLE); } else { speakerButton.setVisibility(View.INVISIBLE); } } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); phone = IncomingPhone.getInstance(getApplicationContext()); phone.setListeners(this, this, this); phone.login(DEFAULT_CLIENT_NAME, outgoingCheckBox.isChecked(), incomingCheckBox.isChecked()); } private void syncMainButton() { handler.post(new Runnable() { public void run() { if (IncomingPhone.decodedString.length() != 0) { if (phone.isConnected()) { switch (phone.getConnectionState()) { default: mainButton.setClickable(true); mainButton.setEnabled(true); call_btn.setEnabled(true); mainButton.setVisibility(View.VISIBLE); disconnect_btn.setVisibility(View.GONE); disconnect_btn.setClickable(false); break; case DISCONNECTED: mainButton.setVisibility(View.VISIBLE); disconnect_btn.setVisibility(View.GONE); disconnect_btn.setClickable(false); break; case CONNECTED: mainButton.setVisibility(View.GONE); disconnect_btn.setVisibility(View.VISIBLE); disconnect_btn.setClickable(true); break; case CONNECTING: mainButton.setVisibility(View.GONE); disconnect_btn.setVisibility(View.VISIBLE); disconnect_btn.setClickable(true); break; } } else if (phone.hasPendingConnection()) { mainButton.setClickable(true); mainButton.setEnabled(true); call_btn.setEnabled(true); mainButton.setVisibility(View.VISIBLE); disconnect_btn.setVisibility(View.GONE); disconnect_btn.setClickable(false); } else { mainButton.setVisibility(View.VISIBLE); disconnect_btn.setVisibility(View.GONE); disconnect_btn.setClickable(false); } /* * else { Toast.makeText(getApplicationContext(), * "TRY AGAIN!", Toast.LENGTH_SHORT).show(); } */ } } }); } public void onBackPressed() { phone.disconnect(); incomingAlert = null; Intent in = new Intent(IncomingPhoneActivity.this, MenuItems.class); in.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(in); finish(); } class TokenTask extends AsyncTask<Void, Void, Void> { String message; JSONObject jsonResponse; int crash_app; @Override protected void onPreExecute() { } @Override protected Void doInBackground(Void... params) { DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httppost = new HttpGet(AUTH_PHP_SCRIPT); try { HttpResponse response = httpclient.execute(httppost); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); if (entity != null) { IncomingPhone.capabilityToken = EntityUtils .toString(entity); IncomingPhone.decodedString = IncomingPhone.capabilityToken .replace("\"", ""); } } } catch (Exception e) { crash_app = 5; message = "Something went wrong. Please try again later."; return null; } return null; } @Override protected void onPostExecute(Void result) { if (status.equals("success")) { final Handler handler12 = new Handler(); handler12.postDelayed(new Runnable() { public void run() { mainButton.setEnabled(true); call_btn.setEnabled(true); mainButton .setBackgroundResource(R.drawable.light_green_connect); } }, 3000); } if (status.equals("failure")) { Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show(); // mainButton.setBackgroundColor(Color.parseColor("#4ca64c")); mainButton.setBackgroundResource(R.drawable.dark_green_connect); mainButton.setEnabled(false); call_btn.setEnabled(false); } } } @Override public void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); } @Override public void onResume() { super.onResume(); if (phone.handleIncomingIntent(getIntent())) { showIncomingAlert(); addStatusMessage(R.string.got_incoming); if (Utils.isNetworkAvailable(IncomingPhoneActivity.this)) { syncMainButton(); } else { Toast.makeText(IncomingPhoneActivity.this, "No internet connection!!", Toast.LENGTH_SHORT).show(); } } } @Override public void onDestroy() { super.onDestroy(); if (phone != null) { phone.setListeners(null, null, null); phone = null; } } @Override public void onClick(View view) { if ((view.getId() == R.id.d_call) || (view.getId() == R.id.d_call_btn)) { phone.disconnect(); incomingAlert = null; phone.setSpeakerEnabled(false); phone.setCallMuted(false); Intent in = new Intent(IncomingPhoneActivity.this, MenuItems.class); startActivity(in); finish(); } if ((view.getId() == R.id.call) || (view.getId() == R.id.call_btn)) { } else if (view.getId() == R.id.capabilites_button) { phone.login(clientNameTextBox.getText().toString(), outgoingCheckBox.isChecked(), incomingCheckBox.isChecked()); } } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (group.getId() == R.id.input_select) { if (checkedId == R.id.input_number) { outgoingTextBox.setInputType(InputType.TYPE_CLASS_PHONE); outgoingTextBox.setHint(R.string.outgoing_number); } else { outgoingTextBox.setInputType(InputType.TYPE_CLASS_TEXT); outgoingTextBox.setHint(R.string.outgoing_client); } outgoingTextBox.setText(""); } } @Override public void onCheckedChanged(CompoundButton button, boolean isChecked) { if (button.getId() == R.id.speaker_btn) { phone.setSpeakerEnabled(isChecked); } else if (button.getId() == R.id.mute_btn) { phone.setCallMuted(isChecked); } } private void addStatusMessage(final String message) { handler.post(new Runnable() { @Override public void run() { logTextBox.append('-' + message + '\n'); } }); } private void addStatusMessage(int stringId) { addStatusMessage(getString(stringId)); } private void showIncomingAlert() { handler.post(new Runnable() { @Override public void run() { if (incomingAlert == null) { notification = RingtoneManager .getDefaultUri(RingtoneManager.TYPE_RINGTONE); r = RingtoneManager.getRingtone(getApplicationContext(), notification); am = (AudioManager) getSystemService(Context.AUDIO_SERVICE); switch (am.getRingerMode()) { case AudioManager.RINGER_MODE_SILENT: r.play(); break; case AudioManager.RINGER_MODE_VIBRATE: long pattern[] = { 0, 500, 200, 300, 500 }; vibrator.vibrate(pattern, 0); break; case AudioManager.RINGER_MODE_NORMAL: r.play(); break; } incomingAlert = new AlertDialog.Builder( IncomingPhoneActivity.this) .setTitle(R.string.incoming_call) .setCancelable(false) .setMessage(R.string.incoming_call_message) .setPositiveButton(R.string.answer, new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dialog, int which) { switch (am.getRingerMode()) { case AudioManager.RINGER_MODE_SILENT: r.stop(); break; case AudioManager.RINGER_MODE_VIBRATE: vibrator.cancel(); break; case AudioManager.RINGER_MODE_NORMAL: r.stop(); break; } phone.acceptConnection(); disconnect_btn .setVisibility(View.VISIBLE); mainButton.setVisibility(View.GONE); incomingAlert = null; } }) .setNegativeButton(R.string.ignore, new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dialog, int which) { switch (am.getRingerMode()) { case AudioManager.RINGER_MODE_SILENT: r.stop(); break; case AudioManager.RINGER_MODE_VIBRATE: vibrator.cancel(); break; case AudioManager.RINGER_MODE_NORMAL: r.stop(); break; } phone.ignoreIncomingConnection(); incomingAlert = null; } }) .setOnCancelListener( new DialogInterface.OnCancelListener() { @Override public void onCancel( DialogInterface dialog) { phone.ignoreIncomingConnection(); } }).create(); incomingAlert.show(); } } }); } private void hideIncomingAlert() { handler.post(new Runnable() { @Override public void run() { if (incomingAlert != null) { incomingAlert.dismiss(); incomingAlert = null; } } }); } @Override public void onLoginStarted() { addStatusMessage(R.string.logging_in); } @Override public void onLoginFinished() { addStatusMessage(phone.canMakeOutgoing() ? R.string.outgoing_ok : R.string.no_outgoing_capability); addStatusMessage(phone.canAcceptIncoming() ? R.string.incoming_ok : R.string.no_incoming_capability); syncMainButton(); } @Override public void onLoginError(Exception error) { if (error != null) addStatusMessage(String.format(getString(R.string.login_error_fmt), error.getLocalizedMessage())); else addStatusMessage(R.string.login_error_unknown); syncMainButton(); } @Override public void onIncomingConnectionDisconnected() { hideIncomingAlert(); addStatusMessage(R.string.incoming_disconnected); syncMainButton(); } @Override public void onConnectionConnecting() { addStatusMessage(R.string.attempting_to_connect); syncMainButton(); } @Override public void onConnectionConnected() { addStatusMessage(R.string.connected); syncMainButton(); } @Override public void onConnectionFailedConnecting(Exception error) { if (error != null) addStatusMessage(String.format( getString(R.string.couldnt_establish_outgoing_fmt), error.getLocalizedMessage())); else addStatusMessage(R.string.couldnt_establish_outgoing); } @Override public void onConnectionDisconnecting() { addStatusMessage(R.string.disconnect_attempt); syncMainButton(); } @Override public void onConnectionDisconnected() { addStatusMessage(R.string.disconnected); syncMainButton(); } @Override public void onConnectionFailed(Exception error) { if (error != null) addStatusMessage(String.format( getString(R.string.connection_error_fmt), error.getLocalizedMessage())); else addStatusMessage(R.string.connection_error); syncMainButton(); } @Override public void onDeviceStartedListening() { addStatusMessage(R.string.device_listening); } @Override public void onDeviceStoppedListening(Exception error) { if (error != null) addStatusMessage(String.format( getString(R.string.device_listening_error_fmt), error.getLocalizedMessage())); else addStatusMessage(R.string.device_not_listening); } } 
+6
source share

All Articles