Android程序----工厂测试软件

1.Lcd测试代码:

  1. package com.android.factorytest;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.graphics.Color;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.TextView;
  10. public class TestColor extends Activity{
  11. private static final StringTAG= "TestColor" ;
  12. private ButtonmReturn= null ;
  13. private ButtonmChangeColor= null ;
  14. private ButtonmNext= null ;
  15. private TextViewmText1= null ;
  16. private TextViewmText2= null ;
  17. private TextViewmText3= null ;
  18. private IntentmIntent= null ;
  19. private int mNum= 0 ;
  20. protected void onCreate(BundlesavedInstanceState)
  21. {
  22. super .onCreate(savedInstanceState);
  23. setContentView(R.layout.test_color);
  24. initView();
  25. }
  26. private void initView()
  27. {
  28. setTitle(R.string.test_color_mess);
  29. mReturn=(Button)findViewById(R.id.but_return);
  30. mChangeColor=(Button)findViewById(R.id.but_changecolor);
  31. mNext=(Button)findViewById(R.id.but_next);
  32. mText1=(TextView)findViewById(R.id.test_color_text1);
  33. mText2=(TextView)findViewById(R.id.test_color_text2);
  34. mText3=(TextView)findViewById(R.id.test_color_text3);
  35. mReturn.setOnClickListener(new View.OnClickListener()
  36. {
  37. public void onClick(Viewv)
  38. {
  39. mIntent=new Intent(TestColor. this ,MainActivity. class );
  40. startActivity(mIntent);
  41. }
  42. });
  43. mNext.setOnClickListener(new View.OnClickListener()
  44. {
  45. public void onClick(Viewv)
  46. {
  47. mIntent=new Intent(TestColor. this ,TestSd. class );
  48. //mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  49. finish();
  50. startActivity(mIntent);
  51. }
  52. });
  53. mChangeColor.setOnClickListener(new View.OnClickListener()
  54. {
  55. public void onClick(Viewv)
  56. {
  57. mNum++;
  58. changeColor(mNum);
  59. }
  60. });
  61. }
  62. private void changeColor( int num)
  63. {
  64. Log.e(TAG,"num=" +(num% 6 ));
  65. switch (num% 6 )
  66. {
  67. case 0 :
  68. mText1.setBackgroundColor(Color.RED);
  69. mText2.setBackgroundColor(Color.RED);
  70. mText3.setBackgroundColor(Color.RED);
  71. break ;
  72. case 1 :
  73. mText1.setBackgroundColor(Color.GREEN);
  74. mText2.setBackgroundColor(Color.GREEN);
  75. mText3.setBackgroundColor(Color.GREEN);
  76. break ;
  77. case 2 :
  78. mText1.setBackgroundColor(Color.BLUE);
  79. mText2.setBackgroundColor(Color.BLUE);
  80. mText3.setBackgroundColor(Color.BLUE);
  81. break ;
  82. case 3 :
  83. mText1.setBackgroundColor(Color.RED);
  84. mText2.setBackgroundColor(Color.RED);
  85. mText3.setBackgroundColor(Color.RED);
  86. break ;
  87. case 4 :
  88. mText1.setBackgroundColor(Color.GREEN);
  89. mText2.setBackgroundColor(Color.GREEN);
  90. mText3.setBackgroundColor(Color.GREEN);
  91. break ;
  92. case 5 :
  93. mText1.setBackgroundColor(Color.BLUE);
  94. mText2.setBackgroundColor(Color.BLUE);
  95. mText3.setBackgroundColor(Color.BLUE);
  96. break ;
  97. }
  98. }
  99. }

LCD测试XML:

  1. <?xmlversion= "1.0" encoding= "utf-8" ?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#383838"
  6. android:orientation="vertical"
  7. android:gravity="center" >
  8. <LinearLayout
  9. android:orientation="horizontal"
  10. android:layout_width="fill_parent"
  11. android:layout_height="fill_parent"
  12. android:layout_weight="1" >
  13. <TextViewandroid:id="@+id/test_color_text1"
  14. android:layout_width="fill_parent"
  15. android:layout_height="fill_parent"
  16. android:layout_weight="1"
  17. android:background="#ffff0000" />
  18. <TextViewandroid:id="@+id/test_color_text2"
  19. android:layout_width="fill_parent"
  20. android:layout_height="fill_parent"
  21. android:layout_weight="1"
  22. android:background="#ffff0000" />
  23. <TextViewandroid:id="@+id/test_color_text3"
  24. android:layout_width="fill_parent"
  25. android:layout_height="fill_parent"
  26. android:layout_weight="1"
  27. android:background="#ffff0000" />
  28. </LinearLayout>
  29. <LinearLayout
  30. android:orientation="horizontal"
  31. android:layout_width="fill_parent"
  32. android:layout_height="wrap_content"
  33. android:layout_marginTop="5dp"
  34. android:gravity="center" >
  35. <Buttonandroid:id="@+id/but_return"
  36. android:layout_width="200dp"
  37. android:layout_height="wrap_content"
  38. android:layout_marginLeft="20dp"
  39. android:text="@string/but_return" />
  40. <Buttonandroid:id="@+id/but_changecolor"
  41. android:layout_width="200dp"
  42. android:layout_height="wrap_content"
  43. android:text="@string/but_changecolor" />
  44. <Buttonandroid:id="@+id/but_next"
  45. android:layout_width="200dp"
  46. android:layout_height="wrap_content"
  47. android:layout_marginRight="20dp"
  48. android:text="@string/but_next" />
  49. </LinearLayout>
  50. </LinearLayout>

