后端云Bomb的使用

来源:互联网 发布:百帝王啤酒知乎 编辑:程序博客网 时间:2024/05/21 19:24
  • 介绍就是:作为一个移动平台开发者,你是否曾因为不会搭建服务端程序而苦恼,是否因为服务器端性能优化、功能实现而四处学习?现在,有了Android移动后端服务(Baas),所有苦恼即将不在,数据存储、消息推送、数据分析等种种功能,Baas已全部帮你搞定,心动不如行动,快快学起来吧!。我一看,这Bmob到底是什么牛逼的技术啊,赶紧去膜拜了一下。
  • 不过还好,如同其他的SDK开发平台一样,使用起来都是很简单的。
  • 这里,主要是对Bmob开发平台提供的服务的一个简单使用。
  • 涉及知识点如下: 
    • 如何去使用一个开放平台提供的SDK。
    • 如何去将数据存储到云端(Bmob云端)。
    • 如何去查询存储到云端的数据。
    • 如何去推送一条消息。(直接在客户端进行的推送|一般的推送应该都是在云端做的)
  • 不过做起来倒是不是很难

    • 对于如何去使用一个开发平台的SDK,以Bmob为例。 
      • 首先当然是去该官网,注册一个帐号,或者直接登录帐号。然后去创建一个应用。这里需要特别注意的是:这里的创建应用不是让你去你的IDE上面new 一个Project .而是,在那个网页上面点击’创建应用的按钮’。 当你创建完成应用了,就会给你一些参数,比如:Application IDREST API KeyAccess Key 等等。当然,不同的开放平台所提供的参数数量名称可能不太一样。但目的都是一个,就是将你的真实的app,绑定到这个云端来。好准确的给你提供服务。
      • 然后就是非常重要的一步,去打开开发文档,选择快速入门。我为什么要强调去看快速入门 呢?因为开发文档里面提供的内容非常多,也非常繁杂。如果我们不去看快速入门,而是直接去看它所提供的详细文档,很难找到我们想要的功能,或者找到了想要的功能,也有点不知道如何下手的感觉。但是快速入门就不同了。完全是傻瓜式的教程,只要做过相关的开发,就能明白上面的步骤。于是,这一步就是看快速入门。按照上面的步骤进行数据的添加与查询操作。
      • ok,到这里前面3条知识点就完成了。然后是最后一条,进行消息推送。不过看开发文档关于消息推送倒是没有说怎么直接在Android端推送消息。不过,按照慕课讲解。我也将这些代码写了一下。然后,我也按照消息推送的开发文档,在web端推送了一下文本消息到手机端,也是ok的。在手机端进行推送,也就是主动调用了BmobPush的一些api而已,也没有别的。 

  • 介绍就这么多了,主要还是参照开发文档来的。以下是代码区:

  • 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/et_name"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="Your Name"        android:inputType="textMultiLine" >    </EditText>    <EditText        android:id="@+id/et_feedback"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/et_name"        android:hint="Your feedBack"        android:inputType="textMultiLine"        android:lines="3" >    </EditText>    <Button        android:id="@+id/bt1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/et_feedback"        android:onClick="submit"        android:text="submit" />    <Button        android:id="@+id/bt2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/bt1"        android:onClick="queryAll"        android:text="queryAll" />    <EditText        android:id="@+id/query_et"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBottom="@+id/bt3"        android:layout_alignParentLeft="true"        android:layout_below="@id/bt2"        android:layout_toLeftOf="@+id/bt3"        android:hint="name" />    <Button        android:id="@+id/bt3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_below="@id/bt2"        android:onClick="queryFeedBack"        android:text="queryFeedBack" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/query_et"        android:layout_centerHorizontal="true"        android:layout_marginTop="43dp"        android:onClick="pushAll"        android:text="pushAll" /></RelativeLayout>
  • 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
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 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
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 这里就一个MainActivity的xml,没有别的。下面是MainActivity代码
package com.duck.moocBmob;import java.util.List;import android.app.Activity;import android.app.AlertDialog;import android.content.Context;import android.os.Bundle;import android.view.View;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;public class MainActivity extends Activity {    private EditText etName;    private EditText etQuery;    private EditText etFeedBack;    private Context context;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        context = this;        setContentView(R.layout.activity_main);        Bmob.initialize(this, "08ddeaf079d64ae5c047a1f3e87e3006");        // 使用推送服务时的初始化操作        BmobInstallation.getCurrentInstallation(this).save();        // 启动推送服务        BmobPush.startWork(this, "08ddeaf079d64ae5c047a1f3e87e3006");        etName = (EditText) findViewById(R.id.et_name);        etFeedBack = (EditText) findViewById(R.id.et_feedback);        etQuery = (EditText) findViewById(R.id.query_et);    }    public void pushAll(View view){        BmobPushManager pushManager = new BmobPushManager(context);        pushManager.pushMessageAll("Test");    }    public void queryFeedBack(View view) {        String name = etQuery.getText().toString().trim();        BmobQuery<FeedBack> bmobQuery = new BmobQuery<FeedBack>();        bmobQuery.addWhereContains("name", name);        bmobQuery.findObjects(context, new FindListener<FeedBack>() {            @Override            public void onSuccess(List<FeedBack> list) {                AlertDialog.Builder builder = new AlertDialog.Builder(context);                builder.setTitle("QueryAll");                StringBuilder sb = new StringBuilder();                for (FeedBack feedBack : list) {                    sb.append(feedBack.getName() + ": "                            + feedBack.getFeedBack() + "\n");                }                builder.setMessage(sb.toString());                builder.create().show();            }            @Override            public void onError(int code, String msg) {                vtoast("ERROR code:" + code + " |msg:" + msg);            }        });    }    public void queryAll(View view) {        BmobQuery<FeedBack> query = new BmobQuery<FeedBack>();        query.findObjects(context, new FindListener<FeedBack>() {            @Override            public void onSuccess(List<FeedBack> list) {                AlertDialog.Builder builder = new AlertDialog.Builder(context);                builder.setTitle("QueryAll");                StringBuilder sb = new StringBuilder();                for (FeedBack feedBack : list) {                    sb.append(feedBack.getName() + ": "                            + feedBack.getFeedBack() + "\n");                }                builder.setMessage(sb.toString());                builder.create().show();            }            @Override            public void onError(int code, String msg) {                vtoast("ERROR code:" + code + " |msg:" + msg);            }        });    }    public void submit(View view) {        String name = etName.getText().toString().trim();        String feedBackStr = etFeedBack.getText().toString().trim();        if (name.equals("") || feedBackStr.equals("")) {            return;        }        FeedBack feedBack = new FeedBack();        feedBack.setName(name);        feedBack.setFeedBack(feedBackStr);        feedBack.save(context, new SaveListener() {            @Override            public void onSuccess() {                vtoast("success");            }            @Override            public void onFailure(int code, String msg) {                System.out.println("onFailure==code:" + code);                System.out.println("onFailure==msg:" + msg);                vtoast("failure");            }        });    }    protected void vtoast(String text) {        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();    }}
  • 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
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 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
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 然后是推送消息接收器的代码,也就是一个广播接收者:
package com.duck.moocBmob;import org.json.JSONException;import org.json.JSONObject;import cn.bmob.push.PushConstants;import android.app.Notification;import android.app.NotificationManager;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class BmobPushReceiver extends BroadcastReceiver {    @Override    @SuppressWarnings("deprecation")    public void onReceive(Context context, Intent intent) {        if (intent != null) {            if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) {                String text = intent                        .getStringExtra(PushConstants.EXTRA_PUSH_MESSAGE_STRING);                Toast.makeText(context, text, Toast.LENGTH_SHORT).show();                try {                    JSONObject jsonObject = new JSONObject(text);                    String value = jsonObject.getString("alert");                    NotificationManager notificationManager = (NotificationManager) context                            .getSystemService(Context.NOTIFICATION_SERVICE);                    Notification notification = new Notification(                            R.drawable.ic_launcher, "Bmob Test",                            System.currentTimeMillis());                    notification.setLatestEventInfo(context, "Bmob Push",                            value, null);                    notificationManager.notify(R.drawable.ic_launcher,                            notification);                } catch (JSONException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }}
  • 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
  • 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
  • 关于通知栏的显示,就是在广播接收这里做的。
  • 因为这里面涉及到一些权限的使用,以及lib库里面一些组建的注册,于是也贴出 清单文件的代码:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.duck.moocBmob"    android:versionCode="1"    android:versionName="1.0" >    <!-- BmobPush SDK权限 -->    <permission        android:name="cn.bmob.permission.push"        android:protectionLevel="normal" >    </permission>    <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" />    <uses-sdk        android:minSdkVersion="12"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.duck.moocBmob.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 android:name="com.duck.moocBmob.BmobPushReceiver" >            <intent-filter>                <action android:name="cn.bmob.push.action.MESSAGE" />            </intent-filter>        </receiver>    </application></manifest>
  • 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
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 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
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 以上,就是全部代码了。
  • 需要注意的是:一定要导入Bmob 提供的lib库。不然上面的全是白搭。
原创粉丝点击