模擬雙色球彩票的隨機選號器:


3.實例:模擬雙色球彩票的隨機選號器:Service2 這里使用的時Bind Service(綁定Service)
a.布局界面
b.創建Service,實現生成隨機的彩票號碼
i.創建一個service文件BinderService,創建一個MyBinder內部類,用於獲取Service對象和Service狀態
ii.返回一個有效的IBinder對象,用於實現Service和綁定它的組件之間進行通信
iii.自定義方法,用於生成隨機數
iiii.重寫onDestory方法,來銷毀Service
c.綁定Service,並獲取顯示隨機的彩票號碼
i.定義一個BinderService對象<=>聲明一個Service類的對象
ii.創建ServiceConnection對象
iii.在onCreate方法中調用BinderService方法中定義的獲取隨機數方法,並將結果顯示在文本框中
iiii.重寫onStart&&onStop方法,讓Service和Activity進行綁定

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

package com.example.service2;

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 BinderService extends Service {
public BinderService() {
}
//創建一個MyBinder內部類,用於獲取Service對象和Service狀態
public class MyBinder extends Binder{
//創建獲取Service的方法
public BinderService getService(){
return BinderService.this;//返回當前的Service類
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
//返回一個有效的IBinder對象,用於實現Service和綁定它的組件之間進行通信
return new MyBinder();//返回MyBinder對象
}
//
public List getRandomNumber(){
List resArr=new ArrayList();
//定義一個變量用於保存生成后的隨機數
String strNumber="";
for(int i=0;i<7;i++){
//生成指定范圍的隨機整數
int number=new Random().nextInt(33)+1;
//字符串格式化
if(number<10){
strNumber="0"+String.valueOf(number);
}else{
strNumber=String.valueOf(number);
}
resArr.add(strNumber);
}
return resArr;
}

@Override
public void onDestroy() {
super.onDestroy();
}
}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
package com.example.service2;

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 {
BinderService binderService;//聲明一個Service類的對象
//文本框組件ID
int[] tvid={R.id.textView1,R.id.textView2,R.id.textView3,R.id.textView4,R.id.textView5,R.id.textView6,R.id.textView7};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//要在這個方法中調用BinderService方法中定義的獲取隨機數方法
List number=binderService.getRandomNumber();
for(int i=0;i<number.size();i++){
TextView tv=findViewById(tvid[i]);
tv.setText(number.get(i).toString());//顯示生成的隨機號碼
}
}
});
}
//創建ServiceConnection對象
private ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
//獲取后台的Service
binderService=((BinderService.MyBinder)iBinder).getService();
}

@Override
public void onServiceDisconnected(ComponentName componentName) {

}
};

@Override
protected void onStart() {
super.onStart();
Intent intent=new Intent(this,BinderService.class);
bindService(intent,conn,BIND_AUTO_CREATE);
//第一個參數式conn對象,第二個式conn對象,第三個參數設定為自動創建Service
}

@Override
protected void onStop() {
super.onStop();
unbindService(conn);
}
}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隨機選號"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第1個號碼"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第2個號碼"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第3個號碼"/>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第4個號碼"/>
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第5個號碼"/>
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第6個號碼"/>
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第7個號碼"/>
</LinearLayout>

</RelativeLayout>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM