一、在你的项目根目录下创建"libs"目录,将下载的BmobSDK文件放入该目录下。
二、在你的应用程序的AndroidManifest.xml
文件中添加相应的权限:
android:name="android.permission.INTERNET"/>
android:name="android.permission.ACCESS_WIFI_STATE"/>
android:name="android.permission.ACCESS_NETWORK_STATE"/>
android:name="android.permission.READ_PHONE_STATE"/>
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
android:name="android.permission.READ_LOGS"/>
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.bmob.example"
android:versionCode="1"
android:versionName="1.0">
android:minSdkVersion="8" android:targetSdkVersion="17"/>
android:name="android.permission.INTERNET"/>
android:name="android.permission.ACCESS_NETWORK_STATE"/>
android:name="android.permission.ACCESS_WIFI_STATE"/>
android:name="android.permission.READ_PHONE_STATE"/>
android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
android:name="android.permission.READ_LOGS"/>
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
android:name="cn.bmob.example.MainActivity"
android:screenOrientation="portrait"
android:label="@string/app_name">
android:name="android.intent.action.MAIN"/>
android:name="android.intent.category.LAUNCHER"/>
android:name=".CreateActivity"
android:screenOrientation="portrait">
android:name=".DeleteActivity"
android:screenOrientation="portrait">
android:name=".UpdateActivity"
android:screenOrientation="portrait">
android:name=".FindActivity"
android:screenOrientation="portrait">
在你应用程序启动的Activity的onCreate()方法中初始化Bmob功能。代码如下所示:
package com.bmob.example;
import cn.bmob.Bmob;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// 初始化 Bmob SDK
// 使用时请将第二个参数Application ID替换成你在Bmob服务器端创建的Application ID
Bmob.initialize(this, "Your Application ID");
}
}
首先创建JavaBean(对应为Bmob后台的数据表,更详细的解释请查看Android开发文档)
public class Person extends BmobObject {
private String name;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
添加数据
Person p2 = new Person();
p2.setName("lucky");
p2.setAddress("北京海淀");
p2.save(this, new SaveListener() {
@Override
public void onSuccess() {
// TODO Auto-generated method stub
toast("添加数据成功,返回objectId为:"+p2.getObjectId());
}
@Override
public void onFailure(int code, String msg) {
// TODO Auto-generated method stub
toast("创建数据失败:" + msg);
}
});
如果toast出添加数据成功的消息,你会在Bmob对应Application Id的数据表中看到有一行新增的数据,如下图所示:
//查找Person表里面id为6b6c11c537的数据
BmobQuery<Person> bmobQuery = new BmobQuery<Person>();
bmobQuery.getObject(this, "6b6c11c537", new GetListener<Person>() {
@Override
public void onSuccess(Person object) {
// TODO Auto-generated method stub
toast("查询成功");
}
@Override
public void onFailure(int code, String msg) {
// TODO Auto-generated method stub
toast("查询失败:" + msg);
}
});
//更新Person表里面id为6b6c11c537的数据,address内容更新为“北京朝阳”
Person p2 = new Person();
p2.setAddress("北京朝阳");
p2.update(this, "6b6c11c537", new UpdateListener() {
@Override
public void onSuccess() {
// TODO Auto-generated method stub
toast("更新成功:" + p2.getUpdatedAt());
}
@Override
public void onFailure(int code, String msg) {
// TODO Auto-generated method stub
toast("更新失败:" + msg);
}
});
Person p2 = new Person();
p2.setObjectId("6b6c11c537");
p2.delete(this, new DeleteListener() {
@Override
public void onSuccess() {
// TODO Auto-generated method stub
toast("删除成功");
}
@Override
public void onFailure(int code, String msg) {
// TODO Auto-generated method stub
toast("删除失败:" + msg);
}
});