サービスクラスからアクティビティクラスメソッドを呼び出す 質問する

サービスクラスからアクティビティクラスメソッドを呼び出す 質問する

これに関して SO で多くの投稿を見てきましたが、サービス クラスからアクティビティ メソッドを呼び出す正確かつ最も簡単な方法がわかりませんでした。ブロードキャスト レシーバーのみがオプションですか? 簡単な方法はありませんか? メディア プレーヤーがサービス クラスで準備された後、アクティビティ クラスで次のメソッドを呼び出す必要があります。

アクティビティクラス:

    public void updateProgress() {
    // set Progress bar values
    songProgressBar.setProgress(0);
    songProgressBar.setMax(100);
    // Updating progress bar
    updateProgressBar();
}

サービスクラス:

   @Override
public IBinder onBind(Intent intent) {
    Log.d(this.getClass().getName(), "BIND");
    return musicBind;
}

@Override
public boolean onUnbind(Intent intent) {
    return false;
}
    @Override
public void onPrepared(MediaPlayer mp) {
    try {
        mp.start();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    }

           // updateProgress();// Need to call the Activity method here 
  }

ベストアンサー1

サービスがイベントを通信するために使用するインターフェースを定義します。

public interface ServiceCallbacks {
    void doSomething();
} 

サービスクラスを記述します。アクティビティはこのサービスにバインドされるので、ここにサンプルを表示. さらに、 を設定するメソッドを追加しますServiceCallbacks

public class MyService extends Service {
    // Binder given to clients
    private final IBinder binder = new LocalBinder();
    // Registered callbacks
    private ServiceCallbacks serviceCallbacks;


    // Class used for the client Binder.
    public class LocalBinder extends Binder {
        MyService getService() {
            // Return this instance of MyService so clients can call public methods
            return MyService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    public void setCallbacks(ServiceCallbacks callbacks) {
        serviceCallbacks = callbacks;
    }
}

同じガイドに従って Activity クラスを記述しますが、ServiceCallbacks インターフェースも実装するようにしてください。サービスにバインド/アンバインドする場合は、setCallbacksサービスを呼び出して登録/登録解除します。

public class MyActivity extends Activity implements ServiceCallbacks {
    private MyService myService;
    private boolean bound = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(...);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // bind to Service
        Intent intent = new Intent(this, MyService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // Unbind from service
        if (bound) {
            myService.setCallbacks(null); // unregister
            unbindService(serviceConnection);
            bound = false;
        }
    }

    /** Callbacks for service binding, passed to bindService() */
    private ServiceConnection serviceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            // cast the IBinder and get MyService instance
            LocalBinder binder = (LocalBinder) service;
            myService = binder.getService();
            bound = true;
            myService.setCallbacks(MyActivity.this); // register
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            bound = false;
        }
    };

    /* Defined by ServiceCallbacks interface */
    @Override
    public void doSomething() {
        ...
    }
}

サービスがアクティビティと通信する必要がある場合は、先ほどのインターフェース メソッドの 1 つを呼び出すだけです。サービス内では、次のようになります。

if (serviceCallbacks != null) { 
    serviceCallbacks.doSomething();
}

おすすめ記事