ذخیره و بازیابی اطلاعات در برنامه نویسی اندروید با SharedPreferences چگونه انجام می شود؟ برای دانستن جواب این مطلب از مجله برنامه نویسی اندروید الکامکو را تا آخر بخوانید تا آموزش کار با SharedPreferences در اندروید همراه یک مثال کاربردی را یاد بگیرید.
ذخیره و بازیابی اطلاعات در برنامه نویسی اندروید با SharedPreferences :
ذخیره اطلاعات در اندروید با روش های گوناگونی قابل انجام است که هر کدام از این روش ها در جای خود و با توجه به سلیقه توسعه دهنده مورد استفاده قرار می گیرند. روشی که در این مقاله اندروید می خواهیم به شما یاد دهیم آموزش کامل SharedPreferences در اندروید استودیو است که برای ذخیره فایل در اندروید با حجم کم کاربرد دارد.
کاربرد SharedPreferences در اندروید چیست؟
Shared Preferences اندروید روشی است که به وسیله آن می توانید مقادیر کمی از داده های اولیه را درون یک فایل XML در حافظه دستگاه اندرویدی ذخیره و بازیابی کنید و به عنوان یک پایگاه همیشگی برای این دادهای ساده مورد استفاده قرار می گیرد.
از Shared Preferences اندروید می توان برای ذخیره تنظیمات برنامه (مانند فونت، رنگ، تم)، ثبت اطلاعات ورود کاربر(مانند لاگین و لاگ اوت)، نمایش و چیدمان لیست ها، فعال یا غیرفعال بودن دریافت نوتیفیکشن ها، و … استفاده کرد.
مزایای استفاده از SharedPreferences در اندروید چیست ؟
- ذخیره اطلاعات در اندروید با سرعت بسیار بالا و حجم پایین انجام می شود.
- ذخیره اطلاعات در اندروید بصورت کوچک بدون نیاز به دیتابیس جداگانه انجام می شود.
- تابع SharedPreferences در اندروید با تمام نسخه های اندروید سازگار است.
- کار با کدنویسی تابع SharedPreferences بسیار ساده و راحت است.
- SharedPreferences در اندروید به ما اجازه می دهد تا اطلاعات را به صورت Key یا Value ذخیره و نگهداری کنیم.
- ذخیره اطلاعات با SharedPreferences در اندروید با فرمت xml ذخیره می شود و این دیتاها در اپلیکیشن باقی می ماند و پاک نمی شود.
ذخیره اطلاعات با SharedPreferences در اندروید چگونه است؟
برای نحوه صحیح و درست کار با SharedPreferences در اندروید ما این مبحث بسیار ساده و کاربردی را به صورت رایگان و با یک مثال به شما آموزش می دهیم. با امید به اینکه این مقاله مورد استفاده شما عزیزان قرار بگیرد.
مراحل ذخیره اطلاعات در دیتابیس در اندروید استودیو با SharedPreferences
مرحله اول ساخت پروژه کار با SharedPreferences در اندروید استودیو :
به منظور شروع کار برای ذخیره فایل در اندروید استودیو، یک پروژه جدید در اندروید استودیو ایجاد می کنیم (من نام پروژه را SharedPref گذاشتم). توجه کنید که سورس کامل این پروژه در پایین همین مقاله برای دانلود قرار گرفته است. مقادیر درون فولدر values را مانند سورس تغییر دهید (استایل و رنگ ها). بعد از اضافه کردن آیکون ها و تصاویر مربوطه در پوشه drawable لایه 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:id="@+id/layout" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:background="@color/colorPrimary" android:layout_width="match_parent" android:layout_height="wrap_content"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/txtName" android:gravity="center" android:textColor="#ffffff" android:textSize="16sp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout> </android.support.v7.widget.Toolbar> <EditText android:id="@+id/edtName" android:layout_margin="20dp" android:hint="نام خود را وارد کنید ..." android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/btnSave" android:textSize="20sp" android:text="ذخیره" android:textColor="@color/white" android:background="@color/colorAccent" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RelativeLayout android:layout_marginTop="50dp" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:id="@+id/txtView" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:text="تنظیمات" android:textColor="#000" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:src="@drawable/ic_settings" android:layout_centerVertical="true" android:layout_toLeftOf="@+id/txtView" android:layout_marginRight="5dp" android:layout_width="40dp" android:layout_height="40dp"/> </RelativeLayout> <View android:layout_margin="20dp" android:background="@color/colorAccent" android:layout_width="match_parent" android:layout_height="1dp"/> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="60dp"> <Button android:id="@+id/btnPurple" android:textColor="#ffffff" android:textSize="20sp" android:text="تم بنفش" android:background="@color/purplePrimary" android:layout_marginRight="5dp" android:layout_marginLeft="10dp" android:layout_weight="0.5" android:layout_width="0dp" android:layout_height="match_parent" /> <Button android:id="@+id/btnBlue" android:text="تم آبی" android:textColor="@color/white" android:textSize="20sp" android:background="@color/colorAccent" android:layout_marginLeft="5dp" android:layout_marginRight="10dp" android:layout_weight="0.5" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout> <TextView android:textColor="#000" android:textSize="20sp" android:layout_margin="20dp" android:text="تغییر سایز فونت" android:layout_width="match_parent" android:layout_height="wrap_content" /> <android.support.v7.widget.AppCompatSeekBar android:id="@+id/seekbar" android:layout_margin="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
تا اینجا خروجی برنامه به شکل زیر می باشد :
اطلاعات خوبی درباره ذخیره و بازیابی اطلاعات در برنامه نویسی اندروید هست مرسی از شما
ممنون از توجه شما