2.SD卡测试代码:

  1. package com.android.factorytest;
  2. import java.io.File;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.graphics.Color;
  6. import android.os.Bundle;
  7. import android.os.Environment;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.TextView;
  12. import java.io.FileOutputStream;
  13. import java.io.OutputStreamWriter;
  14. public class TestSd extends Activity
  15. {
  16. private static final StringLOG_TAG= null ;
  17. private TextViewmTestSd= null ;
  18. private ButtonmReturn= null ;
  19. private ButtonmNext= null ;
  20. private IntentmIntent= null ;
  21. private FilesdcardDir;
  22. //privateStringPATH="/abc";
  23. private StringFILENAME= "/sdcard/cet4hard.txt" ;
  24. //privateStringFILENAME="/mnt/sdcard/cet4hard.txt";//emulatortest
  25. @Override
  26. protected void onCreate(BundlesavedInstanceState)
  27. {
  28. super .onCreate(savedInstanceState);
  29. setContentView(R.layout.test_sd);
  30. sdcardDir=Environment.getExternalStorageDirectory();
  31. initView();
  32. }
  33. private void initView()
  34. {
  35. try
  36. {
  37. Log.d(LOG_TAG,"StartWrite" );
  38. if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
  39. {
  40. //Filepath=newFile(sdcardDir+PATH);
  41. Filef=new File(sdcardDir+FILENAME);
  42. //Filef=newFile(FILENAME);//emulatortest
  43. boolean flag= false ;
  44. //if(!path.exists())
  45. //{
  46. //flag=path.mkdirs();
  47. //if(flag)
  48. //{
  49. //mTestSd=(TextView)findViewById(R.id.test_sd);
  50. //mTestSd.setText(R.string.test_sd_mess3);
  51. //}
  52. //else
  53. //{
  54. //mTestSd=(TextView)findViewById(R.id.test_sd_delete);
  55. //setContentView(R.id.test_sd_delete);
  56. //}
  57. //}
  58. if (!f.exists())
  59. {
  60. flag=f.createNewFile();
  61. if (flag)
  62. {
  63. mTestSd=(TextView)findViewById(R.id.test_sd_create);
  64. mTestSd.setText(R.string.test_sd_mess3);
  65. mTestSd.setTextColor(Color.GREEN);
  66. OutputStreamWriterosw=new OutputStreamWriter( new FileOutputStream(f));
  67. Stringeditor="fdfd" ;
  68. osw.write(editor.toString());
  69. osw.close();
  70. }
  71. else
  72. {
  73. mTestSd=(TextView)findViewById(R.id.test_sd_create);
  74. mTestSd.setText(R.string.test_sd_mess5);
  75. mTestSd.setTextColor(Color.RED);
  76. }
  77. }
  78. else
  79. {
  80. mTestSd=(TextView)findViewById(R.id.test_sd_create);
  81. mTestSd.setText(R.string.test_sd_mess5);
  82. mTestSd.setTextColor(Color.RED);
  83. }
  84. if (f.exists())
  85. {
  86. flag=f.delete();
  87. if (flag)
  88. {
  89. mTestSd=(TextView)findViewById(R.id.test_sd_delete);
  90. mTestSd.setText(R.string.test_sd_mess4);
  91. mTestSd.setTextColor(Color.GREEN);
  92. }
  93. else
  94. {
  95. mTestSd=(TextView)findViewById(R.id.test_sd_delete);
  96. mTestSd.setText(R.string.test_sd_mess6);
  97. mTestSd.setTextColor(Color.RED);
  98. }
  99. }
  100. else
  101. {
  102. mTestSd=(TextView)findViewById(R.id.test_sd_delete);
  103. mTestSd.setText(R.string.test_sd_mess6);
  104. mTestSd.setTextColor(Color.RED);
  105. }
  106. }
  107. else
  108. {
  109. mTestSd=(TextView)findViewById(R.id.test_sd);
  110. mTestSd.setText(R.string.test_sd_mess2);
  111. mTestSd.setTextColor(Color.RED);
  112. }
  113. }
  114. catch (Exceptione)
  115. {
  116. Log.d(LOG_TAG,"filecreateerror" );
  117. }
  118. mReturn=(Button)findViewById(R.id.but_return);
  119. mNext=(Button)findViewById(R.id.but_next);
  120. mReturn.setOnClickListener(new View.OnClickListener()
  121. {
  122. public void onClick(Viewv)
  123. {
  124. mIntent=new Intent(TestSd. this ,MainActivity. class );
  125. startActivity(mIntent);
  126. }
  127. });
  128. mNext.setOnClickListener(new View.OnClickListener()
  129. {
  130. public void onClick(Viewv)
  131. {
  132. mIntent=new Intent(TestSd. this ,TestCamera. class );
  133. //mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  134. finish();
  135. startActivity(mIntent);
  136. }
  137. });
  138. }
  139. }

