Android-best-practices
Android-ベストプラクティス
Androidアプリケーションの開発中に従うことができるいくつかのプラクティスがあります。 これらはアンドロイド自体によって提案され、時間に関して改善し続けます。
これらのベストプラクティスには、対話設計機能、パフォーマンス、セキュリティとプライバシー、互換性、テスト、配布、収益化のヒントが含まれます。 それらは絞り込まれ、以下のようにリストされます。
ベストプラクティス-ユーザー入力
すべてのテキストフィールドは、異なるジョブを対象としています。 たとえば、一部のテキストフィールドはテキスト用で、一部は数字用です。 数値用の場合は、そのテキストフィールドにフォーカスがあるときに数値キーパッドを表示することをお勧めします。 その構文は次のとおりです。
<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"/>
ベストプラクティス-バックグラウンドジョブ
アプリケーションには、アプリケーションのバックグラウンドで実行されている特定のジョブがあります。 彼らの仕事は、インターネットから何かを取り出して、音楽などを演奏することかもしれません。 待ちのタスクはUIスレッドではなく、サービスまたはAsyncTaskによってバックグラウンドで実行することをお勧めします。
AsyncTask Vsサービス。
どちらもバックグラウンドタスクを実行するために使用されますが、サービスはほとんどのユーザーインターフェイスライフサイクルイベントの影響を受けないため、AsyncTaskをシャットダウンするような状況でも実行され続けます。
ベストプラクティス-パフォーマンス
アプリケーションのパフォーマンスは最高水準に達している必要があります。 ただし、フロントエンドではなく、デバイスが電源に接続されているか充電されているバックエンドで異なる動作をする必要があります。 充電は、USBおよびワイヤーケーブルから行うことができます。
デバイスが充電中の場合、デバイスが接続されるたびにリフレッシュレートを最大化するなど、アプリケーションの設定があれば更新することをお勧めします。 これとして行うことができます。
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);
ベストプラクティス-セキュリティとプライバシー
アプリケーションを保護することは非常に重要です。アプリケーションだけでなく、ユーザーデータとアプリケーションデータも保護する必要があります。 以下の要因により、セキュリティを強化できます。
- アプリケーションファイルの保存に外部ではなく内部ストレージを使用する
- 可能な限りコンテンツプロバイダーを使用する
- Webへの接続時にSSlを使用する
- デバイスのさまざまな機能にアクセスするための適切なアクセス許可を使用する
例
以下の例は、Androidアプリケーションを開発する際に従うべきベストプラクティスの一部を示しています。 これは、電話機の充電状態を確認することにより、テキストフィールドの使用方法とパフォーマンスを向上させる方法を指定できる基本的なアプリケーションを作成します。
この例を試すには、実際のデバイスでこれを実行する必要があります。
Steps | Description |
---|---|
1 | You will use Android studio IDE to create an Android application under a package com.example.sairamkrishna.myapplication. |
2 | Modify src/MainActivity.java file to add the code |
3 | Modify layout XML file res/layout/activity_main.xml add any GUI component if required. |
4 | Run the application and choose a running android device and install the application on it and verify the results. |
*src/MainActivity.java* のコンテンツは次のとおりです。
package com.example.sairamkrishna.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* のコンテンツは次のとおりです。
<?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="Tutorials point"
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* のコンテンツは次のとおりです。
<resources>
<string name="app_name">My Application</string>
</resources>
*AndroidManifest.xml* のコンテンツは次のとおりです。
<?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>
アプリケーションを実行してみましょう。 実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像の実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 Android Studioは次の画像を表示します。
上の画像は、アプリケーションの出力を示しています
ユーザー名フィールドに入力するだけで、辞書にある組み込みのAndroidの提案が表示されます。 これは上に示されています。
これで、パスワードフィールドが表示されます。 フィールドで書き込みを開始するとすぐに消えます。 上に示されています。
最後に、デバイスをACケーブルまたはUSBケーブルに接続し、充電チェックボタンを押します。 私の場合、AC電源を接続すると、次のメッセージが表示されます。