android学习笔记(四)
来源:互联网 发布:python数据分析统计学 编辑:程序博客网 时间:2024/06/03 17:07
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视图
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.anotherapplication.MainActivity" android:orientation="vertical" ><Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/start" android:text="启动服务" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/stop" android:text="停止服务" /></LinearLayout>
在模拟器上运行程序,在监控台中可以验证启动了另一个程序的服务。
跨应用绑定服务
基于前面的学习,我们知道绑定一个服务需要使用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"); }}
阅读全文
0 0
- android学习笔记(四)
- Android学习笔记(四)
- android学习笔记(四)
- android学习笔记(四)
- Android学习笔记(四)
- Android学习笔记(四)
- android学习笔记(四)
- Android学习笔记(四)
- Android学习笔记(四)
- Android学习笔记(四)
- Android学习笔记(四)
- android学习笔记(四)
- Android学习笔记四
- Android 学习笔记(四)
- Android学习笔记(四)
- Android学习笔记(四) Hello World
- android分层学习笔记(四)
- android入门:zxing学习笔记(四)
- 极线约束
- 第五周 【项目2
- 【李叫兽】如何识别故作高深的空话
- python 2.7.12报ImportError: No module named _socket
- 下拉选项框联动
- android学习笔记(四)
- C/C++ 第三周线性表项目(2) 顺序表的应用
- Android开发视频教程最新录制——持续更新
- 别尼玛把时间浪费在瞎BB上,就是干!
- Ubuntu下如何关闭指定端口的进程
- call_user_func_array
- CNN卷积网络(概念介绍)
- 福利|PMCAFF问答专家群-社区大咖聚集地
- Linux 虚拟机中配置 GNOME + VNC