Friday, 6 February 2015

IntentService Example


ScreenShots:

LogCat:






activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="Start Service" />

</RelativeLayout>

MyIntentService.java

package com.swamys.intentservice;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

public class MyIntentService extends IntentService {

       String TAG = "Intent Service";

       public MyIntentService() {
              super("Hello from intent service");
       }

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

       @Override
       protected void onHandleIntent(Intent intent) {
              Log.d(TAG, "service started");
       }

} 

MainActivity.java

package com.swamys.intentservice;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

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

              Button start = (Button) findViewById(R.id.button1);
              start.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View v) {
                           Intent intent = new Intent(getApplicationContext(),
                                         MyIntentService.class);
                           startService(intent);
                     }
              });
       }
}

Manifest.xml

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

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="18" />

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

</manifest>



No comments:

Post a Comment