crea dos clases tipo java,myForegroundService y myBroadCastReceiver
package com.example.foregroundservice;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent serviceIntent = new Intent(this, myForegroundService.class);
startForegroundService(serviceIntent);
foregroundServiceRunning();
}
public boolean foregroundServiceRunning(){
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for(ActivityManager.RunningServiceInfo service: activityManager.getRunningServices(Integer.MAX_VALUE)) {
if(myForegroundService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
package com.example.foregroundservice;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
public class myForegroundService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(
new Runnable() {
@Override
public void run() {
while (true){
Log.d("Tag","Foreground service is running...");
try {
Thread.sleep(2000);
}catch (InterruptedException e){
}
}
}
}
).start();
final String CHANNEL_ID = "Foreground Service";
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_LOW);
getSystemService(NotificationManager.class).createNotificationChannel(channel);
Notification.Builder notification = new Notification.Builder(this,CHANNEL_ID)
.setContentText("Foregroud service running")
.setContentTitle("this is title");
startForeground(1001,notification.build());
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
package com.example.foregroundservice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class myBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, myForegroundService.class);
context.startForegroundService(serviceIntent);
}
}
}
plugins {
id 'com.android.application'
}
android {
namespace 'com.example.foregroundservice'
compileSdk 32
defaultConfig {
applicationId "com.example.foregroundservice"
minSdk 26
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
}