Q:

Cannot use findViewById in Service,used LayoutInflater but did not help,no value shown

I developed an app to count footsteps,I wanted it to be able to work in background [service]
There is a problem with findViewbyId using services mentioned here:
Android findViewById in service
I tried this approach,but the value is always 0,what i did wrong?
This is mine MainActivity class:
public class MainActivity extends AppCompatActivity {
private  final int PHYISCAL_ACTIVITY = 0;
private Button btnStopService;
private Button btnStartService;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACTIVITY_RECOGNITION) == PackageManager.PERMISSION_DENIED) {
        //ask for permission
        requestPermissions(new String[]{Manifest.permission.ACTIVITY_RECOGNITION}, PHYISCAL_ACTIVITY);
    }
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    btnStartService =  findViewById(R.id.btnStartService);
    btnStopService = findViewById(R.id.btnStopService);
    initButtonsOnClick();
}

private void initButtonsOnClick() {
    OnClickListener listener = new OnClickListener() {
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btnStartService:
                    startMyService();
                    break;
                case R.id.btnStopService:
                    stopMyService();
                    break;
                default:
                    break;
            }
        }
    };
    btnStartService.setOnClickListener(listener);
    btnStopService.setOnClickListener(listener);
}

private void startMyService() {
    Intent serviceIntent = new Intent(this, MyService.class);
    startService(serviceIntent);
}

private void stopMyService() {
    Intent serviceIntent = new Intent(this, MyService.class);
    stopService(serviceIntent);
}}

This is my service class:
public class MyService extends Service implements SensorEventListener {
private Toast toast;
private Timer timer;
private TimerTask timerTask;
private SensorManager sensorManager;
private Sensor mineSensor;
int Steps;

private TextView textView;

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
    if (sensorEvent.sensor == mineSensor) {
        Steps= (int) sensorEvent.values[0];
        textView.setText(String.valueOf(Steps));
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {

}

private class MyTimerTask extends TimerTask {
    @Override
    public void run() {
        showToast("Your service is still working");
    }
}

private void showToast(String text) {
    toast.setText(text);
    toast.show();
}

private void writeToLogs(String message) {
    Log.d("HelloServices", message);
}

@Override
public void onCreate() {
    super.onCreate();
    writeToLogs("Called onCreate() method.");
    timer = new Timer();
    toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    writeToLogs("Called onStartCommand() methond");
    clearTimerSchedule();
    initTask();
    timer.scheduleAtFixedRate(timerTask, 4 * 1000, 4 * 1000);
    showToast("Your service has been started");
    return super.onStartCommand(intent, flags, startId);
}

private void clearTimerSchedule() {
    if(timerTask != null) {
        timerTask.cancel();
        timer.purge();
    }
}

private void initTask() {
    timerTask = new MyTimerTask();
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.activity_main, null);
    textView = (TextView) layout.findViewById(R.id.image);
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    if (sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER) != null) {
        mineSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);

    } else {

        textView.setText("No Sensor ");

    }
}

@Override
public void onDestroy() {
    writeToLogs("Called onDestroy() method");
    clearTimerSchedule();
    showToast("Your service has been stopped");
    super.onDestroy();
}

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

A:

You need to register a broadcast reciever in main activity to get value from service like this:
private BroadcastReceiver broadcastReceiver;

if(broadcastReceiver == null){
                    broadcastReceiver = new BroadcastReceiver() {
                        @Override
                        public void onReceive(Context context, Intent intent) {
                            // here you get the values from service 
                //get values from intent and set it to your layout

                        }
                    };
registerReceiver(broadcastReceiver,new IntentFilter("my_reciever"));
                Intent i =new Intent(getApplicationContext(),yourservice.class);
                startService(i);
                   
                }
               

In service class:
Intent i = new Intent("my_reciever");
                i.putExtra("myvalues",yourvalues);
                sendBroadcast(i);
    
                
      

