实例
1、需求:使用Bound Service实现随机筛选多个数字
2、Myservice.java
package com.example.service_page;
import android.app.ActivityManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// 返回binder实例
return new MyBinder();
}
// Service被创建的时候调用
@Override
public void onCreate() {
super.onCreate();
}
// 创建内部binder类
public class MyBinder extends Binder{
// 获取当前Service实例
public MyService getService(){
return MyService.this;
}
}
// 筛选随机数
public List<String> listRandomNum(){
int randomNum ;
String result = new String();
List<String> resultList = new ArrayList();
for(int i = 0 ; i < 4 ; i++){
randomNum = new Random().nextInt(33)+1;
if(randomNum < 10){
result = "0"+randomNum;
}else{
result = ""+randomNum;
}
resultList.add(result);
}
return resultList;
}
}
3、MainActivity.java
package com.example.service_page;
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 MainActivity extends AppCompatActivity {
MyService myService = null;
Intent intent = null;
int[] textViews = new int[]{R.id.num1, R.id.num2,R.id.num3,R.id.num4};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startSelect = findViewById(R.id.startSelect);
startSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<String> list = myService.listRandomNum();
for(int i = 0; i < list.size() ; i++){
TextView textView = findViewById(textViews[i]);
textView.setText(list.get(i));
}
}
});
}
// Service连接对象
private ServiceConnection serviceConnection = new ServiceConnection() {
// Service与组件完成绑定调用的方法
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
// 获取Service对象, iBinder是在onBind对象中进行返回
myService = ((MyService.MyBinder)iBinder).getService();
}
// Service与组件完成解除绑定调用的方法
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
@Override
protected void onStart() {
super.onStart();
// 启动绑定Service并
intent = new Intent(MainActivity.this, MyService.class);
bindService(intent, serviceConnection, BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
// 取消绑定
unbindService(serviceConnection);
}
}
4、activity-main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="4"
>
<TextView
android:gravity="center"
android:id="@+id/num1"
android:layout_columnWeight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
<TextView
android:gravity="center"
android:id="@+id/num2"
android:layout_columnWeight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
<TextView
android:gravity="center"
android:id="@+id/num3"
android:layout_columnWeight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
<TextView
android:gravity="center"
android:id="@+id/num4"
android:layout_columnWeight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
</GridLayout>
<Button
android:id="@+id/startSelect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="随机筛选"
></Button>
</LinearLayout>