AnsweredAssumed Answered

How to keep XMP connection stable in background while app closed android.

Question asked by shree on May 29, 2017

I search lot regard how to get over the xmpp connection stable always. even android app closed or exit. I'm using background service process too. but it's not working well. if any one help me. It'll be very helpful to me.

 

MyService.class

 

 

 

public class MyService extends Service {

 

   public int counter = 0;

   private Timer timer;

   private TimerTask timerTask;

   private String TAG = "MyService";

  MyXMPP myXMPP;

   public MyService() {

  }

 

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {

   super.onStartCommand(intent, flags, startId);

 

 

   final String uname = intent.getStringExtra("uname");

   final String pass = intent.getStringExtra("pass");

 

  Log.d("uname_is",""+uname);

  Log.d("uname_is",""+pass);

 

   myXMPP = new MyXMPP(MyService.this, getString(R.string.server2), uname, pass);

   myXMPP.connect("onCreate");
   return START_STICKY;

  }

 

   @Override
   public void onDestroy() {

   super.onDestroy();

  Log.i(TAG, "ondestroy!");

  }

 

 

   @Override
   public void onTaskRemoved(Intent rootIntent) {

  Log.d(TAG, "onTaskRemoved");

  Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());

  restartServiceIntent.putExtra("uname", Pref.getPreferenceUser());

  restartServiceIntent.putExtra("pass", Pref.getPreferencePass());

  restartServiceIntent.setPackage(getPackageName());

 

  PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);

  AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);

  alarmService.set(

  AlarmManager.ELAPSED_REALTIME,

  SystemClock.elapsedRealtime() + 1000,

  restartServicePendingIntent);

 

   super.onTaskRemoved(rootIntent);

  }

 

   @Override
   public IBinder onBind(Intent intent) {

   return null;

  }

}

Outcomes