1

Foreground service class code for calling the shutDown broadcast receiver.

public class SmsForegroundService extends Service{
    private BroadcastReceiver receiver;
  
    public static class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // do something
            Log.d(TAG, "onReceive: "+intent.getAction());
            if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
                Log.d(TAG, "onReceive: ScreenOff");
            }
        }

        // constructor
        public MyReceiver(){

        }
    }
    

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate: ");
        IntentFilter filter = new IntentFilter(Intent.ACTION_SHUTDOWN);
        receiver = new MyReceiver();
        this.registerReceiver(receiver,filter);
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy: service destroyed");
        super.onDestroy();
        this.unregisterReceiver(receiver);
    }


    //
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG ,"service running");
        
        final String CHANNEL_ID = "foreground Service ID";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    CHANNEL_ID,
                    NotificationManager.IMPORTANCE_HIGH
            );
            getSystemService(NotificationManager.class).createNotificationChannel(channel);
            Notification.Builder notification = new Notification.Builder(this,CHANNEL_ID)
                    .setContentText("Service is running")
                    .setContentTitle("Service is enabled")
                    .setSmallIcon(R.drawable.ic_launcher_background);

            startForeground(1001, notification.build());
        }
        return super.onStartCommand(intent, flags, startId);
    }
    
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: ");
        return null;
    }
}

AndroidManifest file, here I added only the permission for Foreground services, I think there is no other permission is required for action_shutdown in manifest for versions greater than Oreo.

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

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <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/Theme.SmsBroadcast">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".SmsForegroundService">
        </service>
    </application>
</manifest>

MainActivity I have called the service class inside on Create method MainActivity class.

public class MainActivity extends AppCompatActivity {
    
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(!foregroundServiceRunning())
        {
            Intent serviceIntent  = new Intent(this, SmsForegroundService.class);
            ContextCompat.startForegroundService(this,serviceIntent);
        }
    }


    public boolean foregroundServiceRunning(){
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for(ActivityManager.RunningServiceInfo serviceInfo: activityManager.getRunningServices(Integer.MAX_VALUE)){
            if(SmsForegroundService.class.getName().equals(serviceInfo.service.getClassName())){
                return true;
            }
        }
        return false;
    }

}

Nns_ninteyFIve
  • 439
  • 4
  • 12

0 Answers0