码迷,mamicode.com
首页 > 移动开发 > 详细

android 之 service

时间:2017-05-31 12:07:16      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:png   http   接收器   eth   ras   ibinder   更新   als   发送   

在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 + "秒");
        }

    }

技术分享

android 之 service

标签:png   http   接收器   eth   ras   ibinder   更新   als   发送   

原文地址:http://www.cnblogs.com/yechanglv/p/6922948.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!