SD卡测试XML:

  1. <?xmlversion= "1.0" encoding= "utf-8" ?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#383838"
  6. android:orientation="vertical"
  7. android:gravity="center" >
  8. <LinearLayout
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_weight="1"
  12. android:orientation="vertical"
  13. android:gravity="center" >
  14. <TextView
  15. android:id="@+id/test_sd"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:textSize="20sp"
  19. />
  20. </LinearLayout>
  21. <LinearLayout
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:layout_weight="1"
  25. android:orientation="vertical"
  26. android:gravity="center" >
  27. <TextView
  28. android:id="@+id/test_sd_create"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:textSize="20sp"
  32. />
  33. </LinearLayout>
  34. <LinearLayout
  35. android:layout_width="fill_parent"
  36. android:layout_height="wrap_content"
  37. android:layout_weight="1"
  38. android:orientation="vertical"
  39. android:gravity="center" >
  40. <TextView
  41. android:id="@+id/test_sd_delete"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:textSize="20sp"
  45. />
  46. </LinearLayout>
  47. <LinearLayout
  48. android:orientation="horizontal"
  49. android:layout_width="fill_parent"
  50. android:layout_height="wrap_content"
  51. android:layout_marginTop="5dp"
  52. android:gravity="center" >
  53. <Buttonandroid:id="@+id/but_return"
  54. android:layout_width="200dp"
  55. android:layout_height="wrap_content"
  56. android:text="@string/but_return" />
  57. <Buttonandroid:id="@+id/but_next"
  58. android:layout_width="200dp"
  59. android:layout_height="wrap_content"
  60. android:text="@string/but_next" />
  61. </LinearLayout>
  62. </LinearLayout>

3.Camera测试代码:

  1. package com.android.factorytest;
  2. import android.app.Activity;
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.IntentFilter;
  7. import android.os.Bundle;
  8. import android.provider.MediaStore;
  9. import android.view.View;
  10. import android.widget.Button;
  11. public class TestCamera extends Activity
  12. {
  13. private ButtonmReturn= null ;
  14. private ButtonmChangeCamera= null ;
  15. private ButtonmNext= null ;
  16. private IntentmIntent= null ;
  17. private static int TAKE_PICTURE= 1 ;
  18. protected void onCreate(BundlesavedInstanceState)
  19. {
  20. super .onCreate(savedInstanceState);
  21. setContentView(R.layout.test_camera);
  22. initView();
  23. RegListener();
  24. }
  25. public void RegListener(){
  26. ExitListenerReceiverexitre=new ExitListenerReceiver();
  27. IntentFilterintentfilter=new IntentFilter();
  28. intentfilter.addAction(this .getPackageName()+ "."
  29. +"ExitListenerReceiver" );
  30. this .registerReceiver(exitre,intentfilter);
  31. }
  32. class ExitListenerReceiver extends BroadcastReceiver{
  33. @Override
  34. public void onReceive(Contextarg0,Intentarg1){
  35. //TODOAuto-generatedmethodstub
  36. ((Activity)arg0).finish();
  37. }
  38. }
  39. private void initView()
  40. {
  41. setTitle(R.string.test_camera_mess);
  42. mReturn=(Button)findViewById(R.id.but_return);
  43. mChangeCamera=(Button)findViewById(R.id.but_changecamera);
  44. mNext=(Button)findViewById(R.id.but_next);
  45. mReturn.setOnClickListener(new View.OnClickListener()
  46. {
  47. public void onClick(Viewv)
  48. {
  49. mIntent=new Intent(TestCamera. this ,MainActivity. class );
  50. startActivity(mIntent);
  51. }
  52. });
  53. mNext.setOnClickListener(new View.OnClickListener()
  54. {
  55. public void onClick(Viewv)
  56. {
  57. mIntent=new Intent(TestCamera. this ,TestWiFi. class );
  58. //mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  59. finish();
  60. startActivity(mIntent);
  61. }
  62. });
  63. mChangeCamera.setOnClickListener(new View.OnClickListener()
  64. {
  65. public void onClick(Viewv)
  66. {
  67. mIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  68. startActivityForResult(mIntent,TAKE_PICTURE);
  69. }
  70. });
  71. }
  72. }

Camera测试XML:

  1. <?xmlversion= "1.0" encoding= "utf-8" ?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#383838"
  6. android:orientation="vertical"
  7. android:gravity="center" >
  8. <LinearLayout
  9. android:orientation="horizontal"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:layout_weight="1"
  13. android:gravity="center" >
  14. <TextView
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:textSize="20sp"
  18. android:text="@string/test_camera_mess" />
  19. </LinearLayout>
  20. <LinearLayout
  21. android:orientation="horizontal"
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:layout_marginTop="5dp"
  25. android:gravity="center" >
  26. <Buttonandroid:id="@+id/but_return"
  27. android:layout_width="200dp"
  28. android:layout_height="wrap_content"
  29. android:layout_marginLeft="20dp"
  30. android:text="@string/but_return" />
  31. <Buttonandroid:id="@+id/but_changecamera"
  32. android:layout_width="200dp"
  33. android:layout_height="wrap_content"
  34. android:text="@string/but_camera_open" />
  35. <Buttonandroid:id="@+id/but_next"
  36. android:layout_width="200dp"
  37. android:layout_height="wrap_content"
  38. android:layout_marginRight="20dp"
  39. android:text="@string/but_next" />
  40. </LinearLayout>
  41. </LinearLayout>

