Archived

This forum has been archived. Please start a new discussion on GitHub.

Android Server

Hello,

I am a student from UC Berkeley and I was wondering if it was possible to have an android server. And if so, do you have any example code that I can look at?

Thank you very much
Daniel

Comments

  • here is a simple server i made using the basic printer example in the ice manual that displays a notification when a new message is received. Im not sure how to run it on the emulator, because I don't know what host name to give to the client. I just run it on my phone using my wireless home network. Then use the local ip address of the phone as the host name.

    Android Server
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Build.VERSION;
    import Demo._PrinterDisp;
    import Ice.Current;
    import android.content.Context;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    
    public class helloServer extends Activity {
        /** Called when the activity is first created. */
    	Ice.Communicator ic=null;
    	Demo.PrinterPrx printer;
    	Ice.Object object = new PrinterI();
    	
        @Override
        public void onCreate(Bundle savedInstanceState) {
        	if(VERSION.SDK_INT == 8) // android.os.Build.VERSION_CODES.FROYO (8)
            {
                //
                // Workaround for a bug in Android 2.2 (Froyo).
                //
                // See http://code.google.com/p/android/issues/detail?id=9431
                //
                java.lang.System.setProperty("java.net.preferIPv4Stack", "true");
                java.lang.System.setProperty("java.net.preferIPv6Addresses", "false");
            }
            super.onCreate(savedInstanceState);
            if(ic==null){
            Ice.Properties props = Ice.Util.createProperties();
            Ice.InitializationData id = new Ice.InitializationData();
            id.properties=props;
            ic=Ice.Util.initialize(id);
            
            Ice.ObjectAdapter adapter=ic.createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10000");
            adapter.add(object, ic.stringToIdentity("SimplePrinterAdapter"));
            adapter.activate();
            }
    
        }
        public class PrinterI extends _PrinterDisp {
    
    	public PrinterI() {
    		
    		// TODO Auto-generated constructor stub
    	}
    
    	@Override
    	public void printString(String s, Current __current) {
    		// TODO Auto-generated method stub
    		NotificationManager notifier = (NotificationManager)helloServer.this.getSystemService(Context.NOTIFICATION_SERVICE);
    		Notification notification = new Notification(1,"Notification",System.currentTimeMillis());
    		Intent toLaunch = new Intent(helloServer.this,helloServer.class);
    		PendingIntent contentIntent =PendingIntent.getActivity(helloServer.this, 0, toLaunch, 0);
    		notification.setLatestEventInfo(helloServer.this,s,"" , contentIntent);
    		notification.flags |= Notification.FLAG_AUTO_CANCEL;
    		notification.flags |= Notification.DEFAULT_SOUND;
    		//Send the notification
    		notifier.notify(0x007, notification);
    	}
    
    }
        
        @Override
        public void onDestroy(){
        	super.onDestroy();
        	//ic.destroy();
        }
        @Override
        public void onPause(){
        	super.onDestroy();
        }
    
    }
    

    c++ client
    #include <Ice/Ice.h>
    #include <Printer.h>
    
    using namespace std;
    using namespace Demo;
    
    
    int
    main(int argc, char* argv[])
    {
        int status = 0;
        Ice::CommunicatorPtr ic;
        try {
           ic = Ice::initialize(argc, argv);
    
    		Ice::ObjectPrx base=ic->stringToProxy("SimplePrinterAdapter:tcp -h 192.168.1.105 -p 10000");
    		PrinterPrx printer=PrinterPrx::checkedCast(base);
    		printer->printString("test");
    
        } catch (const Ice::Exception& e) {
            cerr << e << endl;
            status = 1;
        } catch (const char* msg) {
            cerr << msg << endl;
            status = 1;
        }
        if (ic) {
            try {
    			ic->destroy();
            } catch (const Ice::Exception& e) {
                cerr << e << endl;
                status = 1;
            }
        }
        return status;
    }