AndroidManifest.xml:
MyService.java: Create MyService.java file. Service's don't have its own layout. Hence no XML file required.
MainActivity.java:
activity_main.xml:
Output:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.servicedemo"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" 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>
MyService.java: Create MyService.java file. Service's don't have its own layout. Hence no XML file required.
package com.example.servicedemo;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.Nullable;
public class MyService extends Service {
private Handler handler;
private Runner runner;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
handler = new Handler();
runner = new Runner();
}
public int onStartCommand(Intent intent, int id, int startID) {
handler.post(runner);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacks(runner);
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
public class Runner implements Runnable {
@Override
public void run() {
Log.d("AndroidClarified", "Running");
handler.postDelayed(this, 1000 * 5);
}
}
}
MainActivity.java:
package com.example.servicedemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button startButton, stopButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = findViewById(R.id.start_button);
stopButton = findViewById(R.id.stop_button);
startButton.setOnClickListener(this);
stopButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(this, MyService.class);
switch (v.getId()) {
case R.id.start_button:
startService(intent);
break;
case R.id.stop_button:
stopService(intent);
break;
}
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="horizontal"> <Button android:id="@+id/start_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" android:text="Start Service" /> <Button android:id="@+id/stop_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" android:text="Stop Service" /> </LinearLayout> </RelativeLayout>
Output:
