一看就懂———Android组件之Service全解析

每日一言:这一秒不放弃,下一秒就会有希望。

欢迎一起讨论和学习,QQ:732258496  QQ群:478720016

一.Service作用

      它主要用于在后台处理一些耗时的逻辑,或者去执行某些需要长期运行的任务。必要的时候我们甚至可以在程序退出的情况下,让Service在后台继续保持运行状态。

.基本用法

     代码注意点:1.Service都必须在AndroidManifest.xml中注册;

                           2.多次执行startService()----MyService 中onCreate()只会执行一次,onStartConmmand()可以执行多次,执行stopService()服务销毁;

                           3.可以到手机的应用程序管理界面来检查一下MyService是不是正在运行(经检查是的)

                           4.Service其实是运行在主线程

  public class MyService extends Service {  
      
        public static final String TAG = "MyService";  
      
        @Override  
        public void onCreate() {  
            super.onCreate();  
            Log.d(TAG, "onCreate() executed");  
        }  
      
        @Override  
        public int onStartCommand(Intent intent, int flags, int startId) {  
            Log.d(TAG, "onStartCommand() executed");  
            return super.onStartCommand(intent, flags, startId);  
        }  
          
        @Override  
        public void onDestroy() {  
            super.onDestroy();  
            Log.d(TAG, "onDestroy() executed");  
        }  
      
        @Override  
        public IBinder onBind(Intent intent) {  
            return null;  
        }  
      
    }  

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:orientation="vertical" >  
      
        <Button  
            android:id="@+id/start_service"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Start Service" />  
      
        <Button  
            android:id="@+id/stop_service"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="Stop Service" />  
      
    </LinearLayout>  

    public class MainActivity extends Activity implements OnClickListener {  
      
        private Button startService;  
      
        private Button stopService;  
      
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            startService = (Button) findViewById(R.id.start_service);  
            stopService = (Button) findViewById(R.id.stop_service);  
            startService.setOnClickListener(this);  
            stopService.setOnClickListener(this);  
        }  
      
        @Override  
        public void onClick(View v) {  
            switch (v.getId()) {  
            case R.id.start_service:  
                Intent startIntent = new Intent(this, MyService.class);  
                startService(startIntent);  
                break;  
            case R.id.stop_service:  
                Intent stopIntent = new Intent(this, MyService.class);  
                stopService(stopIntent);  
                break;  
            default:  
                break;  
            }  
        }  
      
    }  
//AndroidManifest.xml配置
<service android:name="com.example.servicetest.MyService" >  

三.Activity和Service之间的通讯(交互)

        代码注意点:1.必要将点击startService()和bindService()之后,stopService()和unbind()两个按钮都点击一下,Service 才会被销毁;

                

//MyService中加入以下内容
 @Override  
    public IBinder onBind(Intent intent) {  
        return mBinder;  
    }
 private MyBinder mBinder = new MyBinder(); 
 class MyBinder extends Binder {  
        public void startDownload() {  
            Log.d("TAG", "startDownload() executed");  
            // 执行具体的下载任务  
        }  
  }  
//布局中加入两个按钮 绑定和解绑操作
 <Button  
        android:id="@+id/bind_service"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:text="Bind Service" />  
      
    <Button   
        android:id="@+id/unbind_service"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:text="Unbind Service"  />  
//MainActivity加入以下代码

    private Button bindService;  
  
    private Button unbindService;  
  
    private MyService.MyBinder myBinder;  
  
    private ServiceConnection connection = new ServiceConnection() {  
  
        @Override  
        public void onServiceDisconnected(ComponentName name) {  
        }  
  
        @Override  
        public void onServiceConnected(ComponentName name, IBinder service) {  
            myBinder = (MyService.MyBinder) service;  
            myBinder.startDownload();  
        }  
    };  
   @Override  
    public void onClick(View v) {  
        switch (v.getId()) {  
        case R.id.bind_service:  
            Intent bindIntent = new Intent(this, MyService.class);  
            bindService(bindIntent, connection, BIND_AUTO_CREATE);  
            break;  
        case R.id.unbind_service:  
            unbindService(connection);  
            break;  
        default:  
            break;  
        }  
    }  

四.Service和Thread的关系

       Service和Thread之间没有任何关系 ,Service的后台概念,耗时操作放在Thread中;


    @Override  
    public int onStartCommand(Intent intent, int flags, int startId) {  
        new Thread(new Runnable() {  
            @Override  
            public void run() {  
                // 开始执行后台任务  
            }  
        }).start();  
        return super.onStartCommand(intent, flags, startId);  
    }  
      
    class MyBinder extends Binder {  
      
        public void startDownload() {  
            new Thread(new Runnable() {  
                @Override  
                public void run() {  
                    // 执行具体的下载任务  
                }  
            }).start();  
        }  
      
    }  

五.创建前台Service

      使用Notification

六.Service跨进程通信 (AIDL)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄毛火烧雪下

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值