@bart12k

Как внутри сервиса вызвать Context?

Вопрос банальный, но.
Есть код MainActivity оттуда я просто запускаю сервис:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent serviceIntent = new Intent(this, MyService.class);
        startService(serviceIntent);


        setContentView(R.layout.activity_main);

    }


Код сервиса:
public class MyService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("debug", "service Oncreate" );
    }

    public MyService() {


     //Как тут получить Context ???

        Log.d("debug", "код сервиса ");


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("debug", "сервис уничтожен");
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

        Log.i("debug", "service запущен" + startId);

        return START_STICKY;
    }
    public void onRebind(Intent intent) {
        super.onRebind(intent);
        Log.i("debug", "перезапуск");
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }




}
  • Вопрос задан
  • 99 просмотров
Пригласить эксперта
Ответы на вопрос 1
iLLuzor
@iLLuzor
Java, Kotlin, Android Developer
Класс Service расширяет Context. Так что просто this.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы