成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術(shù)文章
文章詳情頁

Android如何使用Bmob后端云實(shí)現(xiàn)失物招領(lǐng)功能

瀏覽:2日期:2022-09-20 11:49:14

最近在使用后端云Bmob對數(shù)據(jù)進(jìn)行存儲,目的是在不搭建服務(wù)器的前提下,能對Android應(yīng)用的數(shù)據(jù)進(jìn)行操作處理,其實(shí)這篇是比較久之前寫的了,有些童鞋反饋說現(xiàn)在的源碼會有問題,所以我又重新運(yùn)行了一下,隨著Bmob版本的更新,之前的源碼跑不起來,現(xiàn)在重新更新了最新版本做了個Demo.

實(shí)現(xiàn)步驟:

一、創(chuàng)建賬號

需要的自己去注冊,后端云Bmob首頁地址:https://www.bmob.cn/

二、網(wǎng)站后臺創(chuàng)建應(yīng)用(如圖)

這里使用的是免費(fèi)版的

Android如何使用Bmob后端云實(shí)現(xiàn)失物招領(lǐng)功能

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

Android如何使用Bmob后端云實(shí)現(xiàn)失物招領(lǐng)功能

三、AndroidStudio SDK導(dǎo)入信息配置

詳細(xì)參考:http://doc.bmob.cn/data/android/index.html

1、在 Project 的 build.gradle 文件中添加 Bmob的maven倉庫地址:

buildscript { repositories { google()jcenter() } dependencies {classpath ’com.android.tools.build:gradle:3.6.1’ }}allprojects { repositories { google() jcenter() //Bmob的maven倉庫地址--必填 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)所需,如果這句話報錯,可在dependencies標(biāo)簽下使用compile ’cn.bmob.android:http-legacy:1.0’** useLibrary ’org.apache.http.legacy’ ...}

dependencies { //以下SDK開發(fā)者請根據(jù)需要自行選擇 //bmob-sdk:Bmob的android sdk包,包含了Bmob的數(shù)據(jù)存儲、文件等服務(wù),以下是最新的bmob-sdk: //3.5.5:請務(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的即時通訊包,注意每個版本的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,請?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、啟動頁:

Android如何使用Bmob后端云實(shí)現(xiàn)失物招領(lǐng)功能

初始化BmobSDK:

//第一:默認(rèn)初始化Bmob.initialize(this,'Your Application ID');2、由登錄界面進(jìn)入注冊頁面,進(jìn)行賬戶注冊:

Android如何使用Bmob后端云實(shí)現(xiàn)失物招領(lǐng)功能

輸入賬號密碼后點(diǎn)擊注冊按鈕:

private void bmobRegisterAccount() { final String registerName = accountRegisterName.getText().toString().trim();//賬號 final String registerPassword = accountRegisterPassword.getText().toString().trim();//密碼 if (TextUtils.isEmpty(registerName) || TextUtils.isEmpty(registerPassword)) { showToast('注冊賬號或密碼為空'); 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('恭喜,注冊賬號成功'); finish();} else { showToast('register fail:' + e.getMessage());} } }); }

注冊成功后在后端云Bmob后臺有信心記錄:

Android如何使用Bmob后端云實(shí)現(xiàn)失物招領(lǐng)功能

3、注冊成功后回到登錄界面

Android如何使用Bmob后端云實(shí)現(xiàn)失物招領(lǐng)功能

輸入賬號密碼后點(diǎn)擊登錄按鈕:

private void bmobUserAccountLogin() { final String accountName = accountLoginName.getText().toString().trim();//賬號 final String accountPassword = accountLoginPassword.getText().toString().trim();//密碼 if (TextUtils.isEmpty(accountName)) { showToast('賬號不能為空'); return; } if (TextUtils.isEmpty(accountPassword)) { showToast('密碼不能為空'); return; } //登錄過程 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ā)布界面

Android如何使用Bmob后端云實(shí)現(xiàn)失物招領(lǐng)功能

4-1、信息完成后,點(diǎn)擊右上角點(diǎn)擊發(fā)布按鈕,此時需要添加數(shù)據(jù):先創(chuàng)建bean類,繼承BmobObject類

public class LostInfomationReq extends BmobObject{ private String title; //標(biāo)題 private String phoneNum;//手機(jī)號碼 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 電話號碼 * @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為用戶輸入的號碼 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); //成功后將頁面銷毀 finish();} else { showToast('信息發(fā)布失敗');} } }); }

4-3、添加數(shù)據(jù)成功后,在后臺會有插入的數(shù)據(jù):

Android如何使用Bmob后端云實(shí)現(xiàn)失物招領(lǐng)功能

5、主界面的信息需要讀取后臺用戶插入的數(shù)據(jù):

Android如何使用Bmob后端云實(shí)現(xiàn)失物招領(lǐng)功能

數(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、長按RecyclerView每條Item進(jìn)行編輯與刪除操作:

Android如何使用Bmob后端云實(shí)現(xiàn)失物招領(lǐng)功能

6-1、刪除操作即是將后臺中的將選中的信息刪除

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、編輯操作即對數(shù)據(jù)進(jìn)行修改

/** * @param titleName 標(biāo)題 * @param num 電話號碼 * @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為用戶輸入的號碼 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長按彈出框

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)擊空白處的時候讓PopupWindow消失 popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000)); //設(shè)置偏移量 popupWindow.showAsDropDown(holder.time, 300, -100); //showAsDropDown(View anchor):相對某個控件的位置(正左下方),無偏移 // showAsDropDown(View anchor, int xoff, int yoff):相對某個控件的位置,有偏移 //showAtLocation(View parent, int gravity, int x, int y):相對于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以設(shè)置偏移或無偏移 //點(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)登錄、注冊的過程:

使用Bmob提供專門的用戶類——BmobUser來自動處理用戶賬戶管理所需的功能。有了這個類,你就可以在你的應(yīng)用程序中添加用戶賬戶功能。BmobUser是BmobObject的一個子類,它繼承了BmobObject所有的方法,具有BmobObject相同的功能。不同的是,BmobUser增加了一些特定的關(guān)于用戶賬戶管理相關(guān)的功能。

8-2、失物招領(lǐng):

這個過程中,我們對數(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)功能的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Android
相關(guān)文章: