Bmob mobile backend cloud service platform-Android starts from scratch-(2) android Quick Start, bmob-android

Source: Internet
Author: User

Bmob mobile backend cloud service platform-Android starts from scratch-(2) android Quick Start, bmob-android
Bmob mobile backend cloud service platform-Android starts from scratch-(2) android Quick Start

In the previous blog, we briefly introduced what is the Bmob mobile backend service platform, as well as its functions and advantages. This article will use Bmob to quickly implement a simple example to learn more about its power.

I. Preparations 1. Register a Bmob account

Enter www.bmob.cn in the URL bar or enter Bmob in Baidu for search. Open the Bmob official website and click "register" in the upper right corner. enter your name, email address, and password on the jump page, after confirmation, activate your Bmob account in your mailbox, and you can use Bmob to easily develop applications.


2. Create an application on the website background

Log on to the bmob background, click "create application" in the upper-left corner of the background interface, enter the name of your application in the pop-up box, and then confirm that you have an application waiting for development.


3. Get the app key and download the SDK

Select the application you want to develop and click the corresponding "application key" below the application"


On the jump page, obtain the Application ID, which will be used in the initialization SDK.


After obtaining the Application ID, download the SDK. developers can select the corresponding iOS SDK or Android SDK as needed and click Download.



Ii. Code Implementation

Requirement: This instance allows users to register using their usernames and passwords. After successful registration, use the registered data to log on. After successful registration, a prompt is displayed.

Code implementation steps:

1> introduce the BmobSDK rack package

Introduce the downloaded BmobSDK package containing the following to the Android app.


2> Add the following permissions to the configuration file:


<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"/>
3> initialize BmobSDK


Initialize the Bmob function in the onCreate () method of the Activity started by your application. The Code is as follows:


@Override
     protected void onCreate (Bundle savedInstanceState) {
          // TODO Auto-generated method stub
         super.onCreate (savedInstanceState);
          // Initialize the Bmob SDK
         // When using, please replace the second parameter Application ID with the Application ID you created on the Bmob server
         Bmob.initialize (this, "Your Application ID");
     }
4> Create the UserBean object inherited from BmobObject



package com.example.bmobdemo.bean;

import cn.bmob.v3.BmobObject;

/ **
 *
 *
 * Project Name: BmobDemo
 * Class name: UserBean
 * Class description: User entity class that inherits BmobObject
 * Created on: December 18, 2014 at 10:25:40 pm
 * Modified by: Administrator
 * Modified time: December 18, 2014 at 10:25:40 pm
 * Remarks:
 * @version
 *
 * /
public class UserBean extends BmobObject {
/ **
*
* /
private static final long serialVersionUID = 1L;
private String loginId;
private String userName;
private String password;
public String getLoginId () {
return loginId;
}
public void setLoginId (String loginId) {
this.loginId = loginId;
}
public String getUserName () {
return userName;
}
public void setUserName (String userName) {
this.userName = userName;
}
public String getPassword () {
return password;
}
public void setPassword (String password) {
this.password = password;
}
@Override
public String toString () {
return "UserBean [loginId =" + loginId + ", userName =" + userName
+ ", password =" + password + "]";
}
Ranch
Ranch
Ranch
}


4> case results


Bmob platform server data


5> related layout files and configuration files 1. configuration files, AndroidManifest. xml


<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bmobdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_LOGS" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.bmobdemo.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></span>


2. Main layout file, activity_main.xml


This example requires an EditText username and an EditText password, as well as a registration and login Button.


<span style = "font-size: 18px;"> <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">

    <LinearLayout
        android: layout_width = "match_parent"
        android: layout_height = "wrap_content"
        android: layout_marginBottom = "20dp"
        android: orientation = "horizontal">

        <TextView
            android: layout_width = "wrap_content"
            android: layout_height = "wrap_content"
            android: layout_marginRight = "10dp"
            android: text = "username:"
            android: textColor = "# FF0000" />

        <EditText
            android: id = "@ + id / id_login_name_et"
            android: layout_width = "match_parent"
            android: layout_height = "wrap_content" />
    </ LinearLayout>

    <LinearLayout
        android: layout_width = "match_parent"
        android: layout_height = "wrap_content"
        android: layout_marginBottom = "20dp"
        android: orientation = "horizontal">

        <TextView
            android: layout_width = "wrap_content"
            android: layout_height = "wrap_content"
            android: layout_marginRight = "10dp"
            android: text = "password:"
            android: textColor = "# FF0000" />

        <EditText
            android: id = "@ + id / id_password_et"
            android: layout_width = "match_parent"
            android: layout_height = "wrap_content" />
    </ LinearLayout>

    <Button
        android: id = "@ + id / id_register_btn"
        android: layout_width = "match_parent"
        android: layout_height = "wrap_content"
        android: layout_marginBottom = "10dp"
        android: text = "register"
        android: textColor = "# 0000FF" />

    <Button
        android: id = "@ + id / id_login_btn"
        android: layout_width = "match_parent"
        android: layout_height = "wrap_content"
        android: text = "login"
        android: textColor = "# 0000FF" />

