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- ...
随机推荐
- Apache 配置小技巧
1. 使 Apache 只能通过本地主机访问 1.1. 如果在开发环境中,你希望除了自己以外其他人都无法访问站点,你可以使用以下配置: 首先打开Apache的配置文件httdp.conf,此文件路径为 ...
- Hive优化案例
1.Hadoop计算框架的特点 数据量大不是问题,数据倾斜是个问题. jobs数比较多的作业效率相对比较低,比如即使有几百万的表,如果多次关联多次汇总,产生十几个jobs,耗时很长.原因是map re ...
- 解决将/etc/passwd文件中1000改为0后只能guest进入系统的问题 ||ubuntu下将普通用户权限升级为root用户权限的方法;
其实我现在才知道linux系统对于用户权限管理比较严,在ubuntu下系统不允许root权限的用户进入图像界面系统.由于之前没弄过权限这个东西瞬间掉坑了了. 我是想修改一下root下的nginx.co ...
- 【基础】EM 还是 REM?这是一个问题!
简言 应用象EM 和 REM这种相对长度单位进行页面排版是WEB开发中的最佳实践.在页面排版中较好应用EM 和 REM,根据设备尺寸缩放显示元素的大小.这就使得组件在不同设备上都达到最佳的显示效果成为 ...
- 认识JQuery,JQuery的优势、语法、多库冲突、JS原生对象和JQuery对象之间相互转换和DOM操作,常用的方法
(一)认识JQuery JQuery是一个JavaScript库,它通过封装原生的JavaScript函数得到一套定义好的方法 JQuery的主旨:以更少的代码,实现更多的功能 (二)JQue ...
- [ZJOI2010]数字计数
题目描述 给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次. 输入输出格式 输入格式: 输入文件中仅包含一行两个整数a.b,含义如上所述. 输出格式: 输出文件 ...
- [HNOI2014]江南乐
Description 小A是一个名副其实的狂热的回合制游戏玩家.在获得了许多回合制游戏的世界级奖项之后,小A有一天突然想起了他小时候在江南玩过的一个回合制游戏. 游戏的规则是这样的,首先给定一 ...
- ●POJ 3378 Crazy Thairs
题链: http://poj.org/problem?id=3378 题解: 树状数组维护,高精度. 依次考虑以每个位置结尾可以造成的贡献. 假设当前位置为i,为了达到5个元素的要求,我们需要求出,在 ...
- [Helvetic Coding Contest 2017 online mirror]
来自FallDream的博客,未经允许,请勿转载,谢谢, 第一次在cf上打acm...和同校大佬组队打 总共15题,比较鬼畜,最后勉强过了10题. AB一样的题目,不同数据范围,一起讲吧 你有一个背包 ...
- Mysql 基于GTID的主从复制(实操)
实现环境: Master 主:192.168.0.102 (Mysql 5.6.36) Slave 从 :192.168.0.103 (Mysql 5.6.36) 步骤1.在主DB服务器上建立复制账 ...