求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   Code  
会员   
要资料
 
 
 

Android开发教程
Android 开发环境配置
Android 架构
Android 应用组件
Android Hello World示例
Android 资源组织和访问
Android Activity
Android Service
Android 广播接收器
Android 内容提供者
Android 碎片/片段
Android Intent过滤器
Android UI布局
Android UI控件
Android 事件处理
Android 样式和主题
Android 自定义组件
Android 拖放
Android 通知
Android 基于位置服务
Android 发送电子邮件
Android 发送短信/SMS
Android 拨打电话
发布Android应用
ndroid Alertdialog
Android Animation实例
Android音频捕获
Android音频管理器实例
Android
Android最佳实践
Android Bluetooth实例
Android Camera
Android Clipboard
Android自定义字体
Android数据备份
Android Gestures/手势
Android图片效果
Android图片切换
Android内部存储
Android JetPlayer实例
Android JSON解析器
Android加载Spinner
Android本地化
Android登录实例
Android MediaPlayer
 
 

Android最佳实践
765 次浏览
36次  

有一些做法可以遵循,在开发Android应用程序。这些建议由Android自身和保持在对于时间里可改善。

这些最佳实践包括交互设计功能,性能,安全性和私隐,兼容性,测试,分发和货币化的提示。它们被缩小并列示如下。

最佳实践 - 用户输入

每个文本字段都用于不同的工作。例如,一些文本字段是文本,有些是用于数字。如果它是数字那么最好是显示数字键盘时文本字段居中。其语法如下。

<EditText
      android:id="@+id/phone"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:inputType="phone" />

其他然后,如果字段是密码,那么它必须显示密码提示,这样用户可以很容易地记住密码。它可以实现为如下:

<EditText
      android:id="@+id/password"
      android:hint="@string/password_hint"
      android:inputType="textPassword" />

最佳实践 - 后台作业

但是也有一些在应用程序后台运行的某些工作在应用程序。这些工作可能是获取从互联网上的一些数据或东西,播放音乐等它建议在长等待任务不应在UI线程和相当的后台由服务或异步工作完成。

异步任务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);

最佳实践 - 安全和隐私

它是应用程序应该是安全的,而不是仅重视应用,但用户数据和应用程序数据也应得到保障。安全性可提高受以下因素。

  1. 使用内部存储而不是外部存储应用程序的文件

  2. 尽可能使用内容提供商

  3. 连接到网络时使用SSL

  4. 使用适当的权限来访问设备的不同功能

例子

下面的例子演示了一些开发Android应用程序时应该遵循的最佳实践。创建一个基本的应用程序,允许指定如何使用文本字段,以及如何通过检查手机的充电状态,以提高性能。

为了试验这个例子,需要在实际设备上运行。

Steps 描述
1 使用Android Studio创建Android应用程序,并将其命名为:BestPractices。在创建这个项目时确保目标SDK编译在Android SDK的最新版本或使用更高级别的API。
2 修改?src/MainActivity.j ava 文件添加代码
3 如果修改所需的布局XML文件?res/layout/activity_main.xml添加GUI组件
4 修改 res/values/string.xml?文件,并添加必要的字符串常量组件值
5 修改 AndroidManifest.xml?添加必要的权限
6 运行应用程序并选择运行Android的设备,并在其上安装的应用和验证结果

这里为?src/com.yiibai.bestpractices/MainActivity.java?的内容?

package com.example.bestpractices;
    import android.os.BatteryManager;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    public class MainActivity extends Activity {
       private Button Check;
       private BatteryManager battery;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Check = (Button)findViewById(R.id.button1);
     }
       public void check(View view){
         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;
        // How are we charging?
        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
       public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
     } 
 }  

以下是文件?activity_main.xml?的内容:

<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:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".MainActivity" >
  
  <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="20dp"
        android:text="@string/username"
        android:textAppearance="?android:attr/textAppearanceMedium" />
       <EditText
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:ems="10"
        android:inputType="textCapSentences|textAutoCorrect" >
       <requestFocus />
  </EditText>
       <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="34dp"
        android:ems="10"
        android:hint="@string/password_hint"
        android:inputType="textPassword" />
       <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/message"
        android:layout_marginTop="50dp"
        android:text="@string/password"
        android:textAppearance="?android:attr/textAppearanceMedium" />
       <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/password"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp"
        android:onClick="check"
        android:text="@string/check" />
    </RelativeLayout>

以下是文件?Strings.xml?的内容:

<?xml version="1.0" encoding="utf-8"?>
    <resources>
       <string name="app_name">BestPractices</string>
       <string name="action_settings">Settings</string>
       <string name="hello_world">Hello world!</string>
       <string name="username">Username</string>
       <string name="password">Password</string>
       <string name="password_hint">Hello world!</string>
       <string name="check">Charging check</string>
    </resources>

以下是文件??AndroidManifest.xml?的内容:

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.yiibai.bestpractices"
        android:versionCode="1"
        android:versionName="1.0" >
     <uses-sdk
         android:minSdkVersion="8"
         android:targetSdkVersion="17" />
     <application
         android:allowBackup="true"
         android:icon="@drawable/ic_launcher"
         android:label="@string/app_name"
         android:theme="@style/AppTheme" >
    <activity
           android:name="com.yiibai.bestpractices.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>

让我们试着运行BestPractices应用程序。启动应用程序之前会显示如下窗口,选择要运行的Android应用程序的选项。

选择移动设备作为一个选项,然后检看移动设备将显示如下画面。

现在,只需键入的用户名字段,会看到内置的词典建议将启动显示出来。这如下所示。

现在,将看到提示在密码字段。它尽快将消失,开始写入的字段。它如下所示。

最后,只需将设备连接到AC线或USB线,按下充电复选按钮。就我而言,通过USB电缆连接PC,以便它显示以下信息。



您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码: 验证码,看不清楚?请点击刷新验证码 必填



765 次浏览
36次
 捐助