Basic service example in android

2 votes · 0 comments

Service -A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.

Difference between a Thread , service and asynchronous task

1) Service is like an Activity but has no interface. Probably if you want to fetch the weather for example you won't create a blank activity for it, for this you will use a Service.

2) A Thread is a Thread, probably you already know it from other part. You need to know that you cannot update UI from a Thread. You need to use a Handler for this, but read further.

3) An AsyncTask is an intelligent Thread that is advised to be used. Intelligent as it can help with it's methods, and there are two methods that run on UI thread, which is good to update UI components

Continue Reading

raw ·
copy
· download
public class ServiceDemoActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewById(R.id.start).setOnClickListener(this); findViewById(R.id.stop).setOnClickListener(this); } private Intent inetnt; @Override public void onClick(View v) { switch (v.getId()) { case R.id.start: inetnt=new Intent(this,ServiceExample.class); startService(inetnt); break; case R.id.stop: inetnt=new Intent(this,ServiceExample.class); stopService(inetnt); break; } } @Override protected void onResume() { super.onResume(); } @Override protected void onDestroy() { super.onDestroy(); // } }

Be the first to comment

Sign in with OpenID