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- ...
随机推荐
- leetcode 717. 1-bit and 2-bit Characters -easy
https://leetcode.com/problems/1-bit-and-2-bit-characters/description/ We have two special characters ...
- [LeetCode] Valid Triangle Number 合法的三角形个数
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
- [LeetCode] Find Permutation 找全排列
By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decre ...
- python的变量与赋值
1.变量的命名规则 变量其实通过一个标记调用内存中的值,而变量名就是这个标记的名称,但是万一这个标记已经被提前占用或者解释器认为这个标记是不合法的,那么就会报错.下面总结了一下变量的命名规则: 1.不 ...
- 关于自定义view--实现自定义水波纹效果
开发中的东西太多,怕自己忘记了,简单记录一下. 声明:此控件借鉴了大佬的想法,在此感谢大佬提供的支持,我只是把大佬的想法拿出来而已. ok,废话到此结束,看效果: 分析一下,我们可以看到,图中有两个圆 ...
- 机器学习基石:05 Training versus Testing
train:A根据给定训练集D在H中选出g,使得Ein(g)约等于0: test:g在整个输入空间X上的表现要约等于在训练集D上的表现,使得Eout(g)约等于Ein(g). 如果|H|小,更易保证t ...
- [USACO 08JAN]Telephone Lines
Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone compa ...
- UVALive - 3942:Remember the Word
发现字典里面的单词数目多且长度短,可以用字典树保存 f[i]表示s[i~L]的分割方式,则有f[i]=∑f[i+len(word[j])] 其中word[j]为s[i~L]的前缀 注意字典树又叫前 ...
- [模版]平衡树splay2
题目描述 1. 加入:一个新的成员加入同好会,我会分配给他一个没有使用的id,并且询问他的兴趣值val. 2. 修改:id在区间[a,b]内的成员,兴趣值同时改变k,k有可能是负数,表示他们失去了对同 ...
- 【USACO】 洞穴奶牛
题目描述 贝西喜欢去洞穴探险.这次她去的地方由 N 个洞穴组成,编号分别是 1 到 N,1 号洞穴是出发 的起点. 洞穴之间由 M 条隧道相连,双向通行,第 i 条隧道连接 A i 和 B i .每条 ...