Service的使用

本来打算周更的。。过年那段时间尽情的放纵去了。

我作为一个菜鸟,我来介绍一点关于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
)







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值