Skip to content

Instantly share code, notes, and snippets.

@Anass-ABEA
Created January 10, 2022 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Anass-ABEA/6ee49ef06f3a3e708a47cfffeca3db43 to your computer and use it in GitHub Desktop.
Save Anass-ABEA/6ee49ef06f3a3e708a47cfffeca3db43 to your computer and use it in GitHub Desktop.
public class MainActivity extends AppCompatActivity{
protected ServiceMusique service;
private boolean connected;
private final ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Toast.makeText(getApplicationContext(), "connected To service", Toast.LENGTH_SHORT).show();
ServiceMusique.MyBinder binder = (ServiceMusique.MyBinder) iBinder;
service = binder.service;
connected = true;
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
connected = false;
}
};
}
public class ServiceMusique extends Service {
public MediaPlayer player = new MediaPlayer();
private final IBinder binder = new ServiceBinder();
@Override
public void onCreate() {
Toast.makeText(this, "Service ceéé", Toast.LENGTH_SHORT).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service démarré", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Toast.makeText(this, "Service détruit", Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
public class MyBinder extends Binder {
public ServiceMusique getService() {
return ServiceMusique.this;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment