Moon Light Box

Time is Money

Android - Service

| Comments

  1. Service is a non-UI application.
  2. Service is running in Main Thread.
  3. Service has two forms : Started and Bound.

We need override four methods.

  • onStartCommand()
  • onBind()
  • onCreate()
  • onDestroy()

Here is a Started example.
Activity is simple calling Service to work in background.
There is no interaction between Activity and Service.

MainActivity.java
public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            initMainFragment();
        }
    }

    private void initMainFragment() {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new MainFragment()).commit();
    }
}
MainFragment.java
public class MainFragment extends Fragment implements OnClickListener {
    private Activity mActivity;
  
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        ((Button) rootView.findViewById(R.id.start_service)).setOnClickListener(this);
        ((Button) rootView.findViewById(R.id.stop_service)).setOnClickListener(this);
        return rootView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.start_service:
            mActivity.startService(new Intent(mActivity, MainService.class));
            break;
        case R.id.stop_service:
            mActivity.stopService(new Intent(mActivity, MainService.class));
            break;
        default:
            break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mActivity.finish();
    }
}
MainService.java
public class MainService extends Service {
    public static final String TAG = "MainService";

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate() executed");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand() executed");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy() executed");
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.service"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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>
        <!-- ↓↓↓↓↓↓ Register Your Service Here ↓↓↓↓↓↓ -->
        <service android:name="com.example.service.MainService" />
        <!-- ↑↑↑↑↑↑ Register Your Service Here ↑↑↑↑↑↑ -->
    </application>

</manifest>

You can see the logcat message when click buttons.

D/MainService(8297): onCreate() executed
D/MainService(8297): onStartCommand() executed
D/MainService(8297): onDestroy() executed

Reference:http://developer.android.com/guide/components/services.html
Reference:http://blog.csdn.net/guolin_blog/article/details/11952435

Comments

comments powered by Disqus