'How to create Sharedpreferences in Service class to get some value in android

This is my Service class :

public class MyService extends Service {
    private String TAG = "MyService";
    public static boolean isServiceRunning;
    private String CHANNEL_ID = "NOTIFICATION_CHANNEL";
    AppPreference appPreference;
    public MyService() {
        Log.d(TAG, "constructor called");
        appPreference = new AppPreferenceImpl(getApplicationContext());
        isServiceRunning = false;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate called");
        createNotificationChannel();
        isServiceRunning = true;
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand called");
        Intent notificationIntent = new Intent(this, SplashActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);
        Notification notification;
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setContentTitle(appPreference.getNotificationMsg().getMainText())
                    .setContentText(appPreference.getNotificationMsg().getSubText())
                    .setSmallIcon(R.mipmap.ic_feedback_background_new)
                    .setContentIntent(pendingIntent)
                    .setColor(getResources().getColor(R.color.colorPrimary))
                    .build();

        } else {
            notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setContentTitle(appPreference.getNotificationMsg().getMainText())
                    .setContentText(appPreference.getNotificationMsg().getSubText())
                    .setSmallIcon(R.drawable.ic_foreground)
                    .setContentIntent(pendingIntent)
                    .setColor(getResources().getColor(R.color.colorPrimary))
                    .build();
        }
        startForeground(1, notification);
        return START_STICKY;
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String appName = getString(R.string.app_name);
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    appName,
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
        }
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy called");
        isServiceRunning = false;
        stopForeground(true);
        super.onDestroy();
    }
}

i am calling service from MainActivity using below code

if (!MyService.isServiceRunning) {
            val serviceIntent = Intent(this, MyService::class.java)
            ContextCompat.startForegroundService(this, serviceIntent)
        }

I want to get value from shared preferences to get value which i am passing to Notification title and msg but when i run then i am unable to create Object of shared preference class please help me how to get value from sharedpreference in service .



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source