Realm for Android基本教程---一个简便的数据库

Realm是一款开源的数据库工具,它具有快速,易于使用和支持多平台的特点.它完全可以取代我们Android中的SQLite数据库,它比SQLite使用起来更加的方便快捷.

添加到项目

想在Android的项目当中使用Realm,只需在依赖文件app/build.gradle下的dependencies节点添加依赖即可

1
compile 'io.realm:realm-android:0.84.1'

创建Realm对象

一个Realm对象就相当于一个SQLite数据库,一旦创建了一个Realm对象在Android的文件系统中就会存在一个与之对应的文件.

1
Realm myRealm = Realm.getInstance(context);

通过Realm.getInstance()方法可以得到一个Realm对象,同时也会在Android文件系统中创建一个名为default.realm的Realm文件.
我们还可以通过RealmConfiguration.Builder对象来创建自己命名的Realm对象

1
2
3
Realm myOtherRealm = Realm.getInstance(new RealmConfiguration.Builder(context)
.name("myOtherRealm.realm")
.build());

Read More

Android项目实战---指尖翻译系列0x00

本系列是一个Android的开源项目的教程,这个开源的项目叫做指尖翻译,是一个即时的翻译应用.在后面我会将本开源项目的实现思路和实现步骤一一都告诉大家,希望这个系列能对Android的学习者有一定的帮助.

涉及的技术知识点

  1. 采用MVP架构实现
  2. 使用 Retrofit + RxJava 进行网络服务的访问和数据的加载
  3. 使用 ButterKnife 简化 View 的绑定
  4. 采用了有道翻译的API
  5. 实现对系统粘贴板的监听

上面的知识点和技术点,我都会在后面跟大家一一的介绍.
这个开源的项目其实是根据maoruibin作者的咕咚翻译项目的想法进行我自己的重新开发,感谢maoruibin作者提供了这样的一个idea.

Sample演示

演示

应用下载

大家可以在这里下载到apk文件,完整的代码已经开源到我的Github上,欢迎大家Star和Fork
部分机型可能需要手动添加悬浮窗权限

相关教程

Android项目实战—指尖翻译系列0x01
Android项目实战—指尖翻译系列0x02
Android项目实战—指尖翻译系列0x03

Android项目实战---指尖翻译系列0x03

上次我们把悬浮窗的功能实现了,接下来我会带着大家把数据请求和处理的部分完成.

代码实现

首先我们使用的是有道翻译API,我们需要申请一个API key才可使用api,申请的步骤很简单,我就不多说了.下面是API的数据接口:

http://fanyi.youdao.com/openapi.do?keyfrom=&key=&type=data&doctype=&version=1.1&q=要翻译的文本

可以看到我们需要填入的参数有

  • keyfrom 我申请的keyfromfingertrans
  • key 申请的key是1372247841
  • doctype 返回的数据格式,这里我们选择json
  • q 是我们需要翻译的内容

接下来我们来看一下有道官方提供的返回数据的结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"errorCode":0
"query":"good",
"translation":["好"], // 有道翻译
"basic":{ // 有道词典-基本词典
"phonetic":"gʊd"
"uk-phonetic":"gʊd" //英式发音
"us-phonetic":"ɡʊd" //美式发音
"explains":[
"好处",
"好的"
"好"
]
},
"web":[ // 有道词典-网络释义
{
"key":"good",
"value":["良好","善","美好"]
},
{...}
]
}

Read More

Android项目实战---指尖翻译系列0x02

上次我们实现了对粘贴板的内容获取与复制行为的监听,这次我会带着大家完成悬浮窗功能.

创建悬浮窗的布局

首先我们先来点简单的,就是创建我们悬浮窗的布局文件.项目中的悬浮窗比较简单,是一个位于屏幕上方简洁的卡片,其布局文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000">


<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#f2f4f6"
app:cardCornerRadius="2dp"
app:contentPaddingLeft="10dp"
app:contentPaddingRight="10dp"
app:contentPaddingBottom="10dp"
app:contentPaddingTop="5dp">


<RelativeLayout
android:id="@+id/pv_rl_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">


<TextView
android:id="@+id/pv_tv_query"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:ellipsize="end"
android:maxLines="1"
android:text="复制的内容"
android:textColor="@android:color/black"
android:textStyle="bold" />


<TextView
android:id="@+id/pv_tv_translate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/pv_tv_query"
android:ellipsize="end"
android:lineSpacingExtra="2dp"
android:text="翻译的结果"
android:maxLines="4"
android:textColor="#aa000000" />


<TextView
android:text="词典的详细解析"
android:id="@+id/pv_tv_explains"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/pv_tv_translate"
android:ellipsize="end"
android:lineSpacingExtra="2dp"
android:maxLines="4"
android:textColor="#aa000000" />

</RelativeLayout>
</android.support.v7.widget.CardView>

</LinearLayout>

Read More

Android项目实战---指尖翻译系列0x01

从这次开始,我就会带着大家一步一步的实现指尖翻译这个项目.首先要做到即时的翻译就需要获取到粘贴板的内容.因为我们的项目是通过复制内容然后从粘贴板中获取再进行翻译的.所以首先我们要先获取粘贴板的内容.

粘贴板API的使用

通过查看官网手册的粘贴板的API,我们可以通过getSystemService(CLIPBOARD_SERVICE)得到ClipboardManager对象,系统粘贴板中每次只会持有一个ClipData对象,而一个ClipData对象包含一个ClipDescription和一个或多个的ClipData.Item对象.其中的ClipData.Item对象包含的就是我们需要的数据.这些数据包含文本,URI或是Intent data.我们只需关注文本即可.
从粘贴板获取我们所需的文本基本上可以通过下面的步骤实现:

  1. 获取全局的ClipboardManager对象

    1
    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
  2. 判断当前的粘贴板的数据格式是否符合要求

    1
    2
    3
    4
    5
    6
    7
    // 粘贴板是否有数据
    if (!(clipboard.hasPrimaryClip())) {
    } else if (!(clipboard.getPrimaryClipDescription().hasMimeType(MIMETYPE_TEXT_PLAIN))) {
    // 数据格式不符合要求
    } else {
    // 数据格式符合要求
    }
  3. 从粘贴板获取文本数据

    1
    2
    3
    4
       // 假设应用每次只处理一个item
    ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
    // 将数据转换为文本
    String text = item.coerceToText(context).toString();

    Read More