B
B
bart12k2019-07-31 12:36:40
Java
bart12k, 2019-07-31 12:36:40

How to call Context inside a service?

The question is banal, but.
There is MainActivity code from there I just start the service:

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);

    }

Service code:
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");
    }




}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
illuzor, 2019-07-31
@iLLuzor

The Service class extends Context. So just this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question