Answer the question
In order to leave comments, you need to log in
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);
}
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question