【实验名称】实验三、启动式service
【实验目的】
充分理解Service的作用,与Activity之间的区别,掌握Service的生命周期以及对应函数,了解Service的主线程性质;掌握主线程的界面刷新的设计原则,掌握启动service的方式,及其工作原理;
【实验内容】
任务1:在service中实现随机数产生;
任务2:在Activity界面实现随机数的显示,并采用启动式完成service的启动;
【实验要求】
1、在service中实现随机数产生;
2、实现Service中的各个生命周期函数,并理解其功能;
2、在Activity界面实现随机数的显示,每2秒更新一次;
3、采用启动式完成service的启动;
从csdn上面找了几个博主看了他们写的,奈何他们都用的是线程,这块我是真不懂,拿了本java书回去看,结果打开电脑发现成功了,(没发现的原因是自己让service休眠时间太长了,结果没看到)
Main_Activity
package com.example.test3;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btn1, btn2;
static TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = findViewById(R.id.text);
btn1 = findViewById(R.id.button1);
btn2 = findViewById(R.id.button2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MyService.class);
stopService(intent);
}
});
}
public static void update(double random) {
text.setText(String.valueOf(random));
}
}
MyService
package com.example.test3;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private boolean quit;
private int count;
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();
Log.d("My Service", "onCreate executed");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread()
{
public void run()
{
while(!quit)
{
try
{
Thread.sleep(2000);
}catch(InterruptedException e){e.printStackTrace();}
double random = Math.random() * 100;
MainActivity.update(random);
count++;
Log.d("My Service", String.valueOf(count));
}
};
}.start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.d("My Service", "onDestroy executed");
quit = true;
super.onDestroy();
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="随机数" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text2" />
</LinearLayout>
不过书上说一直让service持续似乎不太好,等有时间再改