郑州app开发android音乐播放器框架实例。由于只是实现框架代码,布局设计上不太追求,所以只是放置了四个button,在这里由于篇幅有限,就不再展示了。直接展示java代码。
MainActivity.java
package cn.xhhkj.cyd;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private Myservice.MyBinder musicControl;
private Myconnection conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent myservice = new Intent(this,Myservice.class);
conn =new Myconnection();
bindService(myservice,conn,BIND_AUTO_CREATE);
startService(myservice);
}
public class Myconnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
musicControl = (Myservice.MyBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
public void pre(View view) {
musicControl.callpre();
}
public void next(View view) {
musicControl.callnext();
}
public void play(View view) {
musicControl.callplay();
}
public void pause(View view) {
musicControl.callpause();
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(conn);
}
}
Myservice.java
package cn.xhhkj.cyd;
import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Binder;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import androidx.annotation.Nullable;
import java.io.IOException;
public class Myservice extends Service {
private static final String TAG = "服务是";
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
public class MyBinder extends Binder{
public void callpre(){
pre();
}
public void callnext(){
next();
}
public void callplay(){
play();
}
public void callpause(){
pause();
}
}
public void pre(){
Log.d(TAG,"上一首:郑州做个app多少钱");
}
public void next(){
Log.d(TAG,"下一首:郑州做个哪家好");
}
public void play(){
Log.d(TAG,"播放:app哪里比较正规");
}
public void pause(){
Log.d(TAG,"暂停:app分行业吗?");
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG,"开始");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG,"销毁");
}
}