Android AIDL介绍
来源:互联网 发布:4399飞机刷枪软件 编辑:程序博客网 时间:2024/05/23 22:29
- package com.android.aidltest;
- import com.android.aidltest.ITaskCallback;
- interface ITaskBinder {
- boolean isTaskRunning();
- void stopRunningTask();
- void registerCallback(ITaskCallback cb);
- void unregisterCallback(ITaskCallback cb);
- }
- package com.android.aidltest;
- interface ITaskCallback {
- void actionPerformed(int actionId);
- }
- private final ITaskBinder.Stub mBinder = new ITaskBinder.Stub() {
- @Override
- public void unregisterCallback(ITaskCallback cb) throws RemoteException {
- printf("service on unregisterCallback");
- // TODO Auto-generated method stub
- if(cb!=null)
- mCallbacks.unregister(cb);
- }
- @Override
- public void stopRunningTask() throws RemoteException {
- printf("service on stopRunningTask");
- // TODO Auto-generated method stub
- }
- @Override
- public void registerCallback(ITaskCallback cb) throws RemoteException {
- printf("service on registerCallback");
- // TODO Auto-generated method stub
- if(cb!=null)
- mCallbacks.register(cb);
- }
- @Override
- public boolean isTaskRunning() throws RemoteException {
- printf("service on isTaskRunning");
- // TODO Auto-generated method stub
- return false;
- }
- };
- private ITaskCallback mCallback = new ITaskCallback.Stub() {
- @Override
- public void actionPerformed(int actionId) throws RemoteException {
- // TODO Auto-generated method stub
- printf("callback id = " + actionId);
- }
- };
public IBinder onBind(Intent t) {
printf("service on bind");
return mBinder;
- void callback(int val) {
- final int N = mCallbacks.beginBroadcast();
- for (int i = 0; i < N; i++) {
- try {
- mCallbacks.getBroadcastItem(i).actionPerformed(val);
- }catch(RemoteException e) {
- }
- }
- mCallbacks.finishBroadcast();
- }
- package com.android.aidltest;
- interface ITaskCallback {
- void actionPerformed(int actionId);
- }
- package com.android.aidltest;
- import com.android.aidltest.ITaskCallback;
- interface ITaskBinder {
- boolean isTaskRunning();
- void stopRunningTask();
- void registerCallback(ITaskCallback cb);
- void unregisterCallback(ITaskCallback cb);
- }
- package com.android.aidltest;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.os.RemoteCallbackList;
- import android.os.RemoteException;
- import android.util.Log;
- public class MyService extends Service {
- public void onCreate() {
- printf("service create");
- }
- public void onStart(Intent intent, int startId) {
- printf("service start id = " + startId);
- callback(startId);
- }
- @Override
- public IBinder onBind(Intent intent) {
- // TODO Auto-generated method stub
- printf("service on bind");
- return mBinder;
- }
- public void onDestroy() {
- printf("service on destroy");
- super.onDestroy();
- }
- public boolean onUnbind(Intent intent) {
- printf("service on unbind");
- return super.onUnbind(intent);
- }
- public void onRebind(Intent intent) {
- printf("service on rebind");
- super.onRebind(intent);
- }
- private void printf(String str) {
- Log.e("TAG","#######################---"+str+"-------");
- }
- void callback(int val) {
- final int N = mCallbacks.beginBroadcast();
- for (int i = 0; i < N; i++) {
- try {
- mCallbacks.getBroadcastItem(i).actionPerformed(val);
- }catch(RemoteException e) {
- }
- }
- mCallbacks.finishBroadcast();
- }
- private final ITaskBinder.Stub mBinder = new ITaskBinder.Stub() {
- @Override
- public void unregisterCallback(ITaskCallback cb) throws RemoteException {
- printf("service on unregisterCallback");
- // TODO Auto-generated method stub
- if(cb!=null)
- mCallbacks.unregister(cb);
- }
- @Override
- public void stopRunningTask() throws RemoteException {
- printf("service on stopRunningTask");
- // TODO Auto-generated method stub
- }
- @Override
- public void registerCallback(ITaskCallback cb) throws RemoteException {
- printf("service on registerCallback");
- // TODO Auto-generated method stub
- if(cb!=null)
- mCallbacks.register(cb);
- }
- @Override
- public boolean isTaskRunning() throws RemoteException {
- printf("service on isTaskRunning");
- // TODO Auto-generated method stub
- return false;
- }
- };
- final RemoteCallbackList<ITaskCallback> mCallbacks
- = new RemoteCallbackList<ITaskCallback>();
- }
- package com.android.aidltest;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.os.RemoteException;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class AidlTest extends Activity {
- private Button btnOk;
- private Button btnCancel;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- btnOk = (Button)findViewById(R.id.btn_ok);
- btnCancel = (Button)findViewById(R.id.btn_cancel);
- btnOk.setText("Start Service");
- btnCancel.setText("Stop Service");
- btnOk.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- onOkClick();
- }
- });
- btnCancel.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- onCancelClick();
- }
- });
- }
- void onOkClick() {
- printf("clicked ok");
- Bundle args = new Bundle();
- Intent intent = new Intent(this, MyService.class);
- intent.putExtras(args);
- bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
- startService(intent);
- }
- void onCancelClick() {
- printf("clicked cancel");
- Intent intent = new Intent(this, MyService.class);
- unbindService(mConnection);
- }
- private void printf(String str) {
- Log.e("TAG", "##################-------"+str+"-----");
- }
- ITaskBinder mService;
- private ServiceConnection mConnection = new ServiceConnection() {
- public void onServiceConnected(ComponentName className,
- IBinder service) {
- mService = ITaskBinder.Stub.asInterface(service);
- try {
- mService.registerCallback(mCallback);
- }catch (RemoteException e) {
- }
- }
- public void onServiceDisconnected(ComponentName className) {
- mService = null;
- }
- };
- private ITaskCallback mCallback = new ITaskCallback.Stub() {
- @Override
- public void actionPerformed(int actionId) throws RemoteException {
- // TODO Auto-generated method stub
- printf("callback id = " + actionId);
- }
- };
- }
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.android.aidltest"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".AidlTest"
- 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" >
- <intent-filter>
- <action android:name="com.android.aidltest.START_MYSERVICE" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </service>
- </application>
- <uses-sdk android:minSdkVersion="8" />
- </manifest>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <Button
- android:id="@+id/btn_ok"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/btn_ok"
- />
- <Button
- android:id="@+id/btn_cancel"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/btn_cancel"
- />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, AidlTest!</string>
- <string name="app_name">AIDL Test</string>
- <string name="btn_ok">OK</string>
- <string name="btn_cancel">Cancel</string>
- </resources>
0 0
- Android AIDL介绍
- Android--AIDL基础介绍
- Android AIDL用法介绍
- android的AIDL简单介绍
- Android AIDL(Android Interface Definition Language)介绍
- Android AIDL(Android Interface Definition Language)介绍
- Android AIDL(Android Interface Definition Language)介绍
- Android AIDL(Android Interface Definition Language)介绍
- android之Service介绍之二 AIDL
- Android的AIDL文件使用入门介绍
- android之Service介绍之二 AIDL
- android之Service介绍之二 AIDL
- Android AIDL简要介绍及使用
- Android AIDL的总结与介绍
- Android AIDL进程间通信介绍
- Android中AIDL的使用介绍
- AIDL介绍
- AIDL介绍
- Session超时,唯一性
- oracle安装报错 /lib/i386/libawt.so:libXt.so.6:cannot open shared object file
- 一.Android 控件架构
- Spring Boot application.propertis配置文件的相关通用属性
- Android四大控件之BroadcastReceiver
- Android AIDL介绍
- MarkDown的使用方法
- 机器学习入门1
- session小结
- Spring Boot入门教程
- Cardboard虚拟现实开发初步(二)
- Boost 库全系统编译
- php算法之冒泡排序
- 机器学习算法一览(附python和R代码)