zoukankan      html  css  js  c++  java
  • android关于service()

    MainActivity:
    package com.example.myservice1;
    import com.example.myservice1.Myservice.MyBinder;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    @SuppressLint("ShowToast")
    public class MainActivity extends Activity implements OnClickListener,ServiceConnection{
        private Button start,stop,bind,unbind,get;
         private Intent intent;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            start=(Button) findViewById(R.id.start);
            stop=(Button) findViewById(R.id.stop);
            bind=(Button) findViewById(R.id.bind);
            unbind=(Button) findViewById(R.id.unbind);
            get=(Button) findViewById(R.id.get);
            start.setOnClickListener(this);
            stop.setOnClickListener(this);
            bind.setOnClickListener(this);
            unbind.setOnClickListener(this);
            get.setOnClickListener(this);
            intent=new Intent(MainActivity.this,Myservice.class);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.start:
                startService(intent);            
                break;
            case R.id.stop:
                stopService(intent);
                break;
            case R.id.bind:
                bindService(intent, this, BIND_AUTO_CREATE);
                break;
            case R.id.unbind:
                unbindService(this);
                break;
            case R.id.get:
                Toast.makeText(MainActivity.this,
                        "当前Service的值为:" + my.getIndex(), Toast.LENGTH_LONG).show();
                break;            
            default:
                break;
            }
            
            
        }
        private Myservice my;
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {        
            System.out.println("onServiceConnected");
            Myservice.MyBinder m=(MyBinder) service;
            my=m.getMyService();        
        }
    
        @Override
        public void onServiceDisconnected(ComponentName name) {                
        }
    
    }



    Myservice:
    package com.example.myservice1;
    import java.util.Timer;
    import java.util.TimerTask;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.Binder;
    import android.os.IBinder;
    
    public class Myservice extends Service {
    
        private Timer timer;
        private TimerTask task;
        private int index = 0;
    
        // 与Activicy进行绑定
        @Override
        public IBinder onBind(Intent intent) {
            return binder;
        }
        //得到binder对象
        private MyBinder binder = new MyBinder();
    
        //自定义已给MyBinder继承Binder实现通信
        public class MyBinder extends Binder{
            //获得当前Service的状态
            public Myservice getMyService(){
                
                return Myservice.this;
            }
        }
        
        
        @Override
        public void onCreate() {
            super.onCreate();
            startTimer();
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return super.onStartCommand(intent, flags, startId);
    
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            stopTimer();
        }
    
        // 开始执行Timer
        public void startTimer() {
            timer = new Timer();
            task = new TimerTask() {
    
                @Override
                public void run() {
                    index++;
                    System.out.println(index);
                }
            };
            timer.schedule(task, 1000, 1000);
        }
    
        // 停止Timer的执行
        public void stopTimer() {
            timer.cancel();
        }
        //实现可读性(Index)
        public int getIndex() {
            return index;
        }
    }
        
  • 相关阅读:
    如何学习 websocket ?
    如何使用 C++ Inja html template 模板
    使用 QSqlTableModel 模型向数据库中插入数据时,为什么使用 rowCount 函数只能返回 256 最大值?
    windows 如何配置 Go 环境(Zip archive 方式)?
    qt 如何使用 lamda 表达式接收线程中发射的数据,并在里面更新 UI ?
    如何使用 VLD 检测程序中的内存泄漏?
    Qt 在相同的线程中可以在信号中传递未注册的元对象,在非相同线程中则不能传递未测试的对象,为什么呢?
    《LeetBook》leetcode题解(5):Longest Palindromic [M]——回文串判断
    《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题
    《Algorithms算法》笔记:元素排序(4)——凸包问题
  • 原文地址:https://www.cnblogs.com/shide/p/3034022.html
Copyright © 2011-2022 走看看