zoukankan      html  css  js  c++  java
  • Android Service 入门

    说明

    Service 工作在主进程上。生命周期图

    Android Service lifecycle

    两种状态

    Started

    比如Activity通过调用startService 方法。一旦被启动(Started),服务就永久在后台运行,即使创建他的Activity被销毁。

    Bound

    当一个Component通过调用bindService方法来绑定该服务。服务提供接口让组件和服务交互。

    回调方法说明

    onStartCommand 其他组件通过调用startService时,被调用,如果实现该方法,需要调用stopSelf或者stopService来结束服务。

    onBind 其他组件通过调用bindService时,被调用。需要实现接口,返回IBinder对象,让Client和服务交互。如果不绑定服务,返回null

    onUnbind 所有已经绑定的组件断开

    onCreate创建服务只有一次。

    onDestroy服务被销毁时调用,这里应该清理相应资源。

    例子1:

     主界面两个按钮

    Activity对应的两个方法

        public void startService(View view)
        {
    
            startService(new Intent(getBaseContext(), MyService.class));
        }
        public void stopService(View view)
        {
            stopService(new Intent(getBaseContext(), MyService.class));
        }

     服务类的内容

    public class MyService extends Service {
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Toast.makeText(this, "Servcie Started", Toast.LENGTH_LONG).show();
            return START_STICKY;
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
        }
    }

    然后在AndroidManifest.xml中添加

     运行效果

    点击启动服务

    点击停止服务

    例子2:使用绑定服务,并且在Activity中调用Service的内部类的方法

    在例子1的基础上

    public class MyService extends Service {
        public static final String SERVICE_LOG = "service_LOG";
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            Log.d(SERVICE_LOG, "MyServic Bound");
            return new Mybind();
        }
    
        @Override
        public boolean onUnbind(Intent intent) {
            Log.d(SERVICE_LOG, "MyService Unbound");
            return super.onUnbind(intent);
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            Log.d(SERVICE_LOG, "Myservice created");
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.d(SERVICE_LOG, "Myservice Started");
            return START_STICKY;
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.d(SERVICE_LOG, "MyService destroyed");
        }
    
        public class Mybind extends Binder
        {
            public  void getString() {
                Log.d(SERVICE_LOG, "============> get a string");
            }
        }
    
    }

    主要变化,新增内部类Mybind,并且在onBind返回Mybind对象。

    Activity中新增

    .

    新增代码

        private MyService.Mybind mybind;
        private ServiceConnection connection=new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                mybind = (MyService.Mybind) iBinder;
                mybind.getString();
            }
    
            @Override
            public void onServiceDisconnected(ComponentName componentName) {
    
            }
        };
        public void BindService(View view) {
            Intent bindIntent = new Intent(LoginActivity.this, MyService.class);
            bindService(bindIntent, connection, BIND_AUTO_CREATE);
        }
    
        public void UnbindService(View view) {
            unbindService(connection);
        }

    全部代码

    public class LoginActivity extends AppCompatActivity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.test_login);
        }
        public void startService(View view)
        {startService(new Intent(getBaseContext(), MyService.class));
        }
        public void stopService(View view)
        {
            stopService(new Intent(getBaseContext(), MyService.class));
        }
        private MyService.Mybind mybind;
        private ServiceConnection connection=new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                mybind = (MyService.Mybind) iBinder;
                mybind.getString();
            }
    
            @Override
            public void onServiceDisconnected(ComponentName componentName) {
    
            }
        };
        public void BindService(View view) {
            Intent bindIntent = new Intent(LoginActivity.this, MyService.class);
            bindService(bindIntent, connection, BIND_AUTO_CREATE);
        }
    
        public void UnbindService(View view) {
            unbindService(connection);
        }
    
    }
    View Code

    运行效果

    A测试

    点击绑定服务,此时创建服务,然后绑定服务,

    Myservice created

    MyService Bound

    ============> get a string

    点击解除绑定(解绑之后,再点击解绑,程序会崩溃)

    MyService Unbound

    MyService destroyed

    如果Activity销毁,服务同样自动解绑,销毁。

    B测试

    点击启动服务

    Myservice created

    Myservice Started

    点击绑定服务

    MyServic Bound

    ============> get a string

    点击解除绑定

    MyService Unbound

    因为是通过startService创建的后台Service,不会销毁。

    如果有client绑定,点击“停止服务”,service 不会停止,一旦所有client解除绑定,因为已经点击过“停止服务”,此时服务停止。

  • 相关阅读:
    Android:Service的非绑定式的创建和生命周期
    [NHibernate]缓存(NHibernate.Caches)
    [NHibernate]Parent/Child
    [NHibernate]关联映射
    [NHibernate]集合类(Collections)映射
    [NHibernate]O/R Mapping基础
    [NHibernate]持久化类(Persistent Classes)
    [js]uploadify结合jqueryUI弹出框上传,js中的冒出的bug,又被ie坑了
    [NHibernate]ISessionFactory配置
    [Nhibernate]体系结构
  • 原文地址:https://www.cnblogs.com/noigel/p/11697230.html
Copyright © 2011-2022 走看看