Android如何使用Bmob后端云實(shí)現(xiàn)失物招領(lǐng)功能
最近在使用后端云Bmob對(duì)數(shù)據(jù)進(jìn)行存儲(chǔ),目的是在不搭建服務(wù)器的前提下,能對(duì)Android應(yīng)用的數(shù)據(jù)進(jìn)行操作處理,其實(shí)這篇是比較久之前寫(xiě)的了,有些童鞋反饋說(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)用秘鑰
三、AndroidStudio SDK導(dǎo)入信息配置
詳細(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依賴(lài)文件:
android { compileSdkVersion 30 buildToolsVersion "30.0.3" **兼容Android6.0系統(tǒng)所需,如果這句話(huà)報(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依賴(lài)特定版本的bmob-sdk,具體的依賴(lài)關(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í)砑哟艘蕾?lài)(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),屏幕和鍵盤(pán)燈有可能是關(guān)閉的,用于文件上傳和下載 --> <uses-permission android:name="android.permission.WAKE_LOCK" /> <!--獲取sd卡寫(xiě)的權(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)行賬戶(hù)注冊(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)有信心記錄:
3、注冊(cè)成功后回到登錄界面
輸入賬號(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類(lèi)為Bmob后端云提供類(lèi) 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類(lèi),繼承BmobObject類(lèi)
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 電話(huà)號(hào)碼 * @param descridle 描述 */ private void publishLostInfo(String titleName, String num, String descridle) { LostInfomationReq lostInfomationReq = new LostInfomationReq(); lostInfomationReq.setTitle(titleName);//titleName為用戶(hù)輸入的標(biāo)題 lostInfomationReq.setPhoneNum(num);//num為用戶(hù)輸入的號(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è)面銷(xiāo)毀 finish(); } else { showToast("信息發(fā)布失敗"); } } }); }
4-3、添加數(shù)據(jù)成功后,在后臺(tái)會(huì)有插入的數(shù)據(jù):
5、主界面的信息需要讀取后臺(tái)用戶(hù)插入的數(shù)據(jù):
數(shù)據(jù)的查詢(xún):
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("查詢(xún)數(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() { @Override public 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 電話(huà)號(hào)碼 * @param descridle 描述 */ private void updataInfo(String titleName, String num, String descridle) { LostInfomationReq lostInfomationReq = new LostInfomationReq(); lostInfomationReq.setTitle(titleName);//titleName為用戶(hù)輸入的標(biāo)題 lostInfomationReq.setPhoneNum(num);//num為用戶(hù)輸入的號(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); //銷(xiāo)毀彈出框 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); //銷(xiāo)毀彈出框 popupWindow.dismiss(); } }); }
8、總結(jié)
8-1、實(shí)現(xiàn)登錄、注冊(cè)的過(guò)程:
使用Bmob提供專(zhuān)門(mén)的用戶(hù)類(lèi)——BmobUser來(lái)自動(dòng)處理用戶(hù)賬戶(hù)管理所需的功能。有了這個(gè)類(lèi),你就可以在你的應(yīng)用程序中添加用戶(hù)賬戶(hù)功能。BmobUser是BmobObject的一個(gè)子類(lèi),它繼承了BmobObject所有的方法,具有BmobObject相同的功能。不同的是,BmobUser增加了一些特定的關(guān)于用戶(hù)賬戶(hù)管理相關(guān)的功能。
8-2、失物招領(lǐng):
這個(gè)過(guò)程中,我們對(duì)數(shù)據(jù)進(jìn)行了添加、查詢(xú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)于A(yíng)ndroid 實(shí)現(xiàn)失物招領(lǐng)功能的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android自定義View實(shí)現(xiàn)可拖拽縮放的矩形框
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)可拖拽縮放的矩形框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05內(nèi)存泄露導(dǎo)致Android?中setVisibility()?失效原理
這篇文章主要介紹了內(nèi)存泄露導(dǎo)致Android?中setVisibility()?失效原理,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-07-07Android中關(guān)于Binder常見(jiàn)面試問(wèn)題小結(jié)
這篇文章主要介紹了Android中關(guān)于Binder幾個(gè)面試問(wèn)題,binder是一種進(jìn)程間通訊的機(jī)制,進(jìn)程間通訊需要了解用戶(hù)空間和內(nèi)核空間,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06Android開(kāi)發(fā)中滑動(dòng)分頁(yè)功能實(shí)例詳解
這篇文章主要介紹了Android開(kāi)發(fā)中滑動(dòng)分頁(yè)功能,結(jié)合實(shí)例形式詳細(xì)分析了Android滑動(dòng)分頁(yè)功能的具體步驟與相關(guān)實(shí)現(xiàn)技巧,代碼中備有詳盡的注釋便于理解,需要的朋友可以參考下2017-10-10android教程之把自己的應(yīng)用加入到系統(tǒng)分享中
在A(yíng)ndroid系統(tǒng)中打開(kāi)相冊(cè)中的某張圖片, 點(diǎn)擊右上角的分享按鈕會(huì)彈出分享列表, 把自己的應(yīng)用加入到里面來(lái),下面是設(shè)置方法2014-02-02使用androidx BiometricPrompt實(shí)現(xiàn)指紋驗(yàn)證功能
這篇文章主要介紹了使用androidx BiometricPrompt實(shí)現(xiàn)指紋驗(yàn)證功能,對(duì)android指紋驗(yàn)證相關(guān)知識(shí)感興趣的朋友跟隨小編一起看看吧2021-07-07