亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android?studio項(xiàng)目:綁定服務(wù)和線程實(shí)現(xiàn)計(jì)時(shí)器

 更新時(shí)間:2022年01月25日 09:32:48   作者:wow_awsl_qwq  
這篇文章主要介紹了android?studio項(xiàng)目:綁定服務(wù)和線程實(shí)現(xiàn)計(jì)時(shí)器,完成一個(gè)秒表,具備啟停功能,通過綁定服務(wù)實(shí)現(xiàn)功能,通過Thread+handler更新界面,需要的朋友可以參考一下

實(shí)驗(yàn)?zāi)康模?/strong>

熟悉和掌握Android線程的使用

實(shí)驗(yàn)要求:

  • 1.完成一個(gè)秒表,具備啟停功能
  • 2.通過綁定服務(wù)實(shí)現(xiàn)功能,通過Thread+handler更新界面

這章節(jié)沒花什么時(shí)間去學(xué),其他事情又很多,所以只是簡單實(shí)現(xiàn)了一下,在生命周期那里還是有些沒處理的地方,因此

主要思路是:在服務(wù)中啟動(dòng)一個(gè)線程實(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í)器的功能。完成實(shí)驗(yàn)后發(fā)現(xiàn)這樣實(shí)現(xiàn)的計(jì)時(shí)器精度比較粗糙,不過功能正常,更好的思路是使用時(shí)間函數(shù),不過在本次實(shí)驗(yàn)的目的是練習(xí)線程和綁定服務(wù)的使用,因此沒有繼續(xù)改動(dòng)。

實(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();//通過這個(gè)來獲取服務(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ì)時(shí)器"
        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ì)時(shí)"
            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ì)時(shí)器的文章就介紹到這了,更多相關(guān)android綁定服務(wù)和線程實(shí)現(xiàn)計(jì)時(shí)器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android中巧妙的實(shí)現(xiàn)緩存詳解

    Android中巧妙的實(shí)現(xiàn)緩存詳解

    采用緩存,可以進(jìn)一步大大緩解數(shù)據(jù)交互的壓力,有的時(shí)候?yàn)榱丝焖俨樵儠?huì)被多次調(diào)用的數(shù)據(jù),或者構(gòu)建比較廢時(shí)的實(shí)例,我們一般使用緩存的方法。無論大型或小型應(yīng)用,靈活的緩存可以說不僅大大減輕了服務(wù)器的壓力,而且因?yàn)楦焖俚挠脩趔w驗(yàn)而方便了用戶。下面來一起看看吧。
    2016-11-11
  • flutter 中監(jiān)聽滑動(dòng)事件

    flutter 中監(jiān)聽滑動(dòng)事件

    這篇文章主要介紹了flutter 中監(jiān)聽滑動(dòng)事件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Android 文件選擇器詳解及實(shí)例代碼

    Android 文件選擇器詳解及實(shí)例代碼

    這篇文章主要介紹了Android 文件選擇器詳解的相關(guān)資料,并附實(shí)例代碼,需要的朋友可以參考下
    2016-10-10
  • Android上傳多張圖片的實(shí)例代碼(RxJava異步分發(fā))

    Android上傳多張圖片的實(shí)例代碼(RxJava異步分發(fā))

    本篇文章主要介紹了Android上傳多張圖片的實(shí)例代碼(RxJava異步分發(fā)),具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-08-08
  • Android性能調(diào)優(yōu)利器StrictMode應(yīng)用分析

    Android性能調(diào)優(yōu)利器StrictMode應(yīng)用分析

    StrictMode意思為嚴(yán)格模式,是用來檢測程序中違例情況的開發(fā)者工具。最常用的場景就是檢測主線程中本地磁盤和網(wǎng)絡(luò)讀寫等耗時(shí)的操作。這篇文章給大家介紹Android性能調(diào)優(yōu)利器StrictMode應(yīng)用分析,感興趣的朋友一起看看吧
    2018-01-01
  • Kotlin編寫Android適配器Adapter

    Kotlin編寫Android適配器Adapter

    這篇文章主要為大家詳細(xì)介紹了Kotlin編寫Android適配器Adapter的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • android 使用kotlin 實(shí)現(xiàn)點(diǎn)擊更換全局語言(中日英切換)

    android 使用kotlin 實(shí)現(xiàn)點(diǎn)擊更換全局語言(中日英切換)

    這篇文章主要介紹了android kotlin 點(diǎn)擊更換全局語言的實(shí)現(xiàn)方法,這里主要介紹中日英切換,需要的朋友可以參考下
    2019-11-11
  • CDC與BG-CDC的含義電容觸控學(xué)習(xí)整理

    CDC與BG-CDC的含義電容觸控學(xué)習(xí)整理

    今天小編就為大家分享一篇關(guān)于CDC與BG-CDC的含義電容觸控學(xué)習(xí)整理,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • SimpleCommand實(shí)現(xiàn)上傳文件或視頻功能(四)

    SimpleCommand實(shí)現(xiàn)上傳文件或視頻功能(四)

    這篇文章主要介紹了SimpleCommand實(shí)現(xiàn)上傳文件或視頻功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Android自定義View實(shí)現(xiàn)雪花特效

    Android自定義View實(shí)現(xiàn)雪花特效

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)雪花特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-02-02

最新評論