android?studio項(xiàng)目:綁定服務(wù)和線程實(shí)現(xiàn)計(jì)時器
實(shí)驗(yàn)?zāi)康模?/strong>
熟悉和掌握Android線程的使用
實(shí)驗(yàn)要求:
- 1.完成一個秒表,具備啟停功能
- 2.通過綁定服務(wù)實(shí)現(xiàn)功能,通過Thread+handler更新界面
這章節(jié)沒花什么時間去學(xué),其他事情又很多,所以只是簡單實(shí)現(xiàn)了一下,在生命周期那里還是有些沒處理的地方,因此
主要思路是:在服務(wù)中啟動一個線程實(shí)現(xiàn)計(jì)數(shù)的功能,并且每隔10ms調(diào)用一下更新界面的函數(shù),這需要用到Thread+handler,當(dāng)然還需要一些控制啟停的公有函數(shù)供activity調(diào)用,同過綁定的服務(wù)的方式,activity中可以獲得服務(wù)的實(shí)例,所以以activity作為控制器,對不同的按鈕事件調(diào)用service的控制啟停的函數(shù)或者計(jì)數(shù)清零的函數(shù),以此來實(shí)現(xiàn)計(jì)時器的功能。完成實(shí)驗(yàn)后發(fā)現(xiàn)這樣實(shí)現(xiàn)的計(jì)時器精度比較粗糙,不過功能正常,更好的思路是使用時間函數(shù),不過在本次實(shí)驗(yàn)的目的是練習(xí)線程和綁定服務(wù)的使用,因此沒有繼續(xù)改動。

實(shí)驗(yàn)代碼:
MyService .java
package com.example.shiyan5;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class MyService extends Service {
private final IBinder binder = new MyBinder();
private Thread workThread;
private int count=0;
private boolean c_stop=true;
public MyService() {
}
public void clearcount()
{
count=0;
}
public void countstop(){
c_stop=true;
}
public void countstart(){
c_stop=false;
}
@Override
public void onCreate() {
super.onCreate();
workThread=new Thread(null,backgroundWork);
workThread.start();
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
public class MyBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return binder;
//throw new UnsupportedOperationException("Not yet implemented");
}
private Runnable backgroundWork =new Runnable() {
@Override
public void run() {
try {
while(true)
{
if(c_stop==false)
{
count++;
}
MainActivity.UpdateGUI(count);
Thread.sleep(10);//10毫秒計(jì)數(shù)一次Z
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}
MainActivity.java
package com.example.shiyan5;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
static TextView textView1,textView2;
Button bt_clear,bt_stop,bt_start;
MyService mService;
boolean mBound;
static int count;
static Handler handler=new Handler();
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
MyService.MyBinder binder = (MyService.MyBinder) service;
mService = binder.getService();//通過這個來獲取服務(wù)的實(shí)例
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
public static void UpdateGUI(int s_count)
{
count=s_count;
handler.post(RefreshText);
}
private static Runnable RefreshText=new Runnable() {
@Override
public void run() {
String sa,sb,sc;
int a=count%100;
if(a<10)sa="0"+a;else sa=String.valueOf(a);
int b=(count/100)%60;
if(b<10)sb="0"+b;else sb=String.valueOf(b);
int c=(count/100/60)%60;
if(c<10)sc="0"+c;else sc=String.valueOf(c);
textView2.setText(sc+":"+sb+":"+sa);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBound=false;
textView1=(TextView) findViewById(R.id.textview);
textView2=(TextView) findViewById(R.id.textview_2);
bt_clear=(Button) findViewById(R.id.bt_clear);
bt_stop=(Button) findViewById(R.id.bt_stop);
bt_start=(Button) findViewById(R.id.bt_start);
bt_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mBound==true){
mService.clearcount();
mService.countstop();
}
}
});
bt_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mBound==true)
{
mService.countstart();
}
}
});
bt_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mBound==true)
{
mService.countstop();
}
}
});
}
@Override
protected void onStart() {
super.onStart();
Intent intent=new Intent(this,MyService.class);
bindService(intent,connection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
unbindService(connection);
mBound=false;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/textview"
android:layout_gravity="center_horizontal"
android:text="計(jì)時器"
android:textSize="46sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/textview_2"
android:gravity="center"
android:textSize="54sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/bt_clear"
android:text="清零"
android:textSize="36sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/bt_stop"
android:text="暫停"
android:textSize="36sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/bt_start"
android:text="計(jì)時"
android:textSize="36sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
到此這篇關(guān)于android studio項(xiàng)目:綁定服務(wù)和線程實(shí)現(xiàn)計(jì)時器的文章就介紹到這了,更多相關(guān)android綁定服務(wù)和線程實(shí)現(xiàn)計(jì)時器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android上傳多張圖片的實(shí)例代碼(RxJava異步分發(fā))
本篇文章主要介紹了Android上傳多張圖片的實(shí)例代碼(RxJava異步分發(fā)),具有一定的參考價值,有興趣的可以了解一下2017-08-08
Android性能調(diào)優(yōu)利器StrictMode應(yīng)用分析
StrictMode意思為嚴(yán)格模式,是用來檢測程序中違例情況的開發(fā)者工具。最常用的場景就是檢測主線程中本地磁盤和網(wǎng)絡(luò)讀寫等耗時的操作。這篇文章給大家介紹Android性能調(diào)優(yōu)利器StrictMode應(yīng)用分析,感興趣的朋友一起看看吧2018-01-01
android 使用kotlin 實(shí)現(xiàn)點(diǎn)擊更換全局語言(中日英切換)
這篇文章主要介紹了android kotlin 點(diǎn)擊更換全局語言的實(shí)現(xiàn)方法,這里主要介紹中日英切換,需要的朋友可以參考下2019-11-11
CDC與BG-CDC的含義電容觸控學(xué)習(xí)整理
今天小編就為大家分享一篇關(guān)于CDC與BG-CDC的含義電容觸控學(xué)習(xí)整理,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
SimpleCommand實(shí)現(xiàn)上傳文件或視頻功能(四)
這篇文章主要介紹了SimpleCommand實(shí)現(xiàn)上傳文件或視頻功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Android自定義View實(shí)現(xiàn)雪花特效
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)雪花特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-02-02

