国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Service的使用

2019-11-09 18:17:37
字體:
來源:轉載
供稿:網友

本來打算周更的。。過年那段時間盡情的放縱去了。

我作為一個菜鳥,我來介紹一點關于Service的用法。

    Service 是 Android 四大基本組件之一,是無界面的應用程序,可以長期在后臺運行,在實際工作中非常重要。比如我們在聽歌的時候突然來一條信息,那么當我點開信息的時候,我的音樂也要運行,這個時候就要服務了。

首先我介紹如何啟動服務。

先創建一個Activity.我這里就寫MainActivyt。創建完成之后在創建一個服務,

創建完成之后,我們先將主布局改為LinearLayout

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.liqipeter.startservice.MainActivity">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/tvout"        />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Hello World!"        android:id="@+id/etData"        />    <Button        android:text="啟動服務"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/btnstartservice" />    <Button        android:text="停止服務"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/btnstopservice" />    <Button        android:text="綁定服務"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/btnbingdservice" />    <Button        android:text="解除綁定"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/btnendservice" />    <Button        android:text="同步數據"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/updata" /></LinearLayout>
然后在Activity中初始化控件,并且添加點擊事件
findViewById(R.id.btnstartservice).setOnClickListener(this);        findViewById(R.id.btnstopservice).setOnClickListener(this);               findViewById(R.id.btnbingdservice).setOnClickListener(this);        findViewById(R.id.btnendservice).setOnClickListener(this);        findViewById(R.id.updata).setOnClickListener(this);
然后在點擊事件中開啟服務

   switch (view.getId()){            case R.id.btnstartservice:                Intent i = new Intent(MainActivity.this,MyService.class);                i.putExtra("data",etData.getText().toString());                startService(i);                break;            case R.id.btnstopservice:                stopService(new Intent(MainActivity.this,MyService.class));                break;            case R.id.btnbingdservice:                bindService(new Intent(this,MyService.class),this, Context.BIND_AUTO_CREATE);                break;            case R.id.btnendservice:                unbindService(this);                break;            case R.id.updata:                if (binder!=null){                    binder.setData(etData.getText().toString());                }                break;        }
然后在Service中寫一個方法
 running = true;        new Thread(){            @Override            public void run() {                super.run();                int i =  0;                while (running){                    i++;                    String str =i+":"+data;                    System.out.PRintln(str);                    if (callback!=null){                        callback.onDataChange(str);                    }                    try {                        sleep(1000);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }.start();
當我們點擊綁定服務的時候,我們會將data值傳到服務中,服務中接收到之后,把他顯示在TextView上。但是不能直接顯示,因為不能在子線程更新UI,所以我們要在寫一個消息機制。
    private Handler handler = new Handler(){        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            tvout.setText(msg.getData().getString("data"));        }    };
這是效果圖:
下面我附上我的GitHub地址源代碼在這里:https://github.com/FoxconnPeter/StartService
(ps:本人現在想找份工作,在深圳。至于薪資我目前在深圳工廠做Android,薪資5000左右。我想學更多東西和提升自己的價值
 郵箱:597058061@QQ.com
)


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 铜川市| 凌源市| 米林县| 文安县| 建瓯市| 富蕴县| 乌苏市| 永仁县| 遂昌县| 宜丰县| 驻马店市| 莎车县| 衡南县| 蓝山县| 齐河县| 将乐县| 东乌珠穆沁旗| 阿勒泰市| 江达县| 宁化县| 中山市| 中阳县| 云阳县| 深水埗区| 榕江县| 平阳县| 广水市| 汝城县| 毕节市| 元谋县| 密山市| 吉林市| 盘锦市| 张家口市| 富民县| 邵东县| 东平县| 香格里拉县| 邳州市| 嘉祥县| 闸北区|