Created
January 10, 2022 09:44
-
-
Save Anass-ABEA/6ee49ef06f3a3e708a47cfffeca3db43 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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