博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android学习笔记(四)
阅读量:7102 次
发布时间:2019-06-28

本文共 4058 字,大约阅读时间需要 13 分钟。

AIDL的理解与使用

跨应用启动服务

前提:已经准备好了一个应用的服务。新建另一个应用程序。

MainActivity.java

package com.example.anotherapplication;import android.content.ComponentName;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Intent serviceIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); serviceIntent = new Intent();// 通过setComponent来实现对另一个应用程序的服务的抓取 // 参数(“包名”,“文件名”) PS:全路径、显示启动 serviceIntent.setComponent(new ComponentName("com.example.myapplication","com.example.myapplication.startService")); findViewById(R.id.start).setOnClickListener(this); findViewById(R.id.stop).setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.start: startService(serviceIntent); break; case R.id.stop: stopService(serviceIntent); break; } }}

xml视图

在模拟器上运行程序,在监控台中可以验证启动了另一个程序的服务。

跨应用绑定服务

基于前面的学习,我们知道绑定一个服务需要使用Binder。但是如何在多个程序之间(不知道程序里面的类)的情况下通信呢。

在之前代码的基础上进行改进:

AnotherApplication.java

package com.example.anotherapplication;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.IBinder;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {
private Intent serviceIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); serviceIntent = new Intent();// 通过setComponent来实现对另一个应用程序的服务的抓取 // 参数(“包名”,“文件名”) PS:全路径、显示启动 serviceIntent.setComponent(new ComponentName("com.example.myapplication","com.example.myapplication.startService")); findViewById(R.id.start).setOnClickListener(this); findViewById(R.id.stop).setOnClickListener(this); findViewById(R.id.bind).setOnClickListener(this); findViewById(R.id.bind).setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.start: startService(serviceIntent); break; case R.id.stop: stopService(serviceIntent); break; case R.id.bind: bindService(serviceIntent,this, Context.BIND_AUTO_CREATE); break; case R.id.unbind: unbindService(this); break; } } @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { System.out.println("bind service"); } @Override public void onServiceDisconnected(ComponentName componentName) { }}

App:

在App(应用程序)中新增AIDL,自动生成需要的代码。
startService:

package com.example.myapplication;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;public class startService extends Service {
public startService() { } @Override public IBinder onBind(Intent intent) { return new IMyAidlInterface.Stub() { @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } }; } @Override public void onCreate() { super.onCreate(); System.out.println("Service started"); } @Override public void onDestroy() { super.onDestroy(); System.out.println("Service stoped"); }}

转载于:https://www.cnblogs.com/comefuture/p/8305937.html

你可能感兴趣的文章
tomcat环境变量
查看>>
linux服务器搭建 NAT和DHCP超详细
查看>>
Java并发编程40道面试题及答案——面试稳了
查看>>
“大数据”领域里的“不明觉厉”
查看>>
调试Python程序代码的几种方法总结
查看>>
Linux引导过程内幕
查看>>
Oracle查询表结构的常用语句
查看>>
CLA简介(开源社区要求签署的CLA是什么?)
查看>>
初探spring core
查看>>
北京平面设计艺术展示
查看>>
牛人用 shell 写的 俄罗斯方块游戏
查看>>
oh-my-zsh
查看>>
浅谈企业信息化建设
查看>>
详解linux运维工程师入门级必备技能
查看>>
window 下 安装 apache(nginx)+mysql+php架构
查看>>
DNS原理概念详解
查看>>
对lucene 的总结
查看>>
使用xmake编译swift代码
查看>>
我的友情链接
查看>>
大数据处理相关的好博文
查看>>