• android 之 service


    在Activity中设置两个按钮,分别为启动和关闭Service:

    bt01.setOnClickListener(new Button.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent i = new Intent(mainActivity.this, MyService.class);
                    startService(i);
                    Toast.makeText(mainActivity.this, "Service启动成功",
                            Toast.LENGTH_LONG).show();
                }
            });
            bt02.setOnClickListener(new Button.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent i = new Intent(mainActivity.this, MyService.class);
                    stopService(i);
                    Toast.makeText(mainActivity.this, "Service启动成功",
                            Toast.LENGTH_LONG).show();
                }
            });

    添加MyService.java

    public class MyService extends Service {
        MyThread myThread;

        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void onStart(Intent intent, int startId) {//启动Service
            // TODO Auto-generated method stub
            myThread = new MyThread();
            myThread.start();
            super.onStart(intent, startId);
        }

        @Override
        public void onDestroy() {//关闭Service
            // TODO Auto-generated method stub
            myThread.flag = false;
            super.onDestroy();
        }

        class MyThread extends Thread {
            boolean flag = true;
            int c = 0;

            public void run() {
                while (flag) {
                   Intent i = new Intent("com.sunny.myThread");
                    i.putExtra("myThread", c);
                    sendBroadcast(i);//发送广播,在mainActivity中进行捕获,更新数据
                    c++;
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }

    }

    mainActivity中,添加广播接收器,过滤接收来自action为com.sunny.myThread的广播:

            IntentFilter intentFilter = new IntentFilter("com.sunny.myThread");
            MyBroadcastReceiver myBroadcastReceiver = new MyBroadcastReceiver();
            registerReceiver(myBroadcastReceiverintentFilter);//注册广播接收器

    其中:

    class MyBroadcastReceiver extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {
                // TODO Auto-generated method stub
                Bundle myBundle = intent.getExtras();
                int myInt = myBundle.getInt("myThread");
                     tv01.setText("后台Service运行了" + myInt + "秒");
            }

        }

    image

  • 相关阅读:
    000-ESP32学习开发(SDK)-ESP32开发板使用说明
    2-STM32 替换说明-CKS32, HK32, MM32, APM32, CH32, GD32, BLM32, AT32(推荐), N32, HC华大系列
    002-CH579M学习开发-官方资料学习说明,开发板蓝牙(蓝牙定位),网口通信测试
    001-CH579M学习开发-硬件使用说明,下载和运行第一个程序
    STM32+CH395Q(以太网)基本控制篇(自建物联网平台)-硬件使用说明
    17-网络芯片CH395Q学习开发-片内EEPROM读写实验
    16-网络芯片CH395Q学习开发-低功耗实验
    15-网络芯片CH395Q学习开发-DNS 域名解析
    14-1-网络芯片CH395Q学习开发-WEB服务器-网页到底是啥, web服务器是啥, 网页如何显示的显示图片和视频
    13-网络芯片CH395Q学习开发-模块使用Socket0作为MAC RAW
  • 原文地址:https://www.cnblogs.com/yechanglv/p/6922948.html
Copyright © 2020-2023  润新知