Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday, 28 March 2016

Started Service

Hi friends

Service : In my words service is a part of five android component (Activity,Intent,BroadcastReciver, Service,ContentProvider) this component is running and perform long-running background operations  without needing and interaction user. There are divide two parts--

1 : Started Service 
2 : Bound Service

So in this blog we are study about Started Service

actvity_main.xml 


<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.samset.startedservice.MainActivity">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Simple Service" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/button2"
        android:layout_marginTop="37dp"
        android:background="@color/colorAccent"
        android:onClick="startService"
        android:padding="5dp"
        android:text="Start Service" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="63dp"
        android:background="@color/colorAccent"
        android:onClick="stopService"
        android:padding="10dp"
        android:text="Stop Service" />
    <TextView
        android:layout_marginTop="5dp"
        android:layout_below="@+id/button2"
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Using IntentService " />

    <EditText
        android:layout_marginTop="5dp"
        android:id="@+id/et"
        android:layout_below="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter no." />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="63dp"
        android:background="@color/colorAccent"
        android:onClick="intentservice"
        android:padding="10dp"
        android:text="Start IntentService" />


</RelativeLayout>


ActvityMain.java


import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.samset.startedservice.intentservice.MyIntentService;
import com.samset.startedservice.simpleservice.MyService;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    // Start the service
   
public void startService(View view) {

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

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

    public void intentservice()
    {
        EditText sleepTime=(EditText)findViewById(R.id.et);

        Long secondsToSleep=Long.parseLong(sleepTime.getText().toString());

        Intent intent= new Intent(MainActivity.this,MyIntentService.class);

        intent.putExtra("seconds", secondsToSleep);

        startService(intent);
    }

}

MyIntentService.java


import android.app.Activity;
import android.app.IntentService;
import android.content.Intent;
import android.os.SystemClock;
import android.text.format.DateFormat;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by samset on 28/03/16.
 */
public class MyIntentService extends IntentService {

    public long seconds;

    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     *
@param name Used to name the worker thread, important only for debugging.
     */
   
public MyIntentService(String name) {
        super(name);
    }

    public MyIntentService() {
        super("MyIntentService");
    }
    @Override
    protected void onHandleIntent(Intent intent) {

        seconds =intent.getExtras().getLong("seconds");
        long millis = seconds * 1000;
        try
       
{
            Thread.sleep(millis);
        }
        catch(InterruptedException e)
        {
            e.printStackTrace();
        }
    }

    public void onDestroy()
    {
        super.onDestroy();
        Toast.makeText(this, String.format("Slept %d seconds", seconds), Toast.LENGTH_SHORT).show();
    }
}


MyService.java


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

/**
 * Created by samset on 28/03/16.
 */
public class MyService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        Toast.makeText(this, "Service was Created", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // Perform your long running operations here.
       
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    }

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

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.samset.startedservice">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".simpleservice.MyService" android:enabled="true"/>
        <service android:name=".intentservice.MyIntentService"/>
    </application>

</manifest>

Thank you

Full Source code



Live Sample







No comments:

Post a Comment