با عرض سلام و وقت بخیر خدمت کاربران سایت پی وی لرن . و کاربرانی که دوره کامل آموزش برنامه نویسی اندروید را دنبال می کنند. بعضی شیوه های نمونه که می توانید در هنگام توسعه نرم افزار آندروید از آن ها بهره گیری کنید، وجود دارد. این ها توسط آندروید پیشنهاد می شوند و با توجه به زمان آنها در حال بهبود است. این بهترین شیوه ها شامل ویژگی های طراحی تعامل، عملکرد، امنیت و حریم خصوصی، سازگاری، تست، توزیع و کسب درآمد از راهنمایی ها است. best practices (بهترین شیوه ها) شامل ویژگی های طراحی تعامل، عملکرد، امنیت و حریم خصوصی، سازگاری، تست، توزیع و کسب درآمد از راهنمایی ها است. برای آشنایی با بهترین شیوه ها در اندروید در ادامه ی مباحث با ما همراه باشید.
در ادامه ی این آموزش ما شما را با بهترین شیوه ها در اندروید آشنا می کنیم.
هر فیلد متن برای یک کار متفاوت طراحی شده است. به عنوان مثال:
برخی از textfield ها متنی برای متن هستند و بعضی دیگر برای اعداد هستند.
ساختار:
1 2 3 4 5 6 7 8 9 10 11 | <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:hint="User Name" android:layout_below="@+id/imageView" android:layout_alignLeft="@+id/imageView" android:layout_alignStart="@+id/imageView" android:numeric="integer" /> |
اگر می خواهید فیلد شما برای دریافت رمز عبور باشد، باید یک password hint نشان دهید تا کاربر رمز عبور را به خاطر داشته باشد.
1 2 3 4 5 6 7 8 9 10 11 | <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText2" android:layout_alignLeft="@+id/editText" android:layout_alignStart="@+id/editText" android:hint="Pass Word" android:layout_below="@+id/editText" android:layout_alignRight="@+id/editText" android:layout_alignEnd="@+id/editText" android:password="true" /> |
فعالیت های خاصی در یک برنامه کاربردی وجود دارد که در پس زمینه برنامه اجرا می شود.
این فعالیت ها ممکن است چیزی را دانلود کند ، آهنگی را پخش کند و یا … را انجام دهد.
هر دو برای انجام وظایف در پیش زمینه مورد استفاده قرار می گیرند.
اما سرویس توسط بسیاری از رویدادهای چرخه عمر رابط کاربری را تحت تاثیر قرار نمی گیرد.
این ویژگی کارایی دستگاه را در زمانی که به کابل شارژ یا USB متصل می شود، مشخص می کند.
هنگامی که دستگاه خود شارژ خود را دارد، توصیه می شود که تنظیمات برنامه خود را در هر صورت به روز کنید.
افزایش حداکثر میزان تجدید از جمله اقدامات بهبود کارایی است، هر زمان که دستگاه به کابل وصل می شود. می توان این کار را انجام داد:
1 2 3 4 5 6 7 8 | IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = context.registerReceiver(null, ifilter); // Are we charging / charged? Full or charging. int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); // How are we charging? From AC or USB. int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); |
بسیار مهم است که برنامه شما باید امن باشد، اما داده های کاربر و داده های برنامه نیز باید امن شود. امنیت می تواند توسط عوامل زیر افزایش یابد.
مثال زیر نشان می دهد برخی از بهترین شیوه های که شما باید در هنگام توسعه نرم افزار آندروید را دنبال کنید.
این مثال چگونگی مشخص کردن fieldtext ها و افزایش کارایی دستگاه را با چ کردن وضعیت شارژ موبایل را نشان می دهد:
مراحل انجام مثال فوق:
مراحل | توضیحات |
---|---|
۱ | مراحل ایجاد برنامه در اندروید استدیو را طبق بخش های قبلی دنبال کرده و نام پکیج را com.pvlearn.myapplication قرار بدهید. |
۲ | به محتوای کد src/MainActivity.java کدهای لازم را اضافه کنید. |
۳ | به محتوای فایل res/layout/activity_main.xml طراحی لازم را اضافه کنید. |
۴ | برنامه را اجرا کنید تا شبیه ساز اندروید نتیجه را نمایش دهد. |
محتوای فایل src/MainActivity.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | package com.example.Pvlearn.myapplication; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends ActionBarActivity { EditText ed1,ed2; Button b1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ed1=(EditText)findViewById(R.id.editText); ed2=(EditText)findViewById(R.id.editText2); b1=(Button)findViewById(R.id.button); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = registerReceiver(null, ifilter); int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL; int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED,-1); boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB; boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC; if(usbCharge){ Toast.makeText(getApplicationContext(),"Mobile is charging on USB", Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(),"Mobile is charging on AC", Toast.LENGTH_LONG).show(); } } }); } @Override protected void onDestroy() { super.onDestroy(); } } |
محتوای فایل activity_main.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:text="Bluetooth Example" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview" android:textSize="35dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pvlearn" android:id="@+id/textView" android:layout_below="@+id/textview" android:layout_centerHorizontal="true" android:textColor="#ff7aff24" android:textSize="35dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/abc" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:hint="User Name" android:layout_below="@+id/imageView" android:layout_alignLeft="@+id/imageView" android:layout_alignStart="@+id/imageView" android:numeric="integer" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText2" android:layout_alignLeft="@+id/editText" android:layout_alignStart="@+id/editText" android:hint="Pass Word" android:layout_below="@+id/editText" android:layout_alignRight="@+id/editText" android:layout_alignEnd="@+id/editText" android:password="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Check" android:id="@+id/button" android:layout_below="@+id/editText2" android:layout_centerHorizontal="true" /> </RelativeLayout> |
محتوای فایل Strings.xml:
1 2 3 | <resources> <string name="app_name">My Application</string> </resources> |
محتوای فایل AndroidManifest.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sairamkrishna.myapplication" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.sairamkrishna.myapplication.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
برای اجرای برنامه ی اندرویدی خود یکی از فایل های activity در برنامه ی خود کلیک کنید.
سپس روی نماد (Run) از نوار ابزار برای شروع اجرای برنامه کلیک کنید.
سپس نتیجه اجرای برنامه را در شبیه ساز یا دستگاه اندروید خود مشابه زیر مشاهده خواهید کرد:
سپس نتیجه ی زیر را مشاهده خواهید کرد:
نتیجه ی تایپ در فیلد password نیز به صورت زیر خواهد بود:
سپس دستگاه خود را به کابل AC یا کابل USB وصل کرده و روی دکمه check فشار دهید تا پیام زیر را دریافت کنید:
در این آموزش ما به شما چند راه حل ساده و کارآمد برای بهبود کارایی دستگاه موبایل شما در حین کار با برنامه ارائه دادیم. این ها که با بهترین شیوه ها در اندروید شناخته می شوند، روشهایی برای افزایش امنیت و بهبود مصرف باتری ارائه شده است.