Android How to - Create service








The following code shows how to Create your own service.

Example

Install service in manifest file

<?xml version="1.0" encoding="utf-8"?>
<!--
  Copyright (c) 2012 Manning
  See the file license.txt for copying permission.
-->
<manifest...

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            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=".MyService" />
    </application>

</manifest>

Layout xml

<?xml version="1.0" encoding="utf-8"?>
<!--
  Copyright (c) 2012 Manning
  See the file license.txt for copying permission.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="hello" />

</LinearLayout>

Main activity

//w w  w  . j  a  v a  2 s  . c om
/*******************************************************************************
 * Copyright (c) 2012 Manning
 * See the file license.txt for copying permission.
 ******************************************************************************/


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
  private ProgressDialog mProgressDialog;
  private TextView mTextView;

  private BroadcastReceiver mReceiver;
  private IntentFilter mIntentFilter;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mTextView = (TextView) findViewById(R.id.text_view);

    mProgressDialog = ProgressDialog.show(this, "Loading",
        "Please wait");
    mProgressDialog.show();

    mReceiver = new MyServiceReceiver();
    mIntentFilter = new IntentFilter(MyService.ACTION);

    startService(new Intent(this, MyService.class));
  }

  @Override
  protected void onResume() {
    super.onResume();
    registerReceiver(mReceiver, mIntentFilter);
  }

  @Override
  public void onPause() {
    super.onPause();
    unregisterReceiver(mReceiver);
  }

  private void update(String msg) {
    mProgressDialog.dismiss();
    mTextView.setText(msg);
  }

  class MyServiceReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
      update(intent.getExtras().getString(MyService.MSG_KEY));

    }

  }
}

Service

//from   w  w  w  . ja va  2  s  . c  om
/*******************************************************************************
 * Copyright (c) 2012 Manning
 * See the file license.txt for copying permission.
 ******************************************************************************/


import java.util.Date;

import android.app.IntentService;
import android.content.Intent;
import android.os.SystemClock;

public class MyService extends IntentService {

  public static final String ACTION = "com.java2s.SERVICE_MSG";
  public static final String MSG_KEY = "MSG_KEY";

  public MyService() {
    super("MyService");
  }

  @Override
  protected void onHandleIntent(Intent intent) {
    SystemClock.sleep(5000L);
    Intent broadcast = new Intent(ACTION);
    broadcast.putExtra(MSG_KEY, new Date().toGMTString());
    sendBroadcast(broadcast);
  }

}