Android自学之路——Service与Activity通信

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android自学之路——Service与Activity通信相关的知识,希望对你有一定的参考价值。

A)MainActivity 类的代码 
1
package com.example.cms.service; 2 3 import android.app.Service; 4 import android.content.ComponentName; 5 import android.content.Intent; 6 import android.content.ServiceConnection; 7 import android.os.IBinder; 8 import android.support.v7.app.AppCompatActivity; 9 import android.os.Bundle; 10 import android.view.View; 11 import android.widget.Button; 12 import android.widget.Toast; 13 14 public class MainActivity extends AppCompatActivity { 15 private Button bind,unBind,getServiceStatus; 16 BindService.MyBinder binder; 17 private ServiceConnection conn=new ServiceConnection() { 18 @Override 19 public void onServiceConnected(ComponentName name, IBinder service) {//service为Service中的onBinder()方法返回的 20 System.out.println("----Service Connected-----"); 21 binder= (BindService.MyBinder) service; 22 } 23 @Override 24 public void onServiceDisconnected(ComponentName name) { 25 System.out.println("----Service Disconnted----"); 26 } 27 }; 28 29 @Override 30 protected void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.activity_main); 33 bind= (Button) findViewById(R.id.bind); 34 unBind= (Button) findViewById(R.id.unBind); 35 getServiceStatus= (Button) findViewById(R.id.getServiceStatus); 36 final Intent intent=new Intent(this,BindService.class); 37 bind.setOnClickListener(new View.OnClickListener() { 38 @Override 39 public void onClick(View v) { 40 //绑定指定Service 41 bindService(intent,conn, Service.BIND_AUTO_CREATE); 42 } 43 }); 44 unBind.setOnClickListener(new View.OnClickListener() { 45 @Override 46 public void onClick(View v) { 47 //接触绑定 48 unbindService(conn); 49 } 50 }); 51 getServiceStatus.setOnClickListener(new View.OnClickListener() { 52 @Override 53 public void onClick(View v) { 54 Toast.makeText(MainActivity.this,"Service 的 count值为:"+binder.getCount(),Toast.LENGTH_SHORT).show(); 55 } 56 }); 57 } 58 59 }

B)Service类代码
package com.example.cms.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

/**
 * Created by CMS on 2016/3/28.
 */
public class BindService extends Service {
    private int count;
    private boolean quit=false;
    private MyBinder binder=new MyBinder();

    class MyBinder extends Binder{
        public int getCount(){
            return count;
        }
    }
    @Override
    public IBinder onBind(Intent intent) {
        System.out.print("Service is Binded");
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        System.out.print("Service is Created");
        new Thread(){
            public void run(){
                while(!quit){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    count++;
                }
            }
        }.start();
    }
    @Override
    public boolean onUnbind(Intent intent) {
        System.out.println("Service is Unbinded");
        return true;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.quit=true;
        System.out.println("Service id Destroyed");
    }
}

 

以上是关于Android自学之路——Service与Activity通信的主要内容,如果未能解决你的问题,请参考以下文章

Android自学之路--ContentProvider

Android中View研究自学之路

Android开发学习之路-回调实现Service向activity传递数据

自学之路 遇到瓶颈与江郎才尽

android studio开发笔记五

**.Python自学之路:网络编程