Jump to content



Photo

I need help, android + services


  • Please log in to reply
21 replies to this topic

#1 Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 17 July 2013 - 00:09

hey guys! i am having such trouble getting my head around services for android and I do not know why :/ its just not going in..

 

thats it really lol I have looked at them and I understand how they are set up in code structure ... but I dont know how to integrate a service?

 

lets say for example I had a chat client on my phone, I want that chat client to receive messages even when the screen isnt open, how would I go about that? (I have already programmed a chat client)..

 

I dont understand how my program and the service I am writing for it integrate together I am willing to practice with smaller bits of code to get used to it but all the examples I find rather then giving good clear English examples give 2 million pages of code and only explain the service, how am i meant to know how the service works if I dont know about the rest of their code lol?

 

currently reading SAMS teach your self java in 24 hours, while looking at official documentation and tutorials, just need someone use plain english please 




#2 firey

firey

    F͎̗͉͎͈͑͡ȉ͎̣̐́ṙ͖̺͕͙̓̌è̤̞͉̟̲͇̍̍̾̓ͥͅy͓̍̎̌̏̒

  • Tech Issues Solved: 6
  • Joined: 30-October 05
  • Location: Alberta, Canada
  • OS: Windows 7
  • Phone: Android (4.4.2)

Posted 17 July 2013 - 01:31

I just recently wrote a TCP Client into a service as I was having problems with it disconnecting whenever I rotated the display.   Essentially there are two ways to do the service.  1) Application based, or 2) System based.  I have only done application based.   But what you do is think of the service like an object,  which is created, then you join it.

http://developer.and...pp/Service.html
 

I followed this, and made the local service.  I did the tutorial 100% and then made modifications to match what I was doing.  I then implemented a handler

http://robotoworks.c...droid-handlers/

 

to communicated from the service to my Activity (I use it to pass TCP Data I receive) so that I can fill in forms, make toasts, etc.

Make sure to set the service to STICKY.

For the record, I am a decent programmer, but doign services and java, and android for that matter is 100% new to me, and I was able to get and understand it relatively quickly.  There were a few things that stopped me up.  For example, you must explicity start the service, before binding otherwise it doesn't create an instance that can be rebound to.



#3 OP Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 17 July 2013 - 12:01

I just recently wrote a TCP Client into a service as I was having problems with it disconnecting whenever I rotated the display.   Essentially there are two ways to do the service.  1) Application based, or 2) System based.  I have only done application based.   But what you do is think of the service like an object,  which is created, then you join it.

http://developer.and...pp/Service.html
 

I followed this, and made the local service.  I did the tutorial 100% and then made modifications to match what I was doing.  I then implemented a handler

http://robotoworks.c...droid-handlers/

 

to communicated from the service to my Activity (I use it to pass TCP Data I receive) so that I can fill in forms, make toasts, etc.

Make sure to set the service to STICKY.

For the record, I am a decent programmer, but doign services and java, and android for that matter is 100% new to me, and I was able to get and understand it relatively quickly.  There were a few things that stopped me up.  For example, you must explicity start the service, before binding otherwise it doesn't create an instance that can be rebound to.

 

thanks I will look into this :p, I am rather a good programmer my self but I think the problem is with something as deep as services it becomes a problem for me as it uses a lot of technical language, being self taught and learning off examples means I lose the advantage of know the technical terms and reading about services?! my sky daddy there are alot of terms that I have no idea what they mean I barely know about objects :p 

When someone says object Im still like eerrrrrmm... oh yea I know what you mean lol... I use them ALL the time in both PHP, JAVA and android but I never talk about programming really, so have never needed to know terms. anyway once again thanks :p if you do find even more simply explinations pass them here xD once I do it once I will be ok...just need that once though 



#4 firey

firey

    F͎̗͉͎͈͑͡ȉ͎̣̐́ṙ͖̺͕͙̓̌è̤̞͉̟̲͇̍̍̾̓ͥͅy͓̍̎̌̏̒

  • Tech Issues Solved: 6
  • Joined: 30-October 05
  • Location: Alberta, Canada
  • OS: Windows 7
  • Phone: Android (4.4.2)

Posted 17 July 2013 - 12:56