4.Wifi测试代码:

  1. package com.android.factorytest;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. //importandroid.net.ConnectivityManager;
  5. //importandroid.net.wifi.WifiConfiguration;
  6. import android.net.wifi.WifiManager;
  7. import android.os.Bundle;
  8. import android.provider.Settings;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.Toast;
  13. public class TestWiFi extends Activity
  14. {
  15. private static final StringTAG= "TestWiFi" ;
  16. private ButtonmReturn= null ;
  17. private ButtonmChangeState= null ;
  18. private ButtonmNext= null ;
  19. private IntentmIntent= null ;
  20. private WifiManagermWifiManager= null ;
  21. //privateConnectivityManagerconnectivity=null;
  22. private int mWifiState=- 1 ;
  23. //privateintnetworkPreference=-1;
  24. @Override
  25. protected void onCreate(BundlesavedInstanceState)
  26. {
  27. super .onCreate(savedInstanceState);
  28. setContentView(R.layout.test_wifi);
  29. initView();
  30. }
  31. private void initView()
  32. {
  33. mReturn=(Button)findViewById(R.id.but_return);
  34. mNext=(Button)findViewById(R.id.but_next);
  35. mChangeState=(Button)findViewById(R.id.but_changestate);
  36. if (mChangeState== null )
  37. Log.e(TAG,"mChangeStateisnull" );
  38. mReturn.setOnClickListener(new View.OnClickListener()
  39. {
  40. public void onClick(Viewv)
  41. {
  42. mIntent=new Intent(TestWiFi. this ,MainActivity. class );
  43. startActivity(mIntent);
  44. }
  45. });
  46. mNext.setOnClickListener(new View.OnClickListener()
  47. {
  48. public void onClick(Viewv)
  49. {
  50. mIntent=new Intent(TestWiFi. this ,TestBluetooth. class );
  51. //mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  52. finish();
  53. startActivity(mIntent);
  54. }
  55. });
  56. mWifiManager=(WifiManager)getSystemService(WIFI_SERVICE);
  57. if (mWifiManager.getWifiState()==WifiManager.WIFI_STATE_ENABLED)
  58. {
  59. mWifiState=1 ;
  60. //将当前网络设置为首选网络
  61. //connectivity=(ConnectivityManager)getSystemService(WIFI_SERVICE);
  62. //networkPreference=connectivity.getNetworkPreference();
  63. //connectivity.setNetworkPreference(networkPreference);
  64. //mChangeState.setText(R.string.but_wifi_close);
  65. mChangeState.setText(R.string.test_wifi_mess_enter_wifisetting);
  66. }
  67. else if (mWifiManager.getWifiState()==WifiManager.WIFI_STATE_DISABLED)
  68. {
  69. mWifiState=0 ;
  70. //mChangeState.setText(R.string.but_wifi_open);
  71. mChangeState.setText(R.string.test_wifi_mess_enter_wifisetting);
  72. }
  73. else
  74. {
  75. Log.e(TAG,"WifiState=" +mWifiManager.getWifiState());
  76. }
  77. mChangeState.setOnClickListener(new View.OnClickListener()
  78. {
  79. public void onClick(Viewv)
  80. {
  81. changeWifiState(mWifiState);
  82. mWifiState++;
  83. }
  84. });
  85. }
  86. private void changeWifiState( int state)
  87. {
  88. Log.e(TAG,"mWifiState=" +state);
  89. Toast.makeText(this ,R.string.test_wifi_mess_enter_wifisetting,Toast.LENGTH_LONG).show();
  90. //startActivity(newIntent(Settings.ACTION_WIRELESS_SETTINGS)); //进入无线网络配置界面
  91. startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); //直接进入wifi网络设置界面
  92. switch (state% 2 )
  93. {
  94. case 0 :
  95. {
  96. new Thread()
  97. {
  98. public void run()
  99. {
  100. Log.e(TAG,"setWifiEnabled(true)" );
  101. mWifiManager.setWifiEnabled(true );
  102. }
  103. }.start();
  104. //if(mIntent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION))
  105. //{
  106. //Toast.makeText(this,R.string.test_wifi_mess_state_changed,Toast.LENGTH_LONG).show();
  107. //}
  108. while ((mWifiManager.getWifiState()==WifiManager.WIFI_STATE_ENABLING)||
  109. (mWifiManager.getWifiState()==WifiManager.WIFI_STATE_ENABLED))
  110. {
  111. Toast.makeText(this ,R.string.test_wifi_mess_enable,Toast.LENGTH_LONG).show();
  112. break ;
  113. }
  114. //获取网络列表并激活一个网络连接
  115. //List<WifiConfiguration>configurations=mWifiManager.getConfiguredNetworks();
  116. //if(configurations.size()>0)
  117. //{
  118. //intnetID=configurations.get(0).networkId;
  119. //booleandisableAllOthers=true;
  120. //mWifiManager.enableNetwork(netID,disableAllOthers);
  121. //}
  122. //mChangeState.setText(R.string.but_wifi_close);
  123. mChangeState.setText(R.string.test_wifi_mess_enter_wifisetting);
  124. break ;
  125. }
  126. case 1 :
  127. {
  128. new Thread()
  129. {
  130. public void run()
  131. {
  132. Log.e(TAG,"setWifiEnabled(false)" );
  133. mWifiManager.setWifiEnabled(false );
  134. }
  135. }.start();
  136. //mChangeState.setText(R.string.but_wifi_open);
  137. mChangeState.setText(R.string.test_wifi_mess_enter_wifisetting);
  138. //if(mIntent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION))
  139. //{
  140. //Toast.makeText(this,R.string.test_wifi_mess_state_changed,Toast.LENGTH_LONG).show();
  141. //}
  142. while ((mWifiManager.getWifiState()==WifiManager.WIFI_STATE_DISABLING)||
  143. (mWifiManager.getWifiState()==WifiManager.WIFI_STATE_DISABLED))
  144. {
  145. Toast.makeText(this ,R.string.test_wifi_mess_disable,Toast.LENGTH_LONG).show();
  146. break ;
  147. }
  148. break ;
  149. }
  150. default :
  151. Log.e(TAG,"Wifistateerror!" );
  152. Toast.makeText(this ,R.string.test_wifi_mess_error,Toast.LENGTH_LONG).show();
  153. break ;
  154. }
  155. }
  156. }

