AIDL的简单实现
2015-12-07 14:59
489 查看
package com.example.service_text; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.Process; import android.os.RemoteException; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /** * @author HD * @date 2015-12-7 * @package_name com.example.service_text * @file_name MainActivity.java */ public class MainActivity extends Activity implements OnClickListener { private Button mButton1, mButton2; private MyAIDLService mAIDLService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.i("hd", "process is" + Process.myPid()); mButton1 = (Button) findViewById(R.id.button1); mButton2 = (Button) findViewById(R.id.button2); mButton1.setOnClickListener(this); mButton2.setOnClickListener(this); } @Override public void onClick(View v) { // TODO 自动生成的方法存根 switch (v.getId()) { case R.id.button1: Intent startintent = new Intent(); startintent.setClass(MainActivity.this, MyService.class); bindService(startintent, conn, BIND_AUTO_CREATE); break; case R.id.button2: Intent stopIntent = new Intent(); stopIntent.setClass(MainActivity.this, MyService.class); unbindService(conn); break; } } ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO 自动生成的方法存根 Log.i("hd", "onServiceDisconnected"); } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO 自动生成的方法存根 Log.i("hd", "onServiceConnected"); mAIDLService = MyAIDLService.Stub.asInterface(service); try { int rusult = mAIDLService.plus(5, 4); String upperStr = mAIDLService.toUpperCase("hello world"); Log.i("hd", "result is"+rusult); Log.i("hd", upperStr); } catch (RemoteException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } }; }
package com.example.service_text; import com.example.service_text.MyAIDLService.Stub; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.Process; import android.os.RemoteException; import android.util.Log; /** * @author HD * @date 2015-12-7 * @package_name com.example.service_text * @file_name MyService.java */ public class MyService extends Service { @Override public IBinder onBind(Intent intent) { // TODO 自动生成的方法存根 return mBinder; } @Override public void onCreate() { // TODO 自动生成的方法存根 super.onCreate(); Log.i("hd", "process is" + Process.myPid()); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO 自动生成的方法存根 return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { // TODO 自动生成的方法存根 super.onDestroy(); Log.i("hd", "onDestroy"); } MyAIDLService.Stub mBinder = new Stub() { @Override public String toUpperCase(String str) throws RemoteException { // TODO 自动生成的方法存根 if (str != null) { return str.toUpperCase(); } return null; } @Override public int plus(int a, int b) throws RemoteException { // TODO 自动生成的方法存根 return a + b; } }; }
/Service_text/src/com/example/service_text/MyAIDLService.aidl
package com.example.service_text; interface MyAIDLService{ int plus(int a,int b); String toUpperCase(String str); }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.service_text" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MyService" android:process=":remote" > </service> </application> </manifest>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 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.service_text.MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="104dp" android:layout_marginTop="146dp" android:text="绑定Service" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/button1" android:layout_marginLeft="20dp" android:layout_marginTop="50dp" android:text="解除绑定Service" /> </RelativeLayout>
相关文章推荐
- 说说IO(七)- RAID
- Codeforces Gym 100800J Jelly Raid (暴搜)
- 审计参数 audit_trail
- 关于 error: LINK1123: failure during conversion to COFF: file invalid or corrupt 错误的解决方案
- ECSHOP后台SQL查询提示错误 this sq May contain
- http://blog.csdn.net/desow/article/details/4843556
- Failure to transfer org.apache.maven:maven-archiver:pom:2.5.0 from http://repo1.maven.org/maven2 was
- Mina源码阅读笔记(七)—Mina的拦截器FilterChain
- UVALive 6709 - Mosaic 二维线段树
- Jenkins学习之——(4)Email Extension Plugin插件的配置与使用
- 混合高斯模型&AIC-BIC挑选中心个数
- 服务器高并发下出现大量的time wait的解决办法
- AEAI WM V1.0 工作管理系统开源发版
- AEAI WM V1.0 工作管理系统开源发版
- GSM cell phone calls use outdated encryption that can now be cracked with rainbow tables on a PC
- USB peripherals can turn against their users
- URAL 1181 Cutting a Painted Polygon
- KeyChain保存和获取应用的UDID
- rails_pjax
- rails的身份认证gem