java - Good approach for my specific issue -


i wrote client(android smartphone)-server(java)-program.

it's practice , till sending string server saving file.

currently want implement new feature sending gps coordinates every 5 minutes server , saving it.

for i'd set alarmmanager receiving broadcast every 300000ms (or 5 minutes).

when receiving broadcast wanted establish connection server:

alarmreceiver.class:

    public class alarmreceiver extends broadcastreceiver {      @override     public void onreceive(context context, intent intent) {         // todo auto-generated method stub         log.e("alarmreceiver","received alarm");         mainactivity activity = (mainactivity) context;         activity.cconnect = new connectionthread(activity.clientserver, activity.out);         activity.cconnect.start();     } 

}

connectionthread.class:

    public class connectionthread extends thread {      socket clientserver;     bufferedwriter out;      public connectionthread(socket clientserver_, bufferedwriter out_) {         // todo auto-generated constructor stub         this.clientserver = clientserver_;         this.out = out_;     }      @override     public void run() {         // todo auto-generated method stub         super.run();         try {             clientserver = new socket(mainactivity.dstname, mainactivity.dstport);              out = new bufferedwriter(new outputstreamwriter(clientserver.getoutputstream()));             out.write("alarm received"); //currently sending string             out.flush();             out.close();             clientserver.close();         } catch (unknownhostexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }     } } 

where in mainactivity.class declared following:

protected static string dstname = ... protected static int dstport = ... public socket clientserver; public bufferedwriter out; private alarmmanager alarmmgr; private static int repeatalarmevery = 30000; private static int firstalarmafter = repeatalarmevery;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);... 

my problem is, afraid trying transmit text while alarmreceiver received broadcast , has established connection on same port.. decided have 1 socket-object called clientserver , way can check if clientserver != null -> do not establish new connection, instead use existing one.

but trying run client-program on emulator getting following error:

06-03 19:24:29.969: e/androidruntime(2186): caused by: java.lang.classcastexception: android.app.receiverrestrictedcontext cannot cast com.example.clientservercommunication.mainactivity 06-03 19:24:29.969: e/androidruntime(2186):     @ com.example.clientservercommunication.alarmreceiver.onreceive(alarmreceiver.java:14) 

what think/see context received broadcast restricted , not allowed cast activity mainactivity. not able access clientsocket , out mainactivity.class.

now have 2 solutions in mind:

1) declaring clientsocket , out static.

2) establish different connection (different port) text transmitting avoiding conflict between transmitting text , gps coordinates. e.g. text transmitting port 50000, gps data transmitting on port 50001

question:

which 1 better(regarding memory leak if there etc., not regarding opinion e.g. because solution better etc.)/ more common approach or there better design pattern/solution achieve want.

it important me support right pattern because sick of trying without enough background knowledge running tons of problems. hope described issue well.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Website Login Issue developed in magento -

Can the constants be defined inside a model file of a framework in PHP? -