Wifi测试XML:

  1. <?xmlversion= "1.0" encoding= "utf-8" ?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#383838"
  6. android:orientation="vertical"
  7. android:gravity="center" >
  8. <LinearLayout
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_weight="1"
  12. android:orientation="vertical"
  13. android:gravity="center" >
  14. <TextView
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:textSize="20sp"
  18. android:text="@string/test_wifi_mess" />
  19. </LinearLayout>
  20. <LinearLayout
  21. android:orientation="horizontal"
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:layout_marginTop="5dp"
  25. android:gravity="center" >
  26. <Buttonandroid:id="@+id/but_return"
  27. android:layout_width="200dp"
  28. android:layout_height="wrap_content"
  29. android:text="@string/but_return" />
  30. <Buttonandroid:id="@+id/but_changestate"
  31. android:layout_width="200dp"
  32. android:layout_height="wrap_content"
  33. android:text="@string/but_wifi_close" />
  34. <Buttonandroid:id="@+id/but_next"
  35. android:layout_width="200dp"
  36. android:layout_height="wrap_content"
  37. android:text="@string/but_next" />
  38. </LinearLayout>
  39. </LinearLayout>

5.Bluetooth测试代码:

  1. packagecom.android.factorytest;
  2. importandroid.app.Activity;
  3. importandroid.bluetooth.BluetoothAdapter;
  4. importandroid.content.BroadcastReceiver;
  5. importandroid.content.Context;
  6. importandroid.content.Intent;
  7. importandroid.content.IntentFilter;
  8. importandroid.graphics.Color;
  9. importandroid.os.Bundle;
  10. importandroid.provider.Settings;
  11. importandroid.util.Log;
  12. importandroid.view.View;
  13. importandroid.widget.Button;
  14. importandroid.widget.TextView;
  15. importandroid.widget.Toast;
  16. public class TestBluetoothextendsActivity
  17. {
  18. private static finalStringTAG= "TestBluetooth" ;
  19. private TextViewmBluetoothstate= null ;
  20. private ButtonmReturn= null ;
  21. private ButtonmChangeState= null ;
  22. private ButtonmNext= null ;
  23. private IntentmIntent= null ;
  24. private BluetoothAdaptermBluetooth= null ;
  25. private int mBluetoothState=-1;
  26. @Override
  27. protected void onCreate(BundlesavedInstanceState)
  28. {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.test_bluetooth);
  31. initView();
  32. }
  33. private void initView()
  34. {
  35. mReturn=(Button)findViewById(R.id.but_return);
  36. mNext=(Button)findViewById(R.id.but_next);
  37. mChangeState=(Button)findViewById(R.id.but_changestate);
  38. mBluetoothstate=(TextView)findViewById(R.id.bluetooth_state);
  39. if (mChangeState== null )
  40. Log.e(TAG,"mChangeStateisnull" );
  41. mReturn.setOnClickListener(new View.OnClickListener()
  42. {
  43. public void onClick(Viewv)
  44. {
  45. mIntent=new Intent(TestBluetooth. this ,MainActivity. class );
  46. startActivity(mIntent);
  47. }
  48. });
  49. mNext.setOnClickListener(new View.OnClickListener()
  50. {
  51. public void onClick(Viewv)
  52. {
  53. mIntent=new Intent(TestBluetooth. this ,TestColor. class );
  54. finish();
  55. startActivity(mIntent);
  56. }
  57. });
  58. mBluetooth=BluetoothAdapter.getDefaultAdapter();
  59. if ( null ==mBluetooth)
  60. {
  61. mBluetoothstate.setText("NoBluetoothDevice" );
  62. mBluetoothstate.setTextColor(Color.RED);
  63. }
  64. if (mBluetooth.isEnabled())
  65. {
  66. mBluetoothState=1;
  67. mChangeState.setText(R.string .but_bluetooth_close);
  68. }
  69. else if (!mBluetooth.isEnabled())
  70. {
  71. mBluetoothState=0;
  72. mChangeState.setText(R.string .but_bluetooth_open);
  73. }
  74. else
  75. {
  76. Log.e(TAG,"BluetoothState=" );
  77. }
  78. mChangeState.setOnClickListener(new View.OnClickListener()
  79. {
  80. public void onClick(Viewv)
  81. {
  82. startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS)); //进入Bluetooth配置界面
  83. changeBluetoothState(mBluetoothState);
  84. mBluetoothState++;
  85. }
  86. });
  87. }
  88. private void changeBluetoothState( int state)
  89. {
  90. Log.e(TAG,"mBluetoothState=" +state);
  91. switch (state%2)
  92. {
  93. case 0:
  94. {
  95. startBluetooth();
  96. Toast.makeText(this ,R. string .test_bluetooth_mess_state_changed,Toast.LENGTH_LONG).show();
  97. mChangeState.setText(R.string .but_bluetooth_close);
  98. break ;
  99. }
  100. case 1:
  101. {
  102. stopBluetooth();
  103. Toast.makeText(this ,R. string .test_bluetooth_mess_state_changed,Toast.LENGTH_LONG).show();
  104. mChangeState.setText(R.string .but_bluetooth_open);
  105. break ;
  106. }
  107. default :
  108. Log.e(TAG,"Wifistateerror!" );
  109. Toast.makeText(this ,R. string .test_bluetooth_mess_error,Toast.LENGTH_LONG).show();
  110. break ;
  111. }
  112. }
  113. private void startBluetooth()
  114. {
  115. mBluetoothstate=(TextView)findViewById(R.id.bluetooth_state);
  116. mBluetooth=BluetoothAdapter.getDefaultAdapter();
  117. if ( null ==mBluetooth)
  118. {
  119. mBluetoothstate.setText("NoBluetoothDevice" );
  120. mBluetoothstate.setTextColor(Color.RED);
  121. }
  122. BroadcastReceiverBluetoothState=new BroadcastReceiver()
  123. {
  124. @Override
  125. public void onReceive(Contextcontext,Intentintent)
  126. {
  127. //StringprevStateExtra=BluetoothAdapter.EXTRA_PREVIOUS_STATE;
  128. StringstateExtra=BluetoothAdapter.EXTRA_STATE;
  129. int state=intent.getIntExtra(stateExtra,-1);
  130. //intpreviousState=intent.getIntExtra(prevStateExtra,-1);
  131. Stringtt="" ;
  132. switch (state)
  133. {
  134. case (BluetoothAdapter.STATE_TURNING_ON):
  135. {
  136. tt="Bluetoothturningon" ;
  137. break ;
  138. }
  139. case (BluetoothAdapter.STATE_ON):
  140. {
  141. tt="Bluetoothon" ;
  142. unregisterReceiver(this );
  143. break ;
  144. }
  145. case (BluetoothAdapter.STATE_TURNING_OFF):
  146. {
  147. tt="Bluetoothturningoff" ;
  148. break ;
  149. }
  150. case (BluetoothAdapter.STATE_OFF):
  151. {
  152. tt="Bluetoothoff" ;
  153. break ;
  154. }
  155. default :
  156. tt="UnkownBluetoothstate!" ;
  157. break ;
  158. }
  159. //Toast.makeText(this,tt,Toast.LENGTH_LONG).show();
  160. mBluetoothstate.setText(tt);
  161. }
  162. };
  163. if (!mBluetooth.isEnabled())
  164. {
  165. StringactionStateChanged=BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED;
  166. //StringactionRequestEnable=BluetoothAdapter.ACTION_REQUEST_ENABLE;
  167. registerReceiver(BluetoothState,new IntentFilter(actionStateChanged));
  168. //startActivityForResult(newIntent(actionRequestEnable),0);
  169. mBluetooth.enable();
  170. }
  171. }
  172. private void stopBluetooth()
  173. {
  174. mBluetoothstate=(TextView)findViewById(R.id.bluetooth_state);
  175. mBluetooth=BluetoothAdapter.getDefaultAdapter();
  176. if ( null ==mBluetooth)
  177. {
  178. mBluetoothstate.setText("NoBluetoothDevice" );
  179. mBluetoothstate.setTextColor(Color.RED);
  180. }
  181. BroadcastReceiverBluetoothState=new BroadcastReceiver()
  182. {
  183. @Override
  184. public void onReceive(Contextcontext,Intentintent)
  185. {
  186. //StringprevStateExtra=BluetoothAdapter.EXTRA_PREVIOUS_STATE;
  187. StringstateExtra=BluetoothAdapter.EXTRA_STATE;
  188. int state=intent.getIntExtra(stateExtra,-1);
  189. //intpreviousState=intent.getIntExtra(prevStateExtra,-1);
  190. Stringtt="UnkownBluetooth" ;
  191. switch (state)
  192. {
  193. case (BluetoothAdapter.STATE_TURNING_ON):
  194. {
  195. tt="Bluetoothturningon" ;
  196. break ;
  197. }
  198. case (BluetoothAdapter.STATE_ON):
  199. {
  200. tt="Bluetoothon" ;
  201. unregisterReceiver(this );
  202. break ;
  203. }
  204. case (BluetoothAdapter.STATE_TURNING_OFF):
  205. {
  206. tt="Bluetoothturningoff" ;
  207. break ;
  208. }
  209. case (BluetoothAdapter.STATE_OFF):
  210. {
  211. tt="Bluetoothoff" ;
  212. break ;
  213. }
  214. default :
  215. tt="UnkownBluetoothstate!" ;
  216. break ;
  217. }
  218. //Toast.makeText(this,tt,Toast.LENGTH_LONG).show();
  219. mBluetoothstate.setText(tt);
  220. }
  221. };
  222. if (mBluetooth.isEnabled())
  223. {
  224. StringactionStateChanged=BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED;
  225. registerReceiver(BluetoothState,new IntentFilter(actionStateChanged));
  226. mBluetooth.disable();
  227. }
  228. }
  229. }

