源碼下載下傳:http://download.csdn.net/download/jjhahage/10034519
PS:一般情況下,我們在寫android程式的時候,想要實作登入注冊功能,可以選擇自己用servlet作為服務端來實作過濾沒有注冊過的使用者,但是太麻煩,而且不是随時都可以用的。這裡介紹一個移動後端雲伺服器平台bmob,這不僅可以實作雲資料庫儲存,還可以擷取手機驗證等,随時随地都很輕松,下面寫一個小demo,實作一個登陸注冊功能,認識增删查改。
1:首先到bmob官網,注冊一個賬号,裡面建立一個項目,如圖:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2QvwVe0lmdhJ3ZvwFM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2Lc1TVyoFaodVWvBXbhZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39zM0QjMzcTM1EjMyATM3EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
2:建立一個android項目,(AndroidStudio)
(1:添加依賴):在app下的build.gradle中添加
compile 'cn.bmob.android:bmob-sdk:3.4.6'
compile 'com.squareup.okhttp:okhttp:2.4.0'//CDN檔案服務使用okhttp相關包進行檔案的上傳和下載下傳(必填)
compile 'com.squareup.okio:okio:1.4.0'
sourceSets {
main.jniLibs.srcDirs = ['libs']
}
useLibrary 'org.apache.http.legacy'
位置如圖:
(2)添權重限:
<!--允許聯網-->
<uses-permission android:name="android.permission.INTERNET"/>
<!--擷取GSM(2g)、WCDMA(聯通3g)等網絡狀态的資訊 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!--擷取wifi網絡狀态的資訊-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<!--保持CPU運轉,螢幕和鍵盤燈有可能是關閉的,用于檔案上傳和下載下傳-->
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<!--擷取sd卡寫的權限,用于檔案上傳和下載下傳-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!--允許讀取手機狀态 用于建立BmobInstallation-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
(3):添加maven,到指定的雲庫
maven { url "https://raw.github.com/bmob/bmob-android-sdk/master"}
(4:)初始化:
Bmob.initialize(this,"你的 應用ID");
3:下面就是代碼了
寫一個實體類person,
package cn.day1.model;
import cn.bmob.v3.BmobObject;
/**
* Created by CMusketeer on 17/10/22.
*/
public class Person extends BmobObject {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
寫三個布局,分别是注冊頁面,登入頁面,登入成功跳轉的頁面
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="cn.day1.bmobtest1.MainActivity">
<TextView
android:gravity="center"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登入" />
<EditText
android:id="@+id/id_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="username"/>
<EditText
android:id="@+id/id_userpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="password" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/id_ok"
android:layout_width="0dp"
android:text="登入"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/id_register"
android:text="注冊"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
register_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="cn.day1.bmobtest1.MainActivity">
<TextView
android:gravity="center"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注冊中心" />
<EditText
android:id="@+id/id_register_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="username"/>
<EditText
android:id="@+id/id_register_userpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="password" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/id_register_ok"
android:text="注冊"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
success.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="成功登入"
android:gravity="center"
android:textSize="50dp"/>
</LinearLayout>
注冊Activity,RegisterActivity.java 功能:增
這裡是一個簡單的注冊,裡面沒有加判斷,是以,一個号可以重複注冊,但是隻有唯一ID。
package cn.day1.bmobtest1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import cn.bmob.v3.listener.SaveListener;
import cn.day1.model.Person;
/**
* Created by CMusketeer on 17/10/22.
*/
public class RegisterActivity extends Activity {
private TextView register_user;
private TextView register_password;
private Button register_ok;
private Person p2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register_layout);
addControl();//加載控件
addRegisterShow();//注冊方法
}
private void addRegisterShow() {
register_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String rUser=register_user.getText().toString().trim();
String rPassword=register_password.getText().toString().trim();
//判斷使用者名和密碼是否為空,如果為空則不能進去。
if(rUser.length()>0&&rPassword.length()>0){
p2 = new Person();
p2.setName(rUser);
p2.setPassword(rPassword);
//插入方法
p2.save(RegisterActivity.this, new SaveListener() {
@Override
public void onSuccess() {
// TODO Auto-generated method stub
register_password.setText("");
register_user.setText("");
Toast.makeText(RegisterActivity.this, "添加資料成功,傳回objectId為:" + p2.getObjectId(), Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int code, String msg) {
// TODO Auto-generated method stub
Toast.makeText(RegisterActivity.this, "建立資料失敗:" + msg, Toast.LENGTH_SHORT).show();
}
});
}else{
Toast.makeText(RegisterActivity.this, "使用者名或者密碼不能為空", Toast.LENGTH_SHORT).show();
}
}
});
}
private void addControl() {
register_user = (TextView) findViewById(R.id.id_register_username);
register_password = (TextView) findViewById(R.id.id_register_userpassword);
register_ok = (Button) findViewById(R.id.id_register_ok);
}
}
登入頁面:MainActivity.java 功能:查
package cn.day1.bmobtest1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.day1.model.Person;
public class MainActivity extends AppCompatActivity {
private Person p2;
private TextView lgUser;
private TextView lgPassword;
private Button btn_ok;
private Button btn_rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bmob.initialize(this, "你的 應用id");
setContentView(R.layout.activity_main);
addControl();
addLogin();
}
private void addLogin() {
btn_rg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,RegisterActivity.class);
startActivity(intent);
}
});
btn_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BmobQuery<Person> query=new BmobQuery<Person>();
query.findObjects(MainActivity.this,new FindListener<Person>(){
String lgU=lgUser.getText().toString().trim();
String lgp=lgPassword.getText().toString().trim();
int panduan=1;
@Override
public void onSuccess(List<Person> list) {
for(int i=0;i<list.size();i++){
String name=list.get(i).getName();
String password=list.get(i).getPassword();
Log.e("user","唯一 id:"+list.get(i).getObjectId()+"----"+name+"---"+password);
if(name.equals(lgU) && password.equals(lgp)){
Toast.makeText(MainActivity.this, "登入成功", Toast.LENGTH_SHORT).show();
panduan=2;
//成功後panduan等于2,則跳出該循環,并且把輸入快都清空,跳轉到指定頁面
lgUser.setText("");
lgPassword.setText("");
Intent intent=new Intent(MainActivity.this,Success.class);
startActivity(intent);
break;
}
}
if(panduan==1){
Toast.makeText(MainActivity.this, "登入失敗", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onError(int i, String s) {
}
});
}
});
}
private void addControl() {
lgUser = (TextView) findViewById(R.id.id_username);
lgPassword = (TextView) findViewById(R.id.id_userpassword);
btn_ok = (Button) findViewById(R.id.id_ok);
btn_rg = (Button) findViewById(R.id.id_register);
}
}
登入成功頁面 Success.java
package cn.day1.bmobtest1;
import android.app.Activity;
import android.os.Bundle;
/**
* Created by CMusketeer on 17/10/22.
*/
public class Success extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.success);
}
}
總結:
唯一id的擷取可以通過使用者名來擷取,當使用者輸入使用者名時,隻要資料庫中使用者名和輸入的一緻,則就可以list.get(i).getObjectId()
處理增删查改
增:
person = new Person();
person.setName(user);
person.setAddress(password);
person.save(new SaveListener<String>() {
@Override
public void done(String s, BmobException e) {
if(e == null){
Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT).show();
}
else{
}
}
});
删
Id可以通過查處所有的,進而得到id
id=list.get(i).getObjectId();
person = new Person();
person.delete(id, new UpdateListener() {
@Override
public void done(BmobException e) {
if(e==null){
Log.e("sss","删除成功"); }
}
});
查 :和上面的查不大一樣,這也是一種方法
//查詢所有,
query.findObjects(new FindListener<Person>() {
@Override
public void done(List<Person> list, BmobException e) {
}}
//查詢單個
query.getObject(id,new listener)
改
person.setName(“111”);
person.update(id,new UpdateListener() {
@Override
public void done(BmobException e) {
if(e==null){
// Toast.makeText(MainActivity.this, "更改成功", Toast.LENGTH_SHORT).show();
Log.e("sss","更改成功");
}
}
效果圖: