目錄
- 安卓實驗(Service)
- 1. 創建兩個按鈕控制元件
- 2. 創建用于計時的服務(Service)
- 3. 注冊TimerService服務
- 4. 按鈕功能實作
- 5. 進行除錯運行
安卓實驗(Service)
在Android Studio創建計時服務,并使用兩個按鈕進行控制
1. 創建兩個按鈕控制元件
在activity_main.xml中添加如下代碼新建兩個按鈕控制元件,也可在Design視窗中拖動控制元件進行添加
<Button
android:id="@+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="300dp"
android:text="@string/start_button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/stop_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="350dp"
android:text="@string/stop_button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

2. 創建用于計時的服務(Service)
新建java檔案TimerService.java,用于實作計時服務類,在檔案中新建TimerService類并重寫onCreate()、onBind()、onStartCommand()、onDestroy(),
我們在onStartCommand()方法中實作后臺計時功能,并在控制臺進行輸出,為了防止Service卡死Activity導致界面無法點擊,我們需要在該方法中創建一個執行緒來完成計時,回傳START_STICKY保證服務在后臺運行
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v("TimerService","onStartCommand");
new Thread(new Runnable() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void run(){
while(!threadDisable){
Log.v("TimerService","Timer is " + timer);
timer++;
try{
Thread.sleep(1000);
}catch (InterruptedException e){
}
}
}
}).start();
return START_STICKY;
}
TimerService.java完整代碼
import android.app.Service;
import android.content.ComponentName;
import android.os.Build;
import android.os.IBinder;
import android.content.Intent;
import android.util.Log;
import androidx.annotation.RequiresApi;
public class TimerService extends Service {
private boolean threadDisable;
private int timer;
@Override
public void onCreate() {
super.onCreate();
Log.v("TimerService","oncreat");
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v("TimerService","onStartCommand");
new Thread(new Runnable() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void run(){
while(!threadDisable){
Log.v("TimerService","Timer is " + timer);
timer++;
try{
Thread.sleep(1000);
}catch (InterruptedException e){
}
}
}
}).start();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
this.threadDisable = true;
Log.v("TimerService","onDestroy");
}
}
3. 注冊TimerService服務
創建完TimerServer服務類后需要在AndroidMainfest.xml中注冊才能使用
在AndroidMainfest.xml中添加
<service android:name=".TimerService"/>
4. 按鈕功能實作
在MainActivity中實作按鈕的功能,創建Intent物件,使用Intent物件來控制服務的開啟和關閉,
實作代碼
Button start_button = findViewById(R.id.start_button);
Button stop_button = findViewById(R.id.stop_button);
start_button.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
final Intent intent = new Intent(MainActivity.this, TimerService.class);
startService(intent);
}
});
stop_button.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
final Intent intent = new Intent(MainActivity.this, TimerService.class);
stopService(intent);
}
});
5. 進行除錯運行
在debug下運行app,點擊開始計時按鈕后在控制臺可查看當前計時時間,點擊停止計時可將服務停止,再次點擊開始計時會重新進行計時,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/157306.html
標籤:其他
上一篇:使用VS Code進行Qt開發