Bluetooth测试Xml:

  1. <?xmlversion= "1.0" encoding= "utf-8" ?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#383838"
  6. android:orientation="vertical"
  7. android:gravity="center" >
  8. <LinearLayout
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_weight="1"
  12. android:orientation="vertical"
  13. android:gravity="center" >
  14. <TextView
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:textSize="20sp"
  18. android:text="@string/test_bluetooth_mess" />
  19. <TextView
  20. android:id="@+id/bluetooth_state"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:textSize="20sp" />
  24. </LinearLayout>
  25. <LinearLayout
  26. android:orientation="horizontal"
  27. android:layout_width="fill_parent"
  28. android:layout_height="wrap_content"
  29. android:layout_marginTop="5dp"
  30. android:gravity="center" >
  31. <Buttonandroid:id="@+id/but_return"
  32. android:layout_width="200dp"
  33. android:layout_height="wrap_content"
  34. android:text="@string/but_return" />
  35. <Buttonandroid:id="@+id/but_changestate"
  36. android:layout_width="200dp"
  37. android:layout_height="wrap_content"
  38. android:text="@string/but_bluetooth_close" />
  39. <Buttonandroid:id="@+id/but_next"
  40. android:layout_width="200dp"
  41. android:layout_height="wrap_content"
  42. android:text="@string/but_next" />
  43. </LinearLayout>
  44. </LinearLayout>