</ LinearLayout> </ span>



5> java Implementation Code 1. Main Interface code MainActivity. java


package com.example.bmobdemo;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

import com.example.bmobdemo.bean.UserBean;
import com.example.bmobdemo.util.ToastUtils;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {

// Application ID obtained when Bmob application was created, and written according to the application created by itself
private static final String BMOB_APPLICATION_ID = "be783fdda4eac8a781a9f80596e98fe0";
private Button registerBtn, loginBtn;
private EditText loginEt, passwordEt;

@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);

/ **
* Initialize BmobSDK
* /
Bmob.initialize (this, BMOB_APPLICATION_ID);
initView ();
}

/ **
*
* Initialize controls
* /
private void initView () {
registerBtn = (Button) this.findViewById (R.id.id_register_btn);
loginBtn = (Button) this.findViewById (R.id.id_login_btn);
loginEt = (EditText) this.findViewById (R.id.id_login_name_et);
passwordEt = (EditText) this.findViewById (R.id.id_password_et);

registerBtn.setOnClickListener (this);
loginBtn.setOnClickListener (this);

}

/ **
* Registration
* /
private void register () {
String loginId = loginEt.getText (). ToString ();
String password = passwordEt.getText (). ToString ();
if (loginId.isEmpty () || password.isEmpty ()) {
ToastUtils.toast (this, "The password or account is not empty!");
return;
}

final UserBean userBean = new UserBean ();
userBean.setLoginId (loginId);
userBean.setPassword (password);
userBean.setUserName ("Bmob");
/ **
* Save data to Bmob server
* /
userBean.save (this, new SaveListener () {

@Override
public void onSuccess () {
ToastUtils.toast (MainActivity.this, userBean.toString ()
+ "Successfully registered");
}

@Override
public void onFailure (int arg0, String arg1) {
ToastUtils.toast (MainActivity.this, arg0 + "," + arg1 + "Registration failed");
}
});

}

/ **
* Login
* /
private void login () {
String loginId = loginEt.getText (). ToString ();
String password = passwordEt.getText (). ToString ();
if (loginId.isEmpty () || password.isEmpty ()) {
ToastUtils.toast (this, "The password or account number is not empty!");
return;
}

BmobQuery <UserBean> userQuery = new BmobQuery <UserBean> ();  // Query conditions
userQuery.addWhereEqualTo ("loginId", loginId);
userQuery.addWhereEqualTo ("password", password);

userQuery.findObjects (this, new FindListener <UserBean> () {

@Override
public void onError (int arg0, String arg1) {
ToastUtils.toast (MainActivity.this, arg0 + "," + arg1 + "Login failed");
}

@Override
public void onSuccess (List <UserBean> userList) {
if (userList! = null && userList.size ()> 0)
ToastUtils.toast (MainActivity.this, "Successfully logged in");
else {
ToastUtils.toast (MainActivity.this, "Login failed");
}
}
});
}

@Override
public void onClick (View v) {
switch (v.getId ()) {
case R.id.id_register_btn:
register ();
break;
case R.id.id_login_btn:
login ();
break;
}
}

}


2. entity UserBean



package com.example.bmobdemo.bean;

import cn.bmob.v3.BmobObject;

/ **
 *
 *
 * Project Name: BmobDemo
 * Class name: UserBean
 * Class description: User entity class that inherits BmobObject
 * Created on: December 18, 2014 at 10:25:40 pm
 * Modified by: Administrator
 * Modified time: December 18, 2014 at 10:25:40 pm
 * Remarks:
 * @version
 *
 * /
public class UserBean extends BmobObject {
/ **
*
* /
private static final long serialVersionUID = 1L;
private String loginId;
private String userName;
private String password;
public String getLoginId () {
return loginId;
}
public void setLoginId (String loginId) {
this.loginId = loginId;
}
public String getUserName () {
return userName;
}
public void setUserName (String userName) {
this.userName = userName;
}
public String getPassword () {
return password;
}
public void setPassword (String password) {
this.password = password;
}
@Override
public String toString () {
return "UserBean [loginId =" + loginId + ", userName =" + userName
+ ", password =" + password + "]";
}
Ranch
Ranch
Ranch
}


3. Tool class ToastUtils. java



package com.example.bmobdemo.util;

import android.content.Context;
import android.widget.Toast;

/ **
  * Toast tooltip
  * createdTime: December 18, 2014 at 10:55:21 PM
  *
  * /
public class ToastUtils {
Ranch
public static void toast (Context context, String msg) {
Toast.makeText (context, msg, Toast.LENGTH_SHORT) .show ();
}
Ranch
public static void toast (Context context, int msgId) {
Toast.makeText (context, msgId, Toast.LENGTH_SHORT) .show ();
}
Ranch
} 



Source code path: http://download.csdn.net/detail/a123demi/8283761



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.