Service
service是一种服务,可以脱离activity运行,就好像网易云音乐的后台播放等等;
分类
具有Stat Services 和Bound Service两种
1.创建services
右键点击创建services即可,如果手动创建需要在xml中配置信息
2.使用services
public class MyService extends Service {
static boolean isplay;
MediaPlayer player;
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
player=MediaPlayer.create(this,R.raw.who);
Log.i("Service","oncreat方法调用");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(!player.isPlaying())
{
player.start();
isplay=player.isPlaying();
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.i("Service","销毁方法调用");
player.stop();
isplay=player.isPlaying();
player.release();
super.onDestroy();
}
调用方法播放音乐;
services的生命周期和activity相似
3.启动services就完事了
Intent intent=new Intent(services.this,MyService.class);
startService(intent);
Bound Service
boundservice是将services与activity连接起来的一种services。
他可以实现与activity共消失和创建,另外可以调用services里的方法。
不是很懂,待探究
package com.example.demo;
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;
import android.widget.TextView;
import java.util.List;
public class caipiao extends AppCompatActivity {
caipiaoservise caipiaoservise;
int[] tvid={R.id.h1,R.id.h2,R.id.h3,R.id.h4,R.id.h5};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_caipiao);
Button button= findViewById(R.id.xuan);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<String> number=caipiaoservise.getnumb();
for(int i=0;i<number.size();i++)
{
TextView tv=(TextView)findViewById(tvid[i]);
tv.setText(number.get(i).toString());
}
}
});
}
//创建servicescon
private ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
caipiaoservise=((caipiaoservise.MyBinder)service).getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onStart() {
super.onStart();
Intent intent=new Intent(caipiao.this,caipiaoservise.class);
bindService(intent,conn,BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(conn);
}
}
package com.example.demo;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class caipiaoservise extends Service {
public caipiaoservise() {
}
public class MyBinder extends Binder{
public caipiaoservise getService()
{
return caipiaoservise.this;
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
return new MyBinder();
}
public List<String> getnumb()
{
String n;
ArrayList<String> arrayList=new ArrayList<>();
for (int i=0;i<5;i++) {
int numb = new Random().nextInt(32);
if (numb < 10) {
n = "0" + numb;
}
else
{
n=String.valueOf(numb);
}
arrayList.add(n);
}
return arrayList;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}