6.MainActivity代码:

  1. packagecom.android.factorytest;
  2. importandroid.app.Activity;
  3. importandroid.content.Intent;
  4. importandroid.os.Bundle;
  5. importandroid.view.View;
  6. importandroid.widget.Button;
  7. public class MainActivityextendsActivity
  8. {
  9. private ButtonmTestColor= null ;
  10. private ButtonmTestSd= null ;
  11. private ButtonmTestCamera= null ;
  12. private ButtonmTestWiFi= null ;
  13. private ButtonmTestBluetooth= null ;
  14. private IntentmIntent= null ;
  15. /**Calledwhentheactivityisfirstcreated.*/
  16. @Override
  17. public void onCreate(BundlesavedInstanceState)
  18. {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21. initButton();
  22. }
  23. private void initButton()
  24. {
  25. mTestColor=(Button)findViewById(R.id.test_color);
  26. mTestSd=(Button)findViewById(R.id.test_sd);
  27. mTestCamera=(Button)findViewById(R.id.test_camera);
  28. mTestWiFi=(Button)findViewById(R.id.test_wifi);
  29. mTestBluetooth=(Button)findViewById(R.id.test_bluetooth);
  30. mTestColor.setOnClickListener(new View.OnClickListener()
  31. {
  32. @Override
  33. public void onClick(Viewv)
  34. {
  35. mIntent=new Intent(MainActivity. this ,TestColor. class );
  36. //mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  37. finish();
  38. startActivity(mIntent);
  39. }
  40. });
  41. mTestSd.setOnClickListener(new View.OnClickListener()
  42. {
  43. @Override
  44. public void onClick(Viewv)
  45. {
  46. mIntent=new Intent(MainActivity. this ,TestSd. class );
  47. //mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  48. finish();
  49. startActivity(mIntent);
  50. }
  51. });
  52. mTestCamera.setOnClickListener(new View.OnClickListener()
  53. {
  54. @Override
  55. public void onClick(Viewv)
  56. {
  57. mIntent=new Intent(MainActivity. this ,TestCamera. class );
  58. //mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  59. finish();
  60. startActivity(mIntent);
  61. }
  62. });
  63. mTestWiFi.setOnClickListener(new View.OnClickListener()
  64. {
  65. @Override
  66. public void onClick(Viewv)
  67. {
  68. mIntent=new Intent(MainActivity. this ,TestWiFi. class );
  69. //mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  70. finish();
  71. startActivity(mIntent);
  72. }
  73. });
  74. mTestBluetooth.setOnClickListener(new View.OnClickListener()
  75. {
  76. @Override
  77. public void onClick(Viewv)
  78. {
  79. mIntent=new Intent(MainActivity. this ,TestBluetooth. class );
  80. //mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  81. finish();
  82. startActivity(mIntent);
  83. }
  84. });
  85. }
  86. }

