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

實驗代碼:
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毫秒計數(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ù)的實例
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="計時器"
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="計時"
android:textSize="36sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
到此這篇關(guān)于android studio項目:綁定服務(wù)和線程實現(xiàn)計時器的文章就介紹到這了,更多相關(guān)android綁定服務(wù)和線程實現(xiàn)計時器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android 線程thread的兩種實現(xiàn)方法(必看)
下面小編就為大家?guī)硪黄狝ndroid 線程thread的兩種實現(xiàn)方法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
Android自定義SwipeLayout仿QQ側(cè)滑條目
這篇文章主要為大家詳細介紹了Android自定義SwipeLayout仿QQ側(cè)滑條目,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08
Android判斷軟鍵盤的狀態(tài)和隱藏軟鍵盤的簡單實例
下面小編就為大家?guī)硪黄狝ndroid判斷軟鍵盤的狀態(tài)和隱藏軟鍵盤的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
Android中ListView的幾種常見的優(yōu)化方法總結(jié)
Android中的ListView應(yīng)該算是布局中幾種最常用的組件之一,本篇文章主要做了三種優(yōu)化總結(jié),有興趣的可以了解一下。2017-02-02
Android編程使用內(nèi)容提供者方式(ContentProvider)進行存儲的方法
這篇文章主要介紹了Android編程使用內(nèi)容提供者方式進行存儲的方法,涉及Android內(nèi)容提供者的創(chuàng)建,配置及針對數(shù)據(jù)的增刪改查等操作技巧,需要的朋友可以參考下2016-01-01

