目录
- 实验目的
- 实验内容
- 实验要求
- 项目结构
- 代码实现
- 结果展示
实验目的
充分理解Service的作用,与Activity之间的区别,掌握Service的生命周期以及对应函数,了解Service的主线程性质;掌握主线程的界面刷新的设计原则,掌握启动service的方式,及其工作原理
实验内容
任务1:在service中实现随机数产生;
任务2:在Activity界面实现随机数的显示,并采用启动式完成service的启动;
实验要求
1、在service中实现随机数产生;
2、实现Service中的各个生命周期函数,并理解其功能;
2、在Activity界面实现随机数的显示,每2秒更新一次;
3、采用启动式完成service的启动;
项目结构
代码实现
mainActivity
import android.app.Service;
import android.content.Intent;
import android.util.Log;
import android.os.IBinder;
import java.util.Random;
public class RandomNumberService extends Service {
private static final String TAG="RandomNumberService";//日志标签
private static final Random random=new Random();//随机数生成器
private static int randomNumber;//随机数的变量
@Override
public void onCreate(){
super.onCreate();
Log.d(TAG,"onCreate:Service is Created");//打印日志
}
@Override
public int onStartCommand(Intent intent,int flags,int startID){
Log.d(TAG,"onStartCommand:Service activity");
randomNumber=random.nextInt(1000);//打印随机数
return super.onStartCommand(intent,flags,startID);
}
@Override
public void onDestroy(){
super.onDestroy();
Log.d(TAG,"drop Service");
}
@Override
public IBinder onBind(Intent intent){
return null;//不支持绑定服务
}
public static int getRandomNumber(){
return randomNumber;
}
}
RandomNumberService
import android.app.Service;
import android.content.Intent;
import android.util.Log;
import android.os.IBinder;
import java.util.Random;
public class RandomNumberService extends Service {
private static final String TAG="RandomNumberService";//日志标签
private static final Random random=new Random();//随机数生成器
private static int randomNumber;//随机数的变量
@Override
public void onCreate(){
super.onCreate();
Log.d(TAG,"onCreate:Service is Created");//打印日志
}
@Override
public int onStartCommand(Intent intent,int flags,int startID){
Log.d(TAG,"onStartCommand:Service activity");
randomNumber=random.nextInt(1000);//打印随机数
return super.onStartCommand(intent,flags,startID);
}
@Override
public void onDestroy(){
super.onDestroy();
Log.d(TAG,"drop Service");
}
@Override
public IBinder onBind(Intent intent){
return null;//不支持绑定服务
}
public static int getRandomNumber(){
return randomNumber;
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Exp3"
tools:targetApi="31">
<activity
android:name=".mainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.Exp3">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".RandomNumberService" />
</application>
</manifest>
结果展示
每隔2s刷新一个随机数字