main XML:

  1. <?xmlversion= "1.0" encoding= "utf-8" ?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:background="#383838"
  7. android:gravity="center"
  8. >
  9. <TextView
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_marginTop="20dp"
  13. android:layout_marginBottom="30dp"
  14. android:textSize="25dp"
  15. android:text="@string/app_name"
  16. />
  17. <Button
  18. android:id="@+id/test_color"
  19. android:layout_width="400dp"
  20. android:layout_height="wrap_content"
  21. android:layout_marginBottom="20dp"
  22. android:text="@string/but_test_color"
  23. />
  24. <Button
  25. android:id="@+id/test_sd"
  26. android:layout_width="400dp"
  27. android:layout_height="wrap_content"
  28. android:layout_marginBottom="20dp"
  29. android:text="@string/but_test_sd"
  30. />
  31. <Button
  32. android:id="@+id/test_camera"
  33. android:layout_width="400dp"
  34. android:layout_height="wrap_content"
  35. android:layout_marginBottom="20dp"
  36. android:text="@string/but_test_camera"
  37. />
  38. <Button
  39. android:id="@+id/test_wifi"
  40. android:layout_width="400dp"
  41. android:layout_height="wrap_content"
  42. android:layout_marginBottom="20dp"
  43. android:text="@string/but_test_wifi"
  44. />
  45. <Button
  46. android:id="@+id/test_bluetooth"
  47. android:layout_width="400dp"
  48. android:layout_height="wrap_content"
  49. android:layout_marginBottom="20dp"
  50. android:text="@string/but_test_bluetooth"
  51. />
  52. </LinearLayout>

7.资源文件String.Xml:

  1. <?xmlversion= "1.0" encoding= "utf-8" ?>
  2. <resources>
  3. <string name= "hello" >HelloWorld,MainActivity!</ string >
  4. <string name= "app_name" >工厂测试</ string >
  5. <string name= "but_test_color" >LCD 测试</ string >
  6. <string name= "but_test_sd" >SD卡测试</ string >
  7. <string name= "but_test_camera" >Camera 测试</ string >
  8. <string name= "but_test_wifi" >WiFi测试</ string >
  9. <string name= "but_test_bluetooth" >Bluetooth 测试</ string >
  10. <string name= "but_return" >返回菜单</ string >
  11. <string name= "but_retest" > 重测本项</ string >
  12. <string name= "but_changecolor" >改变颜色</ string >
  13. <string name= "but_next" > 下一项测试</ string >
  14. <string name= "but_exit" >退出</ string >
  15. <string name= "but_camera_open" > 打开Camera</ string >
  16. <string name= "but_camera_exit" >关闭Camera</ string >
  17. <string name= "but_wifi_open" >打开WiFi</ string >
  18. <string name= "but_wifi_close" >关闭WiFi</ string >
  19. <string name= "but_bluetooth_open" >打开Bluetooth</ string >
  20. <string name= "but_bluetooth_close" >关闭Bluetooth</ string >
  21. <string name= "test_sd_mess1" >SD 卡已经插入!</ string >
  22. <string name= "test_sd_mess2" >SD卡已经拔出!</ string >
  23. <string name= "test_sd_mess3" >在SD卡中创建文件成功!</ string >
  24. <string name= "test_sd_mess4" >从SD卡中删除文件成功!</ string >
  25. <string name= "test_sd_mess5" >在SD卡中创建文件失败!</ string >
  26. <string name= "test_sd_mess6" >从SD卡中删除文件失败!</ string >
  27. <string name= "test_color_mess" > 请点击“改变颜色”,显示是否正常!</ string >
  28. <string name= "test_camera_mess" >请点击“打开Camera”,测试 Camera是否正常!</ string >
  29. <string name= "test_wifi_mess" >请点击“打开WiFi/关闭WiFi”,是否有正 确的WiFi信息!</ string >
  30. <string name= "test_wifi_mess_enable" >WiFi打开成功!</ string >
  31. <string name= "test_wifi_mess_disable" >WiFi关闭成功!</ string >
  32. <string name= "test_wifi_mess_state_changed" >WiFi状态改 变!</ string >
  33. <string name= "test_wifi_mess_enter_wifisetting" >进入 WiFi设置界面</ string >
  34. <string name= "test_wifi_mess_error" >未知的WiFi状态!</ string >
  35. <string name= "test_bluetooth_mess" >请点击“打开Bluetooth/关闭 Bluetooth”,是否有正确的Bluetooth信息!</ string >
  36. <string name= "test_bluetooth_mess_state_changed" >Bluetooth 状态改变!</ string >
  37. <string name= "test_bluetooth_mess_error" >未知的Bluetooth 状态!</ string >
  38. </resources>

你可能感兴趣的:(android)