thanks I will look into this :p, I am rather a good programmer my self but I think the problem is with something as deep as services it becomes a problem for me as it uses a lot of technical language, being self taught and learning off examples means I lose the advantage of know the technical terms and reading about services?! my sky daddy there are alot of terms that I have no idea what they mean I barely know about objects :p 

When someone says object Im still like eerrrrrmm... oh yea I know what you mean lol... I use them ALL the time in both PHP, JAVA and android but I never talk about programming really, so have never needed to know terms. anyway once again thanks :p if you do find even more simply explinations pass them here xD once I do it once I will be ok...just need that once though 

I try and think of the technical terms in a real-world sense.   

ie) a Pointer... something that points to something else, and draws attention to that thing.
  an Object.... something.  ie) A ball is a physical object, it has properties, it is something
 a Service...  something that is done for me, that I don't have to worry about but something else is preforming it.

I am mostly self-taught the same way, and I know exactly what you mean by the technical side of things terminology wise.  Until recently I didn't know what it meant to "Serialize" something, but I knew the process of it.



#5 OP Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 17 July 2013 - 14:37

I try and think of the technical terms in a real-world sense.   

ie) a Pointer... something that points to something else, and draws attention to that thing.
  an Object.... something.  ie) A ball is a physical object, it has properties, it is something
 a Service...  something that is done for me, that I don't have to worry about but something else is preforming it.

I am mostly self-taught the same way, and I know exactly what you mean by the technical side of things terminology wise.  Until recently I didn't know what it meant to "Serialize" something, but I knew the process of it.

 

 

ha serialize people always telling me its the way? dont even know lol! ... out of the two methods you have shown me, what do you personally think would be best for a chat program ? possibly multi-threaded? 



#6 firey

firey

    F͎̗͉͎͈͑͡ȉ͎̣̐́ṙ͖̺͕͙̓̌è̤̞͉̟̲͇̍̍̾̓ͥͅy͓̍̎̌̏̒

  • Tech Issues Solved: 6
  • Joined: 30-October 05
  • Location: Alberta, Canada
  • OS: Windows 7
  • Phone: Android (4.4.2)

Posted 17 July 2013 - 14:57

You could multi-thread, but the way activities work, if orientation changes or the activity is suspended the threads are suspended too.  By using a serice, they can remain active even if the activity is paused/closed/disconnected/etc.

The things I showed you work hand in hand and is how I implemented it into my project.

 

As for serializing, I've heard it's the way to go, especially for saving objects / sending objects.



#7 OP Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 17 July 2013 - 20:06

You could multi-thread, but the way activities work, if orientation changes or the activity is suspended the threads are suspended too.  By using a serice, they can remain active even if the activity is paused/closed/disconnected/etc.

The things I showed you work hand in hand and is how I implemented it into my project.

 

As for serializing, I've heard it's the way to go, especially for saving objects / sending objects.

 

havent got any samples have you? seeing as you have done what I need all i need to see is the interaction between them ?? :) ??



#8 firey

firey

    F͎̗͉͎͈͑͡ȉ͎̣̐́ṙ͖̺͕͙̓̌è̤̞͉̟̲͇̍̍̾̓ͥͅy͓̍̎̌̏̒

  • Tech Issues Solved: 6
  • Joined: 30-October 05
  • Location: Alberta, Canada
  • OS: Windows 7
  • Phone: Android (4.4.2)

Posted 17 July 2013 - 20:22

havent got any samples have you? seeing as you have done what I need all i need to see is the interaction between them ?? :) ??


