简体   繁体   中英

Android call method in Service from Activity

I want to call a method in a Service object from an Activity object, however I have found it is not possible to call the method from the MainActivity normally.

I hope my code explains what I mean better:

Service:

public class Timer extends Service {

public Vibrator v;
public MainActivity ma;
public CountDownTimer mycounter;
public static final String MY_SERVICE = "de.example.timer.MY_SERVICE";

public IBinder onBind(Intent arg0) 
{
      return null;
}

public void onCreate() 
{
      super.onCreate();
      ma = new MainActivity();
      v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
      startService();
}

public void startService()
{
    mycounter = null; //delete counter
    mycounter = new CountDownTimer(5000, 100){
        public void onTick(long millisUntilFinished) {
            //ma.timer.setText(ma.formatTime(millisUntilFinished+1000));
            //ma.builder.setContentText("Timer: " + ma.formatTime(millisUntilFinished+1000)); //update Timer
            //ma.notificationManager.notify(MainActivity.MY_NOTIFICATION_ID, ma.builder.build());

//It is not possible to call a methode this way in a service..
        }

        public void onFinish() {
            //ma.timer.setText("00:00:00");
            v.vibrate(1000);
            mycounter.cancel();
            mycounter.start();
        }
    };
    mycounter.start();
}

public void onDestroy() 
{
      super.onDestroy();
      mycounter.cancel();


  }   
}

Activity:

public class MainActivity extends Activity { 

private ImageButton imagebutton;
public Vibrator v;
public TextView timer;
public boolean pressed;
public String output;
public long waitingtime;
public Timer service;

public static final int MY_NOTIFICATION_ID = 1;
public NotificationManager notificationManager;

public Context context;
public Builder builder;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    pressed = false;
    waitingtime = 600000; // Standartmäßig auf 10 min

    v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    imagebutton = (ImageButton) findViewById(R.id.imageButton1);
    imagebutton.setBackgroundResource(R.drawable.start);
    timer = (TextView) findViewById(R.id.timer);
    timer.setText(formatTime(waitingtime));

    Intent intent = new Intent (this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pend = PendingIntent.getActivity(this, 0, intent, 0);
    notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    context = getApplicationContext();
    builder = new NotificationCompat.Builder(context)
    .setSmallIcon(R.drawable.notlogosmall)
    .setContentTitle("Start!")
    .setContentText("Timer set to " + waitingtime/1000 + " seconds")
    .setContentIntent (pend)
    .setTicker("Go!")
    .setWhen(System.currentTimeMillis())
    .setDefaults(0)
    .setAutoCancel(false)
    .setOngoing(true);
}

public void press(View view){
    if(pressed == false){
        imagebutton.setBackgroundResource(R.drawable.stop);
        pressed = true;
        notificationManager.notify(MY_NOTIFICATION_ID, builder.build()); //Notification
        v.vibrate(100);
        hidebuttons();
        startService(new Intent(Timer.MY_SERVICE));
    }
    else{
        imagebutton.setBackgroundResource(R.drawable.start);
        pressed = false;
        notificationManager.cancel(1);
        timer.setText(formatTime(waitingtime));
        showbuttons();
        stopService(new Intent(Timer.MY_SERVICE));
        v.vibrate(100);
    }
}

How can I call one object's method from an object of another class?

The accepted answer isn't wrong but very unnecessarily complex.

Add this to the activity:

Intent i = new Intent(this, SERVICE_NAME.class);
i.setAction("YOUR_ACTION");
startService(i);

and this to the Service:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null && intent.getAction() != null && intent.getAction().equals("YOUR_ACTION")) {
        invokeServiceMethod(); // here you invoke the service method
    }
    return START_STICKY; // or whatever floats your boat
}

A simpke way is to send an intent from Activity and handle it in onStartCommand() method of Service. Don't forget to supply intent with right action/extras & check that in onStartCommand()

EDIT:

Activity:

Add a private class:

    private class CustomReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION_CUSTOM_ACTION)) {
            doCustomAction();
        }
    }
}

Add a private field:

private CustomReceiver mCustomReceiver;

In onCreate() method:

mCustomReceiver = new CustomReceiver();

In onResume() or other lifecycle method:

IntentFilter filter = new IntentFilter(ACTION_CUSTOM_ACTION);   
registerReceiver(mCustomReceiver , filter);

In onPause() or other paired (to previous step) lifecycle method

unregisterReceiver(mCustomReceiver );

In activity whenever you wish to call use Service methods:

startService(new Intent(SOME_ACTION));

In Service:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    if (intent == null) {
        return START_STICKY;
    }

    String action = intent.getAction();

    if (action == null) {
        return START_STICKY;
    } else if (action.equals(SOME_ACTION)) {
                    invokeSomeServiceMethod();// here you invoke service method
            }

    return START_STICKY;
}

Note, that START_STICKY may be not the best choice for you, read about modes in documentation.

Then, when you want to notify activity that you've finished, call:

startActivity(ACTION_CUSTOM_ACTION);

This will trigger broadcast reciever where you can handle finish event.

Seems that that's a lot of code, but really nothing difficult.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM