Android如何使用Bmob后端云實(shí)現(xiàn)失物招領(lǐng)功能
最近在使用后端云Bmob對(duì)數(shù)據(jù)進(jìn)行存儲(chǔ),目的是在不搭建服務(wù)器的前提下,能對(duì)Android應(yīng)用的數(shù)據(jù)進(jìn)行操作處理,其實(shí)這篇是比較久之前寫的了,有些童鞋反饋說(shuō)現(xiàn)在的源碼會(huì)有問(wèn)題,所以我又重新運(yùn)行了一下,隨著B(niǎo)mob版本的更新,之前的源碼跑不起來(lái),現(xiàn)在重新更新了最新版本做了個(gè)Demo.
實(shí)現(xiàn)步驟:
一、創(chuàng)建賬號(hào)需要的自己去注冊(cè),后端云Bmob首頁(yè)地址:https://www.bmob.cn/
二、網(wǎng)站后臺(tái)創(chuàng)建應(yīng)用(如圖)這里使用的是免費(fèi)版的

創(chuàng)建成功之后點(diǎn)擊創(chuàng)建的應(yīng)用進(jìn)去,在設(shè)置->應(yīng)用秘鑰中可以找到應(yīng)用秘鑰

詳細(xì)參考:http://doc.bmob.cn/data/android/index.html
1、在 Project 的 build.gradle 文件中添加 Bmob的maven倉(cāng)庫(kù)地址:
buildscript { repositories { google()jcenter() } dependencies {classpath ’com.android.tools.build:gradle:3.6.1’ }}allprojects { repositories { google() jcenter() //Bmob的maven倉(cāng)庫(kù)地址--必填 maven { url 'https://raw.github.com/bmob/bmob-android-sdk/master' } }}task clean(type: Delete) { delete rootProject.buildDir}
2、在app的build.gradle文件中添加compile依賴文件:
android { compileSdkVersion 30 buildToolsVersion '30.0.3' **兼容Android6.0系統(tǒng)所需,如果這句話報(bào)錯(cuò),可在dependencies標(biāo)簽下使用compile ’cn.bmob.android:http-legacy:1.0’** useLibrary ’org.apache.http.legacy’ ...}
dependencies { //以下SDK開(kāi)發(fā)者請(qǐng)根據(jù)需要自行選擇 //bmob-sdk:Bmob的android sdk包,包含了Bmob的數(shù)據(jù)存儲(chǔ)、文件等服務(wù),以下是最新的bmob-sdk: //3.5.5:請(qǐng)務(wù)必查看下面注釋[1] implementation ’cn.bmob.android:bmob-sdk:3.5.5’ //bmob-push:Bmob的推送包 implementation ’cn.bmob.android:bmob-push:0.8’ //bmob-im:Bmob的即時(shí)通訊包,注意每個(gè)版本的im依賴特定版本的bmob-sdk,具體的依賴關(guān)系可查看下面注釋[2] implementation ’cn.bmob.android:bmob-im:2.0.5@aar’ implementation ’cn.bmob.android:bmob-sdk:3.4.7-aar’ //bmob-sms :Bmob單獨(dú)為短信服務(wù)提供的包 implementation ’cn.bmob.android:bmob-sms:1.0.1’ //如果你想應(yīng)用能夠兼容Android6.0,請(qǐng)?zhí)砑哟艘蕾?org.apache.http.legacy.jar) implementation ’cn.bmob.android:http-legacy:1.0’}
3、在你的應(yīng)用程序的AndroidManifest.xml文件中添加相應(yīng)的權(quán)限:
<uses-permission android:name='android.permission.INTERNET' /> <!--獲取GSM(2g)、WCDMA(聯(lián)通3g)等網(wǎng)絡(luò)狀態(tài)的信息 --> <uses-permission android:name='android.permission.ACCESS_NETWORK_STATE' /> <!--獲取wifi網(wǎng)絡(luò)狀態(tài)的信息 --> <uses-permission android:name='android.permission.ACCESS_WIFI_STATE' /> <!--保持CPU 運(yùn)轉(zhuǎn),屏幕和鍵盤燈有可能是關(guān)閉的,用于文件上傳和下載 --><uses-permission android:name='android.permission.WAKE_LOCK' /> <!--獲取sd卡寫的權(quán)限,用于文件上傳和下載--><uses-permission android:name='android.permission.WRITE_EXTERNAL_STORAGE' /><!--允許讀取手機(jī)狀態(tài) 用于創(chuàng)建BmobInstallation--> <uses-permission android:name='android.permission.READ_PHONE_STATE' />四、應(yīng)用中使用詳情
這里演示部分的API:
1、啟動(dòng)頁(yè):
初始化BmobSDK:
//第一:默認(rèn)初始化Bmob.initialize(this,'Your Application ID');2、由登錄界面進(jìn)入注冊(cè)頁(yè)面,進(jìn)行賬戶注冊(cè):

輸入賬號(hào)密碼后點(diǎn)擊注冊(cè)按鈕:
private void bmobRegisterAccount() { final String registerName = accountRegisterName.getText().toString().trim();//賬號(hào) final String registerPassword = accountRegisterPassword.getText().toString().trim();//密碼 if (TextUtils.isEmpty(registerName) || TextUtils.isEmpty(registerPassword)) { showToast('注冊(cè)賬號(hào)或密碼為空'); return; } BmobUser bmobUser = new BmobUser(); bmobUser.setUsername(registerName); bmobUser.setPassword(registerPassword); bmobUser.signUp(new SaveListener<BmobUser>() { @Override public void done(BmobUser bmobUser, BmobException e) {if (e == null) { showToast('恭喜,注冊(cè)賬號(hào)成功'); finish();} else { showToast('register fail:' + e.getMessage());} } }); }
注冊(cè)成功后在后端云Bmob后臺(tái)有信心記錄:


輸入賬號(hào)密碼后點(diǎn)擊登錄按鈕:
private void bmobUserAccountLogin() { final String accountName = accountLoginName.getText().toString().trim();//賬號(hào) final String accountPassword = accountLoginPassword.getText().toString().trim();//密碼 if (TextUtils.isEmpty(accountName)) { showToast('賬號(hào)不能為空'); return; } if (TextUtils.isEmpty(accountPassword)) { showToast('密碼不能為空'); return; } //登錄過(guò)程 showProgressBar(); new Handler().postDelayed(new Runnable() { @Override public void run() {//BmobUser類為Bmob后端云提供類BmobUser bmobUser = new BmobUser();bmobUser.setUsername(accountName);bmobUser.setPassword(accountPassword);bmobUser.login(new SaveListener<BmobUser>() { @Override public void done(BmobUser bmobUser, BmobException e) { if (e == null) { //登錄成功后進(jìn)入主界面 Intent intent = new Intent(LoginActivity.this, LostAndFoundActivity.class); startActivity(intent); finish(); } else { showToast('' + e.getMessage()); hiddenProgressBar();//隱藏 } }}); } }, 3000); }4、登錄成功后進(jìn)入主界面,右上方按鈕進(jìn)入失物招領(lǐng)的信息發(fā)布界面

4-1、信息完成后,點(diǎn)擊右上角點(diǎn)擊發(fā)布按鈕,此時(shí)需要添加數(shù)據(jù):先創(chuàng)建bean類,繼承BmobObject類
public class LostInfomationReq extends BmobObject{ private String title; //標(biāo)題 private String phoneNum;//手機(jī)號(hào)碼 private String desc;//描述 public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getPhoneNum() { return phoneNum; } public void setPhoneNum(String phoneNum) { this.phoneNum = phoneNum; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; }}
4-2、發(fā)布信息:
/** * @param titleName 標(biāo)題 * @param num 電話號(hào)碼 * @param descridle 描述 */ private void publishLostInfo(String titleName, String num, String descridle) { LostInfomationReq lostInfomationReq = new LostInfomationReq(); lostInfomationReq.setTitle(titleName);//titleName為用戶輸入的標(biāo)題 lostInfomationReq.setPhoneNum(num);//num為用戶輸入的號(hào)碼 lostInfomationReq.setDesc(descridle);//descridle為信息描述 lostInfomationReq.save(new SaveListener<String>() { @Override public void done(String s, BmobException e) {if (e == null) { showToast('招領(lǐng)信息發(fā)布成功'); //成功后提示主界面刷新數(shù)據(jù) Intent intent = new Intent(); setResult(RESULT_OK, intent); //成功后將頁(yè)面銷毀 finish();} else { showToast('信息發(fā)布失敗');} } }); }
4-3、添加數(shù)據(jù)成功后,在后臺(tái)會(huì)有插入的數(shù)據(jù):


數(shù)據(jù)的查詢:
private void initData() { BmobQuery<LostInfomationReq> lostInfomationReqBmobQuery = new BmobQuery<>(); lostInfomationReqBmobQuery.order('-updatedAt');//排序 lostInfomationReqBmobQuery.findObjects(new FindListener<LostInfomationReq>() { @Override public void done(List<LostInfomationReq> list, BmobException e) {if (e == null) { lostInfomationReqList = list; lostAndFoundAdapter.setData(list); recyclerView.setAdapter(lostAndFoundAdapter);} else { showToast('查詢數(shù)據(jù)失敗');} } }); }6、長(zhǎng)按RecyclerView每條Item進(jìn)行編輯與刪除操作:

6-1、刪除操作即是將后臺(tái)中的將選中的信息刪除
private void deleteItemData(final int position) { if (lostInfomationReqList.size() != 0) { LostInfomationReq lostInfomationReq = new LostInfomationReq(); lostInfomationReq.setObjectId(lostInfomationReqList.get(position).getObjectId()); lostInfomationReq.delete(new UpdateListener() {@Overridepublic void done(BmobException e) { if (e == null) { lostInfomationReqList.remove(position); lostAndFoundAdapter.setData(lostInfomationReqList); lostAndFoundAdapter.notifyDataSetChanged(); } else { showToast('刪除數(shù)據(jù)失敗'); }} }); } }
6-2、編輯操作即對(duì)數(shù)據(jù)進(jìn)行修改
/** * @param titleName 標(biāo)題 * @param num 電話號(hào)碼 * @param descridle 描述 */ private void updataInfo(String titleName, String num, String descridle) { LostInfomationReq lostInfomationReq = new LostInfomationReq(); lostInfomationReq.setTitle(titleName);//titleName為用戶輸入的標(biāo)題 lostInfomationReq.setPhoneNum(num);//num為用戶輸入的號(hào)碼 lostInfomationReq.setDesc(descridle);//descridle為信息描述 lostInfomationReq.update(infomationReq.getObjectId(), new UpdateListener() { @Override public void done(BmobException e) {if (e == null) { showToast('更新信息成功'); //更新數(shù)據(jù)后提示主界面進(jìn)行數(shù)據(jù)刷新 Intent intent = new Intent(); setResult(RESULT_OK, intent); finish();} } }); }7、使用PopupWindow長(zhǎng)按彈出框
private void showWindow(LostAndFoundHolder holder, final int pos) { //加載布局文件 View contentview = LayoutInflater.from(mContext).inflate(R.layout.pop_window_view,null); final PopupWindow popupWindow = new PopupWindow(contentview, LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); //設(shè)置焦點(diǎn) popupWindow.setFocusable(true); //觸摸框外 popupWindow.setOutsideTouchable(true); //點(diǎn)擊空白處的時(shí)候讓PopupWindow消失 popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000)); //設(shè)置偏移量 popupWindow.showAsDropDown(holder.time, 300, -100); //showAsDropDown(View anchor):相對(duì)某個(gè)控件的位置(正左下方),無(wú)偏移 // showAsDropDown(View anchor, int xoff, int yoff):相對(duì)某個(gè)控件的位置,有偏移 //showAtLocation(View parent, int gravity, int x, int y):相對(duì)于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以設(shè)置偏移或無(wú)偏移 //點(diǎn)擊編輯按鈕 contentview.findViewById(R.id.edit_btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {//回調(diào)給主界面,進(jìn)行數(shù)據(jù)操作mItemClickListener.onEditOrDeleteClick(pos, EDIT_CODE);//銷毀彈出框popupWindow.dismiss(); } }); //點(diǎn)擊刪除按鈕 contentview.findViewById(R.id.delete_btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {//回調(diào)給主界面,進(jìn)行數(shù)據(jù)操作mItemClickListener.onEditOrDeleteClick(pos, DELETE_CODE);//銷毀彈出框popupWindow.dismiss(); } }); }8、總結(jié)
8-1、實(shí)現(xiàn)登錄、注冊(cè)的過(guò)程:
使用Bmob提供專門的用戶類——BmobUser來(lái)自動(dòng)處理用戶賬戶管理所需的功能。有了這個(gè)類,你就可以在你的應(yīng)用程序中添加用戶賬戶功能。BmobUser是BmobObject的一個(gè)子類,它繼承了BmobObject所有的方法,具有BmobObject相同的功能。不同的是,BmobUser增加了一些特定的關(guān)于用戶賬戶管理相關(guān)的功能。
8-2、失物招領(lǐng):
這個(gè)過(guò)程中,我們對(duì)數(shù)據(jù)進(jìn)行了添加、查詢、刪除以及更新操作,當(dāng)然后端云Bmob還不止提供了這些API,還有很多API還需要掌握。
BmobDemo源碼:鏈接:https://pan.baidu.com/s/1wTcWWEitQT65MDtr3PSvhQ提取碼:emue
以上就是Android如何使用Bmob后端云實(shí)現(xiàn)失物招領(lǐng)功能的詳細(xì)內(nèi)容,更多關(guān)于Android 實(shí)現(xiàn)失物招領(lǐng)功能的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 編程語(yǔ)言PHP在Web開(kāi)發(fā)領(lǐng)域的優(yōu)勢(shì)在哪?2. 詳解Android studio 動(dòng)態(tài)fragment的用法3. Android如何加載Base64編碼格式圖片4. Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁(yè)效果(實(shí)例代碼)5. 什么是python的自省6. 基于android studio的layout的xml文件的創(chuàng)建方式7. 圖文詳解vue中proto文件的函數(shù)調(diào)用8. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟9. 在IDEA中實(shí)現(xiàn)同時(shí)運(yùn)行2個(gè)相同的java程序10. 解決Android studio xml界面無(wú)法預(yù)覽問(wèn)題

網(wǎng)公網(wǎng)安備