zoukankan      html  css  js  c++  java
  • android服务之启动方式

    服务有两种启动方式

    • 通过startService方法来启动
    • 通过bindService来开启服务

    布局文件


    在布局文件中我们定义了四个按键来测试这两种方式来开启服务的不同

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="开始服务"
            android:onClick="start"/>
        <Button
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="停止服务"
            android:onClick="stop"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="绑定服务"
            android:onClick="bind"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="解绑"
            android:onClick="unbind"/>
    </LinearLayout>

    Activity


    在Activity中我们使用了这两种方式来开启服务

    package xidian.dy.com.chujia;
    
    import android.content.ComponentName;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
        Intent service;
        MyConnection mc;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            service = new Intent(this, MyService.class);
            mc = new MyConnection();
        }
    
        public void start(View v){
            Toast.makeText(this,"开启服务",Toast.LENGTH_SHORT).show();
            startService(service);
        }
    
        public void stop(View v){
            Toast.makeText(this,"关闭服务",Toast.LENGTH_SHORT).show();
            stopService(service);
        }
    
        public void bind(View v){
            bindService(service,mc,BIND_AUTO_CREATE);
        }
    
        public void unbind(View v){
            unbindService(mc);
        }
    
        class MyConnection implements ServiceConnection{
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
    
            }
    
            @Override
            public void onServiceDisconnected(ComponentName name) {
    
            }
        }
    }

    注意在绑定服务的时候需要传入回调函数(第二个参数),当绑定成功会调用onServiceConnected方法,当解除绑定会调用onServiceDisconnected方法。

    Service


    在服务代码中我们将各个方法复写了一边,其实代码中各个方法也表示了服务的生命周期。

    package xidian.dy.com.chujia;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.support.annotation.Nullable;
    import android.util.Log;
    
    /**
     * Created by dy on 2016/7/12.
     */
    public class MyService extends Service {
        @Override
        public void onCreate() {
            super.onCreate();
            Log.i(this.getClass().getName(), "****start方法****");
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.i(this.getClass().getName(), "*****startCommand方法*****");
            return super.onStartCommand(intent, flags, startId);
        }
    
        @Override
        public void onDestroy() {
            Log.i(this.getClass().getName(), "***Destroy方法***");
            super.onDestroy();
        }
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            Log.i(this.getClass().getName(), "****Bind方法被调用****");
            return null;
        }
    
        @Override
        public boolean onUnbind(Intent intent) {
            Log.i(this.getClass().getName(),"****unBind方法被调用****");
            return super.onUnbind(intent);
        }
    }

    两者区别


    启动服务

    • 点击开始服务

    • 点击返回按键销毁Activity,我们发现服务仍在运行

    • 点击停止服务

     绑定服务

    • 点击绑定服务

    • 点击返回按键销毁Activity,则会抛出异常并且有两方法被调用

     通过对比我们发现startService开启的服务是独立运行的,而通过bindService开启的服务则依赖于Activity,但Activity被销毁时绑定的服务也被销毁了

  • 相关阅读:
    C# 6.0:在catch和finally中使用await
    C# 6.0:Expression – Bodied Methods
    C# 6.0:Auto-Property initializer
    C# 6.0:String Interpolation
    【转】http://www.cnblogs.com/yuzukwok/p/3884377.html
    ThoughtWorks面试题(标记给自己看)
    C# 强制关闭当前程序进程(完全Kill掉不留痕迹)
    (转)C#编程中的66个好习惯
    (转)解决WINDOWS 程序界面闪烁问题的一些经验
    C#利用反射动态绑定事件
  • 原文地址:https://www.cnblogs.com/xidongyu/p/5669567.html
Copyright © 2011-2022 走看看