Android Open Source - hts-cycle Remote Service Connection






From Project

Back to project page hts-cycle.

License

The source code is released under:

GNU General Public License

If you think the Android project hts-cycle listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package it.uniroma2.musicsharep2p;
/* w ww  .j a  va2  s. c o  m*/

import it.uniroma2.wifionoff.aidl.OnOffService;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;

public class RemoteServiceConnection implements ServiceConnection {

  private static final String AIDL_MESSAGE_SERVICE_CLASS = ".OnOffService";
  private static final String AIDL_MESSAGE_SERVICE_PACKAGE = "it.uniroma2.wifionoff";
  private static final String INTENT_ACTION_BIND_MESSAGE_SERVICE = "bind.OnOffService";
  private final static String LOG_TAG = "RemoteServiceConnection";
  public int downloading;
  public OnOffService service;
  private Context context;
  public boolean timeout;

  public static RemoteServiceConnection instance = null;
  
  private RemoteServiceConnection(Context c){
    this.context=c;
    downloading =0;
    timeout=false;
    }
  

  public boolean isdownloading(){
    if(downloading==0){
      
      return false;
    }
    return true;
  }
  
  
  public synchronized void resettime(){
    
    timeout=false;
    
  }
  
  public synchronized void istimeout(){
    
    timeout=false;
    
  }
  
  @Override
  public void onServiceConnected(ComponentName arg0, IBinder arg1) {    
    this.service = OnOffService.Stub.asInterface(arg1);
  
    try {
      service.addMe(DefaultValue.APPNAME, DefaultValue.LISTEN);
    } catch (RemoteException e) {
      // TODO Auto-generated catch block
        Toast toast=Toast.makeText(context, 
            "Error: Service Not Running", 
            Toast.LENGTH_LONG); 
        toast.show();
      e.printStackTrace();
    }
    Log.d(LOG_TAG, "The service is now connected!");
  }

  @Override
  public void onServiceDisconnected(ComponentName arg0) {
    Log.d(LOG_TAG,
        "The connection to the service got disconnected unexpectedly!");
    
    service = null;

  }

  public void safelyDisconnectTheService() {
    if (service != null) {      
      context.unbindService(this);
      try {
        service.removeMe(DefaultValue.APPNAME);
      } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      service = null;
      Log.d(LOG_TAG, "The connection to the service was closed!");
    }
  }


  public void setContext(Context c){
    
    this.context=c;
    
  }
  

  /**
   * Method to connect the Service.
   */
  public void safelyConnectTheService() {
    
    if (service == null) {
      Intent bindIntent = new Intent(INTENT_ACTION_BIND_MESSAGE_SERVICE);
      bindIntent.setClassName(AIDL_MESSAGE_SERVICE_PACKAGE,
          AIDL_MESSAGE_SERVICE_PACKAGE + AIDL_MESSAGE_SERVICE_CLASS);
      context.bindService(bindIntent, this, Context.BIND_AUTO_CREATE);
      Log.d(LOG_TAG,
          "The Service will be connected soon (asynchronus call)...");
    }
  }

  public static RemoteServiceConnection getInstance(Context applicationContext) {
    // TODO Auto-generated method stub
    if(instance==null){
      
      instance=new RemoteServiceConnection(applicationContext);
    
    }
    return instance;
  }
  
  public synchronized void setDownloading(){
    
    this.downloading++;
    
  }
  public synchronized void setNotDownloading(){
    
    this.downloading--;;
    
  }


  public synchronized boolean Timeout() {
    // TODO Auto-generated method stub
    return this.timeout;
  }

  
}




Java Source Code List

it.uniroma2.musicsharep2p.DataBaseHelper.java
it.uniroma2.musicsharep2p.DefaultValue.java
it.uniroma2.musicsharep2p.DownloadActivity.java
it.uniroma2.musicsharep2p.MainActivity.java
it.uniroma2.musicsharep2p.MessengerService.java
it.uniroma2.musicsharep2p.MusicSettingActivity.java
it.uniroma2.musicsharep2p.MusicShutReceiver.java
it.uniroma2.musicsharep2p.PlayListActivity.java
it.uniroma2.musicsharep2p.RemoteServiceConnection.java
it.uniroma2.musicsharep2p.SongsManager.java
it.uniroma2.musicsharep2p.Utilities.java
it.uniroma2.wifionoff.AlarmReceiver.java
it.uniroma2.wifionoff.AppActivity.java
it.uniroma2.wifionoff.AppHelper.java
it.uniroma2.wifionoff.DataBaseHelper.java
it.uniroma2.wifionoff.IpMaker.java
it.uniroma2.wifionoff.MainActivity.java
it.uniroma2.wifionoff.MessageMaker.java
it.uniroma2.wifionoff.MyListActivity.java
it.uniroma2.wifionoff.NameActivity.java
it.uniroma2.wifionoff.OnOffService.java
it.uniroma2.wifionoff.PacketHandler.java
it.uniroma2.wifionoff.ServiceCall.java
it.uniroma2.wifionoff.SettingActivity.java
it.uniroma2.wifionoff.Setting.java
it.uniroma2.wifionoff.ShutDownReceiver.java
it.uniroma2.wifionoff.WifiConfigurationNew.java
it.uniroma2.wifionoff.WifiHandler.java
it.uniroma2.wifionoff.WifiManagerNew.java
it.uniroma2.wifionoff.WifiOffReceiver.java
it.uniroma2.wifionoff.WifiReceiver.java