Мне нужно передать значение классу обслуживания.
Вот как я вызываю сервис из активности:
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();
}
}
Я изо всех сил пытаюсь передать ценность класса обслуживания. Может ли мне помочь с этим любой доброволец?
Вы можете передавать параметры с помощью 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");
}
}