24 AIDL案例
服务端
MainActivity.java
package com.qf.day24_aidl_wordserver;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends Activity {
private EditText etEn,etCh;
private ListView lv;
private MyOpenDbHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化View
initView();
helper = new MyOpenDbHelper(getApplicationContext());
getData();
}
public void initView() {
etEn = (EditText) findViewById(R.id.et_en);
etCh = (EditText) findViewById(R.id.et_ch);
lv = (ListView) findViewById(R.id.lv);
}
//点击进行保存
public void saveClick(View v){
String etEnStr = etEn.getText().toString().trim();
String etChStr = etCh.getText().toString().trim();
String sql = "insert into tb_words(word,detail) values(?,?)";
//添加数据到数据库
helper.execSqlData(sql, new String[]{etEnStr,etChStr});
getData();
}
//查询数据展示到LIstVIew
public void getData(){
String sql = "select * from tb_words";
Cursor cursor = helper.queryData(sql, null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter
(MainActivity.this,
R.layout.item,
cursor,
new String[]{"word","detail"},
new int[]{R.id.tv_en,R.id.tv_ch},
SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
lv.setAdapter(adapter);
}
}
MyOpenDbHelper.java
package com.qf.day24_aidl_wordserver;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MyOpenDbHelper extends SQLiteOpenHelper{
private static final String NAME ="db_words.db";
private static final int VERSION =1;
private SQLiteDatabase db ;
public MyOpenDbHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
public MyOpenDbHelper(Context context) {
super(context, NAME, null, VERSION);
// TODO Auto-generated constructor stub
db = getReadableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String sql ="create table if not exists tb_words(_id integer primary key autoincrement,word,detail)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
//查询
public Cursor queryData(String sql, String[] selectionArgs){
return db.rawQuery(sql, selectionArgs);
}
// 增 删改
public boolean execSqlData(String sql, Object[] bindArgs){
try {
if(bindArgs!=null){
db.execSQL(sql, bindArgs);
}else{
db.execSQL(sql);
}
return true;
} catch (Exception e) {
// TODO: handle exception
return false;
}
}
//查询所有数据 返回List<>
public List<Map<String,Object>> selectData(String sql, String[] selectionArgs){
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
Cursor cusor = db.rawQuery(sql, selectionArgs);
while(cusor.moveToNext()){
Map<String,Object> map = new HashMap<String, Object>();
for(int i=0;i<cusor.getColumnCount();i++){
map.put(cusor.getColumnName(i), cusor.getString(i));
}
list.add(map);
}
return list;
}
}
MyService.java
package com.qf.day24_aidl_wordserver;
import java.util.List;
import java.util.Map;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import com.qf.day24_aidl_wordserver.MyInterfaceWord.Stub;
public class MyService extends Service{
private MyOpenDbHelper helper;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new MyBinder();
}
class MyBinder extends Stub{
@Override
public String getValues(String word) throws RemoteException {
// TODO Auto-generated method stub
String sql = "select * from tb_words where word = ?";
List<Map<String, Object>> list = helper.selectData(sql, new String[]{word});
return list.toString();
}
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
helper = new MyOpenDbHelper(getApplicationContext());
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
MyInterfaceWord.aidl
package com.qf.day24_aidl_wordserver;
interface MyInterfaceWord {
String getValues(String word);
}
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qf.day24_aidl_wordserver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.qf.day24_aidl_wordserver.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">
<intent-filter >
<action android:name="com.qf.day24_aidl_wordserver.MyService"/>
</intent-filter>
</service>
</application>
</manifest>
布局文件
activity_main.xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<EditText
android:id="@+id/et_en"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入英文..." />
<EditText
android:id="@+id/et_ch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入中文..." />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="saveClick"
android:text="保存"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
android:text="以下展示内容"
/>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_en"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tv"
android:textColor="#f00"
/>
<TextView
android:id="@+id/tv_ch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tv"
android:textColor="#00f"
/>
</LinearLayout>
客户端
MainActivity.java
package com.qf.day24_aidl_wordclient;
import com.qf.day24_aidl_wordserver.MyInterfaceWord;
import com.qf.day24_aidl_wordserver.MyInterfaceWord.Stub;
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.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText etWord;
private TextView tvShow;
private MyInterfaceWord myInterfaceWord;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etWord = (EditText) findViewById(R.id.et_word);
tvShow = (TextView) findViewById(R.id.tv_show);
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
myInterfaceWord = Stub.asInterface(service);
}
};
Intent intent = new Intent("com.qf.day24_aidl_wordserver.MyService");
//6.0必须加上
intent.setPackage("com.qf.day24_aidl_wordserver");
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
public void MyClick(View v){
try {
String str = myInterfaceWord.getValues(etWord.getText().toString().trim());
tvShow.setText(str);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
MyInterfaceWord .java
package com.qf.day24_aidl_wordserver;
interface MyInterfaceWord {
String getValues(String word);
}
AndroidManifest.xml 清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qf.day24_aidl_wordclient"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.qf.day24_aidl_wordclient.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>
</application>
</manifest>
activity_main.xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<EditText
android:id="@+id/et_word"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入查询内容" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询"
android:onClick="MyClick"
/>
<TextView
android:id="@+id/tv_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
24 AIDL案例的更多相关文章
- 2-4 测试案例helloWorld
- Android AIDL[Android Interface Definition Language]跨进程通信
全称与中文名IPC:Inter-Process Communication(进程间通信)Ashmem:Anonymous Shared Memory(匿名共享内存)Binder:Binder(进程间通 ...
- [读书笔记]设计原本[The Design of Design]
第1章 设计之命题 1.设计首先诞生于脑海里,再慢慢逐步成形(实现) 2.好的设计具有概念完整性:统一.经济.清晰.优雅.利落.漂亮... 第2章 工程师怎样进行设计思维——理性模型 1.有序模型的有 ...
- Android 中基于 Binder的进程间通信
摘要:对 Binder 工作机制进行了分析. 首先简述 Android 中 Binder 机制与传统的 Linux 进程间的通信比较,接着对基于 Binder 进程间通信的过程分析 最后结合开发实例 ...
- Swiper.js使用方法
<!DOCTYPE html> <html> <head> ... <link rel="stylesheet" href=" ...
- 二十四、Linux 进程与信号---wait 函数
24.1 wait 函数说明 24.1.1 waitpid---等待子进程中断或结束 waitpid(等待子进程中断或结束) 相关函数 wait,fork #include <sys/types ...
- 《MySQL5.7从入门到精通(视频教学版)》
· 一:书籍PDF获取途径 pdf 文档 在 此QQ群(668345923) 的群文件里面 学习视频资源 二:书籍介绍 本书主要包括MySQL的安装与配置.数据库的创建.数据表的创建.数据类型和运算符 ...
- Python3正则表达式(4)
正则表示式的子模式 使用()表示一个子模式,括号中的内容作为一个整体出现. (red)+ ==> redred, redredred, 等多个red重复的情况 子模式的扩展语法 案例1 tel ...
- 《App,这样设计才好卖》
<App,这样设计才好卖> 基本信息 作者: (日)池田拓司 译者: 陈筱烟 丛书名: 图灵交互设计丛书 出版社:人民邮电出版社 ISBN:9787115359438 上架时间:2014- ...
随机推荐
- java中的数组概念
数组的定义形式: 动态初始化方式: 1.声明并开辟数组 String str[]=new String[3];//3表示数组的长度 2.分布完成 String str[]=null; str=new ...
- vector数组中STL习惯性用法
参考:https://blog.csdn.net/lcamisak/article/details/79358060
- HashMap 你真的了解吗?
HashMap深入解析及详细介绍 一. hashmap简介 HashMap是基于哈希表的Map接口的非同步实现.此实现提供所有可选的映射操作,并允许使用null值和null键.此类不保证映射的顺序,特 ...
- hibernate--CRUD初体验
hibernate的crud操作初体验. 看具体实例 package com.fuwh.model; import javax.persistence.Column; import javax.per ...
- [ZJOI2011]看电影(MOVIE)
题目描述 到了难得的假期,小白班上组织大家去看电影.但由于假期里看电影的人太多,很难做到让全班看上同一场电影,最后大家在一个偏僻的小胡同里找到了一家电影院.但这家电影院分配座位的方式很特殊,具体方式如 ...
- ●BZOJ 4237 稻草人
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4237 题解: CDQ分治,单调栈 把所有点先按x从小到大排序,然后去CDQ分治y坐标. 在分 ...
- 洛谷P3209 [HNOI2010]PLANAR
首先用一波神奇的操作,平面图边数m<=3*n-6,直接把m降到n, 然后对于冲突的边一条环内,一条环外,可以用并查集或者2Sat做, 当然并查集是无向的,2Sat是有向的,显然用并查集比较好 复 ...
- 【USACO08NOV】奶牛混合起来Mixed Up Cows
题目描述 约翰有 N 头奶牛,第 i 头奶牛的编号是 S i ,每头奶牛的编号都不同.这些奶牛最近在闹脾气, 为表达不满的情绪,她们在排队的时候一定要排成混乱的队伍.如果一只队伍里所有位置相邻的奶牛 ...
- bzoj3786星系探索 splay
3786: 星系探索 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1314 Solved: 425[Submit][Status][Discuss ...
- Redis wind7 安装
下载地址:https://github.com/MSOpenTech/redis/releases. Redis 支持 32 位和 64 位.这个需要根据你系统平台的实际情况选择,这里我们下载 Red ...