Android service example | Android background bind service

Android service example tutorial start stop service

It is easy to run service in android run an background service it can be hidden service or an layout service. Android bind service return START_STICKY is help to run your application service automatically if it is closed by any reason.

package Write your package name here;

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

public class MainActivity extends AppCompatActivity {

    Button startMyService,stopMyService;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startMyService = (Button) findViewById(R.id.start_service);
        startMyService = (Button) findViewById(R.id.stop_service);

        startMyService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startService(new Intent(getApplicationContext(), MyService.class));
            }
        });

        stopMyService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stopService(new Intent(getApplicationContext(), MyService.class));
            }
        });
    }

}

Android service source code no deprecated in Nougat Oreo Marshmallow
Create a Service class name it as MyService where your MainActivity file is located.

package attraction.blessme.begin;

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

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "Service is Created", Toast.LENGTH_SHORT).show();
        // This area is like onCreate of Activity page.
        //You can set any code, Set music player code here or any thing else. Set notification.
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Toast.makeText(this, "Service is Started", Toast.LENGTH_SHORT).show();
        //You can open another activity when service is started.

        return START_STICKY;
    }

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

}

Service need to be close if you application is destroyed. Run an service without destroy itself.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#3bffd8"
    tools:context=".MainActivity">

    <TextView
        android:text="START STOP SERVICE"
        android:id="@+id/out"
        android:textStyle ="bold"
        android:textSize ="30sp"
        android:layout_width ="wrap_content"
        android:layout_height ="wrap_content"
        android:layout_alignParentBottom ="true"
        android:layout_centerHorizontal ="true"
        android:layout_marginBottom ="18dp">
    </TextView>

    <Button
        android:id="@+id/start_service"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="05dp"
        android:textColor="#ffffff"
        android:background="#128dff"
        android:textStyle="bold"
        android:textSize="25sp"
        android:text="START SERVICE"
        android:layout_above="@+id/stop_service"
        android:layout_alignEnd="@+id/out"
        android:layout_marginBottom="37dp"
        android:layout_alignParentStart="true" />

    <Button
        android:id="@+id/stop_service"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:text="STOP SERVICE"
        android:textColor="#ff0000"
        android:background="#ffffff"
        android:textStyle="bold"
        android:textSize="25sp"
        android:layout_centerVertical="true"
        android:layout_alignParentEnd="true"
        android:layout_alignStart="@+id/out" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:textStyle="bold"
        android:autoLink="web"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="NEXTGENEARN.COM" />

</RelativeLayout>

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

    <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=".MyService"/>
    </application>
</manifest>


No more about Android service from Android official page. Visit Blog NextGenEarn for more new
latest code no deprecation for newer version of Android.

Add a Comment

Your email address will not be published. Required fields are marked *