Android Local Background Service example

Source: Internet
Author: User

Introduction to Android Authoring Service introduces two of the Android's backend services, both local and remote. Here with the local service to do a simulation of the timing of the background text messaging technology prototype.

Start the example as it applies:

Select Start:

When you start, you can see the DDMS logcat log:

This is the operation: Start, stop, and exit the application to display the debug log information. The analog background service sends SMS messages every 5 seconds.

Main code, background service SMSService:

Package com.easymorse;

Import Android.app.Service;
Import android.content.Intent;
Import Android.os.Binder;
Import Android.os.IBinder;
Import Android.util.Log;

public class SMSService extends Service {

Private Boolean started;

Private Boolean threaddisable;

Private Servicebinder Servicebinder = new Servicebinder ();

public class Servicebinder extends Binder implements Ismsservice {

@Override
public Boolean isstarted () {
return started;
}

@Override
public void Start () {
Started=true;
LOG.D ("Sms.service", "SMS Service started.");
}

@Override
public void Stop () {
Started=false;
LOG.D ("Sms.service", "SMS Service stopped.");
}
}

@Override
Public IBinder Onbind (Intent Intent) {
return servicebinder;
}

@Override
public void OnCreate () {
Super.oncreate ();

Thread thread = new Thread () {
@Override
public void Run () {
while (!threaddisable) {
try {
if (started) {
LOG.D ("Sms.service", "Send a SMS message.");
}
Thread.Sleep (1000 * 5);
catch (Interruptedexception e) {
}
}
}
};

Thread.Start ();

LOG.D ("Sms.service", "SMS Service created.");
}

@Override
public void OnDestroy () {
Super.ondestroy ();
Threaddisable = true;
LOG.D ("Sms.service", "SMS Service shutdown.");
}
}

Actvity code for the front desk, smsserviceoptions:

Package com.easymorse;

Import android.app.TabActivity;
Import Android.content.ComponentName;
Import android.content.Intent;
Import android.content.ServiceConnection;
Import Android.os.Bundle;
Import Android.os.IBinder;
Import Android.util.Log;
Import Android.widget.RadioGroup;
Import Android.widget.TabHost;
Import Android.widget.RadioGroup.OnCheckedChangeListener;

public class Smsserviceoptions extends Tabactivity {

Private Radiogroup Radiogroup;

Private Ismsservice SMSService;

Private Serviceconnection serviceconnection = new Serviceconnection () {

@Override
public void onserviceconnected (componentname name, IBinder service) {
SMSService = (ismsservice) service;

if (smsservice.isstarted ()) {
Radiogroup.check (R.id.radiobuttonstart);
} else {
Radiogroup.check (R.id.radiobuttonstop);
}

Radiogroup
. Setoncheckedchangelistener (New Oncheckedchangelistener () {

@Override
public void OnCheckedChanged (Radiogroup Group,
int Checkedid) {
if (Checkedid = = R.id.radiobuttonstart) {
LOG.D ("Sms.service", "Starting service ...");
Smsservice.start ();
} else {
LOG.D ("Sms.service", "Stopping service ...");
Smsservice.stop ();
}
}
});
}

@Override
public void onservicedisconnected (componentname name) {
SMSService = null;
}
};

/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

This.settitle ("SMS Server");
This.bindservice (New Intent ("Com.easymorse.SmsService"),
This.serviceconnection, bind_auto_create);

Tabhost tabhost = This.gettabhost ();
Tabhost.setup ();

Tabhost.tabspec spec = tabhost.newtabspec ("service Options");
Spec.setcontent (R.ID.OPTION01);
Spec.setindicator ("service Options");
Tabhost.addtab (spec);

Spec = Tabhost.newtabspec ("service status");
Spec.setcontent (R.ID.OPTION02);
Spec.setindicator ("service status");
Tabhost.addtab (spec);

Radiogroup = (radiogroup) This.findviewbyid (R.ID.RADIOGROUP01);
}

@Override
protected void OnDestroy () {
Super.ondestroy ();
This.unbindservice (serviceconnection);
}
}

Also, do not forget to add a statement to the service in the configuration file, see Androidmanafest.xml:

<?xml version= "1.0″encoding=" utf-8″?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.easymorse" android:versioncode= "1″android:versionname=" 1.0″>
<application android:icon= "@drawable/icon" android:label= "@string/app_name" >
<activity android:name= ". Smsserviceoptions "android:label=" @string/app_name ">
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<service android:name= ". SMSService ">
<intent-filter>
<action android:name= "Com.easymorse.SmsService" ></action>
</intent-filter>
</service>
</application>
<USES-SDK android:minsdkversion= "3″/>
</manifest>

See Source code:

http://easymorse.googlecode.com/svn/tags/android.local.service.1.demo/

The limitation of this example is that if the activity exits, the background service exits together as well. Whether the local service can still be performed in the background after the activity exits requires further understanding of the Android API. I guess it shouldn't be, local services and activity may be different threading relationships of the same process.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.