Мне нужно передать значение классу обслуживания - PullRequest
0 голосов
/ 12 сентября 2018

Мне нужно передать значение в класс обслуживания.

Так я называю службу по активности:

try {
  Intent lintent = new Intent(getApplicationContext(), BackgroundService.class);
  getApplicationContext().stopService(lintent);

  startService (new Intent(this, BackgroundService.class));
}catch (Exception s){}

Это моя услуга:

public class BackgroundService extends Service {

public Context context = this;
public Handler handler = null;
public static Runnable runnable = null;
String obj;

@Override
public IBinder onBind(Intent intent) {

    return null;
}

@Override
public void onCreate() {
    Toast.makeText(this, "Service created!", Toast.LENGTH_LONG).show();

    handler = new Handler();
    runnable = new Runnable() {
        public void run() {

            Toast.makeText(getApplicationContext(), obj  , Toast.LENGTH_LONG).show();
            handler.postDelayed(runnable, 10000);
        }
    };

    handler.postDelayed(runnable, 15000);
}

@Override
public void onDestroy() {
    /* IF YOU WANT THIS SERVICE KILLED WITH THE APP THEN UNCOMMENT THE FOLLOWING LINE */
    handler.removeCallbacks(runnable);
    Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "Service started by user.", Toast.LENGTH_LONG).show();
}
}

Я изо всех сил пытаюсь передать ценность классу обслуживания, может ли любой волонтер помочь мне с этим

1 Ответ

0 голосов
/ 12 сентября 2018

Вы можете передать параметры с помощью Intent:

Intent serviceIntent = new Intent(this,ListenLocationService.class); 
   serviceIntent.putExtra("ID", "1234");
   startService(serviceIntent);

и получите параметр в методе onStart вашего класса обслуживания

@Override
public void onStart(Intent intent, int startId) {
  super.onStart(intent, startId);
  Bundle extras = intent.getExtras(); 
  if(extras != null) {
    String stringID = (String) extras.get("ID");
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...