标签:bmob android后端服
一、Bmob简介
(1)数据服务
支持丰富的数据类型,灵活方便的增删改查,可视化的数据操作,安全的角色和ACL管理,多表关联处理,数据的批量处理,本地数据缓存,开发者们无需关注服务器后端,就可以实现多平台的同步开发。
(2)文件服务
文件服务允许上传任何的文件类型,支持分片断点续传,此外,针对移动端对图片业务的需求,还提供了生成高压缩比的缩略图服务。
(3)推送服务
提供了Android和iOS两个版本的推送服务,采用Websocket保持长连接,稳定性更好,目前,64GB的单机能够支撑600万的用户长连接。
(4)扩展服务
提供APP官网、计划任务、地理位置、云端代码等扩展服务,确保您的个性化业务逻辑能够在云端更好的运转。
关于Bmob的使用请参考官网:http://docs.bmob.cn/android/faststart/index.html?menukey=fast_start&key=start_android
二、项目案例
Feedback.java
package com.lc.bombtest.bean;
import cn.bmob.v3.BmobObject;
/**
* 反馈信息的JavaBean
*
*
* 对应的是数据库中一张表
*
* 要继承BmobObject
*
* @author xuliugen
*
*/
public class Feedback extends BmobObject {
private String name;
private String feedback;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFeedback() {
return feedback;
}
public void setFeedback(String feedback) {
this.feedback = feedback;
}
}
MainActivity.java
package com.lc.bombtest;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cn.bmob.push.BmobPush;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobInstallation;
import cn.bmob.v3.BmobPushManager;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import com.lc.bombtest.bean.Feedback;
public class MainActivity extends Activity {
private static final String Application_ID = "e8296559cf0e088ad146c34ae7f99b72"; // 应用的ID
private EditText mName;
private EditText mFeedback;
private Button query_by_name_btn;
private EditText query_by_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化 Bmob SDK
// 使用时请将第二个参数Application ID替换成你在Bmob服务器端创建的Application ID
Bmob.initialize(this, Application_ID);
// 启动Push SDK的初始化
BmobInstallation.getCurrentInstallation(this).save();
BmobPush.startWork(this, Application_ID);
mName = (EditText) this.findViewById(R.id.name);
mFeedback = (EditText) this.findViewById(R.id.feedback);
query_by_name_btn = (Button) this.findViewById(R.id.query_by_name_btn);
query_by_name = (EditText) this.findViewById(R.id.query_by_name);
}
/**
* submit的点击事件
*
* @param view
*/
public void submit(View view) {
String nameString = mName.getText().toString();
String feedbackString = mFeedback.getText().toString();
if (nameString.equals("") || feedbackString.equals("")) {
return;
}
// JavaBean的使用
Feedback feedback = new Feedback();
feedback.setName(nameString);
feedback.setFeedback(feedbackString);
// 保存数据到服务器端
feedback.save(MainActivity.this, new SaveListener() {
@Override
public void onSuccess() {
Toast.makeText(MainActivity.this, "submit success",
Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int i, String s) {
Toast.makeText(MainActivity.this, "submit failure",
Toast.LENGTH_SHORT).show();
}
});
}
/**
* 查询所有的数据queryAll
*/
public void queryAllData(View view) {
BmobQuery<Feedback> query = new BmobQuery<Feedback>();
query.findObjects(MainActivity.this, new FindListener<Feedback>() {
@Override
public void onSuccess(List<Feedback> feedbacks) {
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setTitle("Query All!");
String string = "";
for (Feedback feedback : feedbacks) {
string += feedback.getName() + ":" + feedback.getFeedback()
+ "\n";
}
builder.setMessage(string);
builder.create().show();
}
@Override
public void onError(int i, String s) {
}
});
}
/**
* 根据姓名查询数据
*
* 单条数据的查询
*
* @param view
*/
public void queryName(View view) {
String str = query_by_name.getText().toString();
if (str.equals("")) {
return;
}
BmobQuery<Feedback> query = new BmobQuery<Feedback>();
query.addWhereEqualTo("name", str);// 增加的条件查询
query.findObjects(MainActivity.this, new FindListener<Feedback>() {
@Override
public void onError(int i, String s) {
}
@Override
public void onSuccess(List<Feedback> feedbacks) {
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setTitle("Query By Name!");
String string = "";
for (Feedback feedback : feedbacks) {
string += feedback.getName() + ":" + feedback.getFeedback()
+ "\n";
}
builder.setMessage(string);
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
}
});
}
/**
* 添加的push事件
*
* @param view
*/
public void pushAll(View view) {
BmobPushManager pushManager = new BmobPushManager(MainActivity.this);
pushManager.pushMessageAll("Test");
}
}
PushReceiver.java
package com.lc.bombtest.broadcast;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import cn.bmob.push.PushConstants;
import com.lc.bombtest.R;
public class PushReceiver extends BroadcastReceiver {
/**
* 获取推送消息
*/
@Override
public void onReceive(Context context, Intent intent) {
String msgStringg = "";
if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) {
String message = intent
.getStringExtra(PushConstants.EXTRA_PUSH_MESSAGE_STRING);// 获取推送消息
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
// 解析数据
JSONTokener jsonTokener = new JSONTokener(message);
try {
JSONObject object = (JSONObject) jsonTokener.nextValue();
msgStringg = object.getString("alert");// 这里的alert是key
} catch (JSONException e) {
e.printStackTrace();
}
// notifications管理器
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
// 创建notification对象
Notification notification = new Notification(
R.drawable.ic_launcher, "TestBmob",
System.currentTimeMillis());
// 设置notification的点击事件
notification.setLatestEventInfo(context, "Bomb Test", msgStringg,
null);
notificationManager.notify(R.drawable.ic_launcher, notification);
}
}
}
activity_main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/feedback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/name"
android:layout_alignRight="@+id/name"
android:layout_below="@+id/name"
android:layout_marginTop="25dp"
android:ems="10"
android:hint="feedback" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="your name" />
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/feedback"
android:layout_below="@+id/feedback"
android:layout_marginTop="21dp"
android:onClick="submit"
android:text="提交" />
<Button
android:id="@+id/queryAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/submit"
android:layout_alignBottom="@+id/submit"
android:layout_marginLeft="18dp"
android:layout_toRightOf="@+id/submit"
android:onClick="queryAllData"
android:text="查询所有" />
<EditText
android:id="@+id/query_by_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/submit"
android:layout_centerVertical="true"
android:ems="10"
android:hint="your name" >
<requestFocus />
</EditText>
<Button
android:id="@+id/query_by_name_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/feedback"
android:layout_centerVertical="true"
android:onClick="queryName"
android:text="查询" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/query_by_name"
android:layout_alignParentBottom="true"
android:layout_marginBottom="65dp"
android:layout_marginLeft="18dp"
android:onClick="pushAll"
android:text="推送消息" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lc.bombtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<!-- 使用Bomb的一些权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_LOGS" />
<!-- BmobPush SDK权限 -->
<permission
android:name="cn.bmob.permission.push"
android:protectionLevel="normal" />
<uses-permission android:name="cn.bmob.permission.push" /> <!-- 添加自定义的权限 -->
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.lc.bombtest.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="cn.bmob.push.lib.service.PushService"
android:exported="true"
android:label="PushService"
android:permission="cn.bmob.permission.push"
android:process="cn.bmob.push" >
<intent-filter>
<action android:name="cn.bmob.push.lib.service.PushService" />
</intent-filter>
</service>
<receiver android:name="cn.bmob.push.PushReceiver" >
<intent-filter android:priority="2147483647" > <!-- 优先级加最高 -->
<!-- 系统启动完成后会调用 -->
<action android:name="android.intent.action.BOOT_COMPLETED" />
<!-- 解锁完成后会调用 -->
<action android:name="android.intent.action.USER_PRESENT" />
<!-- 监听网络连通性 -->
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<!-- 自己定义的Receiver -->
<receiver android:name="com.lc.bombtest.broadcast.PushReceiver" >
<intent-filter>
<action android:name="cn.bmob.push.action.MESSAGE" />
</intent-filter>
</receiver>
</application>
</manifest>
效果:
其中Bomb后台数据:
标签:bmob android后端服
原文地址:http://blog.csdn.net/xlgen157387/article/details/44117121