Home » Android

How to use Services in Android?

In this article, we are going to learn about services and how to enable it in Android?
Submitted by Manu Jemini, on February 25, 2018

In the example below, we are going to use the Activity and Service class to make and start the service, so that you can use it in the same screen.

To implement this in your program you will need to import: android.content.Activity, android.app.Service in your java file.

<Button
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="Click here to start Services"
	android:id="@+id/button"
	android:onClick="start"
	android:layout_alignParentBottom="true"
	android:layout_centerHorizontal="true"
	android:layout_marginBottom="114dp" />

In the Button above we have put an onClick property and set it to start function which is in the java File. We assign on click function directly like this : android:onClick=" start ".

Now whenever the user presses the button a function with name save will get call and that function will start the Service.

    startService(new Intent(getBaseContext(),service.class));

This will Start the service and when the service get called a function will called which will Toast a text so to give a signal that service has started.

    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG ). show();
    return START_STICKY;

1) Java file 1:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends Activity
{

	String msg= "Android : ";
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(msg,"Some event");
    }

    public void start(View v)
    {
       startService(new Intent(getBaseContext(),service.class));
        Toast.makeText(this,"Service Started",Toast.LENGTH_LONG).show();

    }
}

2) Java file 2:

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

/**
 * Created by hp on 2/2/2018.
 */

public class service extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Toast.makeText(this,"Service Started",Toast.LENGTH_LONG).show();
        return START_STICKY;
    }

}

3) XML file:

<?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.example.hp.myapplication.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click here to start Services"
        android:id="@+id/button"
        android:onClick="start"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="114dp" />

</RelativeLayout>

4) Android Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.myapplication">
<uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

    <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>
    </application>

</manifest>

Output

Android - Use services 1
Android - Use services 2







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.