1 servers的学习
1.1 servers的创建与功能的介绍
目录
首先先创建一个server
首先简单的创建一个xml页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/start_music_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="start" />
<Button
android:id="@+id/stop_music_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="stop" />
</LinearLayout>
然后再创建个页面绑定下按钮
package com.example.work;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Music_activity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);
Button start_button =findViewById(R.id.start_music_button);
Button stop_button =findViewById(R.id.stop_music_botton);
start_button.setOnClickListener(this);
stop_button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_music_button:
startService(new Intent(Music_activity.this,music_Service.class));
break;
case R.id.stop_music_botton:
stopService(new Intent(Music_activity.this,music_Service.class));
break;
}
}
}
接下来再创建个service
package com.example.work;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class music_Service extends Service {
public music_Service() {
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this,"onCreate: ", Toast.LENGTH_LONG).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this,"onStartCommand: ", Toast.LENGTH_LONG).show();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this,"onDestroy: ", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
1.2 媒体文件的创建
首先声明一个媒体播放器
private MediaPlayer musicplayer;
然后创建一个资源管理的文件夹
在这里面选择raw
然后就可以把你喜欢的音乐放入其中了
musicplayer=MediaPlayer.create(this,R.raw.happy_music);
通过这种方式可以读取你的音乐
musicplayer.start();
musicplayer.stop();
通过这两种方式可以启动和结束你的音乐
1.3 通过bind的方式来启动和结束音乐
依旧是新创建一对bind,然后绑定按钮
onbind的方式相比较之前的方式会多一种serviceconnection,首先要创建这一个serviceconnection
private ServiceConnection mserviceConnection =new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
bind启动音乐和结束音乐的方式
startService(new Intent(Music_activity.this,music_Service.class));
bindService(new Intent(Music_activity.this,music_Service.class),mserviceConnection,BIND_AUTO_CREATE);
unbindService(mserviceConnection);
stopService(new Intent(Music_activity.this,music_Service.class));
1.4 使用bind方式进行与serves的数据传输
先比较直接start,stop的方式,bind方式启动serves方式可以与serves进行数据交流,下面将介绍如何与serves进行数据交流
首先我们创建一个内部类
private IBinder iBinder=new LocalBinder();
public class LocalBinder extends Binder{
music_Service getService(){
return music_Service.this;
}
}
然后将ibinder进行一个return
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return iBinder;
}
private ServiceConnection mserviceConnection =new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
music_Service.LocalBinder localBinder = (music_Service.LocalBinder) service;
music_Service musicService= localBinder.getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
bind和serves的运行周期
如果你曾经绑定过,但是没有解绑,那么服务就无法被销毁
这样就可以通过自定义函数的方式,来进行数据的传递了
1.5 完整代码
Music_activity
package com.example.work;
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;
import android.widget.Button;
public class Music_activity extends AppCompatActivity implements View.OnClickListener {
private ServiceConnection mserviceConnection =new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
music_Service.LocalBinder localBinder = (music_Service.LocalBinder) service;
music_Service musicService= localBinder.getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);
Button start_button =findViewById(R.id.start_music_button);
Button stop_button =findViewById(R.id.stop_music_botton);
start_button.setOnClickListener(this);
stop_button.setOnClickListener(this);
Button bind_start_button =findViewById(R.id.bind_start_music_button);
Button bind_stop_button =findViewById(R.id.bind_stop_music_botton);
bind_start_button.setOnClickListener(this);
bind_stop_button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_music_button:
startService(new Intent(Music_activity.this,music_Service.class));
break;
case R.id.stop_music_botton:
stopService(new Intent(Music_activity.this,music_Service.class));
break;
case R.id.bind_start_music_button:
startService(new Intent(Music_activity.this,music_Service.class));
bindService(new Intent(Music_activity.this,music_Service.class),mserviceConnection,BIND_AUTO_CREATE);
break;
case R.id.bind_stop_music_botton:
unbindService(mserviceConnection);
stopService(new Intent(Music_activity.this,music_Service.class));
break;
}
}
}
music_Service
package com.example.work;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class music_Service extends Service {
private MediaPlayer musicplayer;
private IBinder iBinder=new LocalBinder();
public music_Service() {
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this,"onCreate: ", Toast.LENGTH_LONG).show();
musicplayer=MediaPlayer.create(this,R.raw.happy_music);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this,"onStartCommand: ", Toast.LENGTH_LONG).show();
musicplayer.start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
musicplayer.stop();
super.onDestroy();
Toast.makeText(this,"onDestroy: ", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return iBinder;
}
public class LocalBinder extends Binder{
music_Service getService(){
return music_Service.this;
}
}
}