【Android 基础】Service 使用

Android Service .png

1. Android Service 生命周期

service_lifecycle.png

2. 启动方式

Context.startService();

Context.bindService();
2.1 bindService 使用
Activity 中启动.png
Service.png

3. AIDL

1、 service 端新建AIDL文件
Java 同级目录创建aidl文件夹,在文件夹中创建一个包名和应用包名一致的包

// IMyAidlInterface.aidl
package com.skyworthdigital.service;
interface IMyAidlInterface {  
    int plus(int a, int b);
    String toUpperCase(String str);
}

2、 创建服务端

public class BindService extends Service {
    private static final String TAG = "BindService";
    public BindService() {
    }
    Binder myBinder = new IMyAidlInterface.Stub(){

        @Override
        public int plus(int a, int b) throws RemoteException {
            return a + b;
        }

        @Override
        public String toUpperCase(String str) throws RemoteException {
            if (str != null){
                return str.toUpperCase();
            }
            return null;
        }
    };
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: ");
       
        return myBinder;
    }
    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate: ");
        super.onCreate();
    }
}

3、 Client(客户端) 端调用;
Service 端aidl文件夹一起复制到client工程中(与服务端严格保持一致);

public class MainActivity extends Activity {
    private static final String TAG = "Hello MainActivity";

    private ServiceConnection connect = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            IMyAidlInterface myAidlInterface = IMyAidlInterface.Stub.asInterface(service);
            try {
                int result = myAidlInterface.plus(5,6);
                String upStr = myAidlInterface.toUpperCase("liudongbing");
                Log.d(TAG, "onServiceConnected: " + "\n" + result
                +"\n" + upStr);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button remouteBtn = (Button) findViewById(R.id.remote_btn);
        remouteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent bindIntent = new Intent();
                bindIntent.setAction("android.intent.action.BindService");
                bindIntent.setPackage("com.skyworthdigital.service");
                bindService(bindIntent,connect,BIND_AUTO_CREATE);
            }
        });
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android中的服务和windows中的服务是类似的东西,服务一般没有用户操作界面,它运行于系统中不容易被用户发...
    mm_cuckoo阅读 2,536评论 1 3
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,009评论 19 139
  • Jianwei's blog 首页 分类 关于 归档 标签 巧用Android多进程,微信,微博等主流App都在用...
    justCode_阅读 5,973评论 1 23
  • 1:InputChannel提供函数创建底层的Pipe对象 2: 1)客户端需要新建窗口 2)new ViewRo...
    自由人是工程师阅读 5,403评论 0 18
  • 如何理解斜杠?斜杠就是兼职吗,斜杠就是样样精通吗?如果以兼职或各方面都很牛逼作为成为斜杠的目标,我觉得这有点本末倒...
    陌生如我阅读 419评论 0 1