Not sure what you are after but here is my basic setup/connect code in the MainActivity file

 

    @Override
    protected void onStart() {
    	super.onStart();
    	
    	//Bind to the service
    	Intent intent = new Intent(curContext,TCPService.class);
    	startService(intent);
    	bindService(intent,mConnection,Context.BIND_AUTO_CREATE);
    	
    }
    
    @Override
    protected void onStop() {
    	super.onStop();
    	
    	//Unbind from the service
    	if (mBound) {
    		unbindService(mConnection);
    		
    		mBound = false;
    	}
    }
    
   
    
    //Defines callback for service binding, passed to bindService()
    private ServiceConnection mConnection = new ServiceConnection() {
    	    @Override
    	    public void onServiceConnected(ComponentName className, IBinder service){
    	    	//We are bound, so cast the binder to get the instance
    	    	LocalBinder binder = (LocalBinder) service;
    	    	mService = binder.getService();
    	    	mService.Setup(handler);
    	    	mBound = true;
    	    	
    	    	if (!mService.Connected) {
	    	        if (chkAutoConnect != null && chkAutoConnect.isChecked())
	    	        {
	    	        	btnConnect_Click();
	    	        }
    	    	}
    	    }
    	    
    	    @Override
    	    public void onServiceDisconnected(ComponentName arg0) {
    	    	mBound = false;
    	    }
    };
    

    
    private final Handler handler = new Handler() {
	    public void handleMessage(Message msg) {
	             
	    		parseData(msg.getData().getCharArray("data"));
	            //Log.e("Service",Integer.toString(aResponse)); 
	        }
    };

And this is my setup/TCP Connection in the Service

 

	public void Setup(Handler messageHandler)
	{
		handler = messageHandler;
	}


    public class TCPConnect extends AsyncTask<String,String,TCPClient> {


    	@Override 
    	protected TCPClient doInBackground(String... message)
    	{
    		mTCPClient = new TCPClient(new TCPClient.OnMessageReceived() {
    			
    			@Override
    			public void MessageReceived(String message) {	
    				publishProgress(message);
    			}
    		});
    		

    		mTCPClient.Connect(serverIP,serverPort);
    		  			
    		return null;
    	}

    	@Override
    	protected void onProgressUpdate(String... values) {
    	    super.onProgressUpdate(values);

    	    Message msgObj = handler.obtainMessage();
    		Bundle b = new Bundle();
    		b.putCharArray("data",values[0].toCharArray());
    		msgObj.setData(b);
    		handler.sendMessage(msgObj);
    		
    	}
    }

Setup() takes the handler so it knows where to send messages too.  The TCP data received shows making and sending the message.    As I say most of this is new so it is a mish-mash of tutorials modified to suit what I needed it to do.



#9 OP Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 17 July 2013 - 21:09

Not sure what you are after but here is my basic setup/connect code in the MainActivity file

 

And this is my setup/TCP Connection in the Service
 

 

 

ah ok, so does the service start on the start of the class? or are you manually starting the service within the thread? do not think i can see it actually being called, ALSO is this all in main activity ? and thn you are just calling the TCP service on start?



#10 firey

firey

    F͎̗͉͎͈͑͡ȉ͎̣̐́ṙ͖̺͕͙̓̌è̤̞͉̟̲͇̍̍̾̓ͥͅy͓̍̎̌̏̒

  • Tech Issues Solved: 6
  • Joined: 30-October 05
  • Location: Alberta, Canada
  • OS: Windows 7
  • Phone: Android (4.4.2)

Posted 17 July 2013 - 21:20

ah ok, so does the service start on the start of the class? or are you manually starting the service within the thread? do not think i can see it actually being called, ALSO is this all in main activity ? and thn you are just calling the TCP service on start?

My TCP Thread/Class is inside my service.  In my MainActivity I tell it to start the service (runs the services Constructor, which I have as empty).  I then Bind to it, which allows me to directly interact with the instance of the service.  By having the service start as STICKY, then it will automatically return an instance of a pre-existing service and does not start it again.

I then call a function I wrote that is inside the service.  That function creates a new instance of the TCP Server class which has it's own threading.

So essentially it's like this

MainActivity -> Start Service -> Service Runs Constructor -> Starts as STICKY (So not to dispose itself)
MainActivity -> Bind to Service -> Service Runs OnBind (Returns instance of itself back to MainActivity)
MainActivity -> Runs Connect Function inside Instance of Service -> Service Starts TCP Class which has it's thread

That way my main activity can be doing whatever (stopped, paused, etc), but the service is still running, and able to pass messages back, make toasts, etc.

The Connect function is called in a different function (when user clicks connect).  And is done by doing mService.Connect(ip,port);   mService = my local instance of the service.



#11 OP Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 18 July 2013 - 00:14

My TCP Thread/Class is inside my service.  In my MainActivity I tell it to start the service (runs the services Constructor, which I have as empty).  I then Bind to it, which allows me to directly interact with the instance of the service.  By having the service start as STICKY, then it will automatically return an instance of a pre-existing service and does not start it again.

