Wednesday 4 September 2013

Andorid Service Example Code


 A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors). 

1.Create a Service Class MyService

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
   
   
    @Override
    public void onCreate() {
        Toast.makeText(this, "New Service Created", Toast.LENGTH_LONG).show();
      
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // For time consuming an long tasks you can launch a new thread here...
        Toast.makeText(this, " Service Start", Toast.LENGTH_LONG).show();
      
  
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
       
    }
}

2.Create a Class where you can start service and stop service.

public class MainActivity extends Activity {

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

    // Start the  service
    public void startNewService(View view) {
       
        startService(new Intent(this, MyService.class));
    }

    // Stop the  service
    public void stopNewService(View view) {
       
        stopService(new Intent(this, MyService.class));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

3.Add Service to your manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.service.sample"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.service.sample.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="com.service.sample.MyService"
            android:enabled="true"
            android:exported="true" >
        </service>
    </application>

</manifest>

Now the service handling requests is done outside of the main thread, the application remains responsive to the user and the Android system. 

No comments:

Post a Comment