I then call a function I wrote that is inside the service.  That function creates a new instance of the TCP Server class which has it's own threading.

So essentially it's like this

MainActivity -> Start Service -> Service Runs Constructor -> Starts as STICKY (So not to dispose itself)
MainActivity -> Bind to Service -> Service Runs OnBind (Returns instance of itself back to MainActivity)
MainActivity -> Runs Connect Function inside Instance of Service -> Service Starts TCP Class which has it's thread

That way my main activity can be doing whatever (stopped, paused, etc), but the service is still running, and able to pass messages back, make toasts, etc.

The Connect function is called in a different function (when user clicks connect).  And is done by doing mService.Connect(ip,port);   mService = my local instance of the service.

lol im trying to play around with this at the moment, can you explain this line??  public class TCPConnect extends AsyncTask<String,String,TCPClient> {

thans for all your help btw!!! really appreciate it 



#12 OP Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 18 July 2013 - 00:27

My TCP Thread/Class is inside my service.  In my MainActivity I tell it to start the service (runs the services Constructor, which I have as empty).  I then Bind to it, which allows me to directly interact with the instance of the service.  By having the service start as STICKY, then it will automatically return an instance of a pre-existing service and does not start it again.

I then call a function I wrote that is inside the service.  That function creates a new instance of the TCP Server class which has it's own threading.

So essentially it's like this

MainActivity -> Start Service -> Service Runs Constructor -> Starts as STICKY (So not to dispose itself)
MainActivity -> Bind to Service -> Service Runs OnBind (Returns instance of itself back to MainActivity)
MainActivity -> Runs Connect Function inside Instance of Service -> Service Starts TCP Class which has it's thread

That way my main activity can be doing whatever (stopped, paused, etc), but the service is still running, and able to pass messages back, make toasts, etc.

The Connect function is called in a different function (when user clicks connect).  And is done by doing mService.Connect(ip,port);   mService = my local instance of the service.

lol im trying to play around with this at the moment, can you explain this line??  public class TCPConnect extends AsyncTask<String,String,TCPClient> {

thans for all your help btw!!! really appreciate it 



#13 OP Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 18 July 2013 - 00:54

My TCP Thread/Class is inside my service.  In my MainActivity I tell it to start the service (runs the services Constructor, which I have as empty).  I then Bind to it, which allows me to directly interact with the instance of the service.  By having the service start as STICKY, then it will automatically return an instance of a pre-existing service and does not start it again.

I then call a function I wrote that is inside the service.  That function creates a new instance of the TCP Server class which has it's own threading.

So essentially it's like this

MainActivity -> Start Service -> Service Runs Constructor -> Starts as STICKY (So not to dispose itself)
MainActivity -> Bind to Service -> Service Runs OnBind (Returns instance of itself back to MainActivity)
MainActivity -> Runs Connect Function inside Instance of Service -> Service Starts TCP Class which has it's thread

That way my main activity can be doing whatever (stopped, paused, etc), but the service is still running, and able to pass messages back, make toasts, etc.

The Connect function is called in a different function (when user clicks connect).  And is done by doing mService.Connect(ip,port);   mService = my local instance of the service.

lol im trying to play around with this at the moment, can you explain this line??  public class TCPConnect extends AsyncTask<String,String,TCPClient> {

thans for all your help btw!!! really appreciate it 


i am going crazy I am just staring at this code.



#14 OP Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 18 July 2013 - 01:22

I am going to pull an all nighter I will not let this crap defeat me ... once i understand it il kick my self for being a blind idiot, but i have to get this in there.



#15 OP Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 18 July 2013 - 01:38

I DID IT !!! well i kind of did it ... I made a service happen and I understand how it happened etc and why... now to play around with it and understand it more... 

package com.example.serviceexample;

import android.app.IntentService;
import android.content.Intent;
import android.widget.Toast;

public class MyIntentService extends IntentService {

	@Override
	protected void onHandleIntent(Intent arg0) {
		// TODO Auto-generated method stub
		Toast.makeText(this, "TESTING", Toast.LENGTH_LONG).show();
	}
	public MyIntentService() {
		super("MyIntentService");
	}

}

my service I know it is REALLY basic and cannot do anything! but its a foundation :)