Before writing some small programs, are to take the template to use, no scrutiny good coding standards, in the look at the Google native example, found that many coding specifications can be
For example, the main UI Activity (Bluetoothchat.java) in Bluetoothchat, the native project, is also used for reference:
1. Declarations of related control variables (debug switches) and member variables:
//Debugging Private StaticFinal String TAG ="Bluetoothchat"; Private StaticFinal BooleanD = True; //Message types sent from the Bluetoothchatservice Handler Public StaticFinalintMessage_state_change =1; Public StaticFinalintMessage_read =2; Public StaticFinalintMessage_write =3; Public StaticFinalintMessage_device_name =4; Public StaticFinalintMessage_toast =5; //Key names received from the Bluetoothchatservice Handler Public StaticFinal String device_name ="device_name"; Public StaticFinal String TOAST ="Toast"; //Intent Request Codes Private StaticFinalintRequest_connect_device_secure =1; Private StaticFinalintRequest_connect_device_insecure =2; Private StaticFinalintREQUEST_ENABLE_BT =3; //Layout views PrivateListView Mconversationview; PrivateEditText Moutedittext; PrivateButton Msendbutton; //Name of the connected device PrivateString Mconnecteddevicename =NULL; //Array Adapter for the conversation thread PrivateArrayadapter<string>Mconversationarrayadapter; //String buffer for outgoing messages PrivateStringBuffer Moutstringbuffer; //Local Bluetooth Adapter PrivateBluetoothadapter Mbluetoothadapter =NULL; //Member object for the chat services PrivateBluetoothchatservice Mchatservice =NULL;
2. The function of the parent activity such as Oncreat (), OnStart ():
@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); if (D) log.e (TAG, "+ + + on CREATE + + +"); //Set up the window layoutSetcontentview (R.layout.main); //Get Local Bluetooth AdapterMbluetoothadapter =Bluetoothadapter.getdefaultadapter (); //IF The adapter is null and then Bluetooth was not supported if(Mbluetoothadapter = =NULL) {Toast.maketext ( This, "Bluetooth is not available", Toast.length_long). Show (); Finish (); return; } }
@Override Public voidOnStart () {Super. OnStart (); if(D) LOG.E (TAG, "+ + on START + +")); //If BT is isn't on, the request that it was enabled. //Setupchat () 'll then be called during Onactivityresult if(!mbluetoothadapter.isenabled ()) {Intent enableintent=NewIntent (bluetoothadapter.action_request_enable); Startactivityforresult (Enableintent, REQUEST_ENABLE_BT); //Otherwise, setup the chat session}Else { if(Mchatservice = =NULL) Setupchat (); } }
3. Define the relevant member method:
Private voidSetupchat () {log.d (TAG,"Setupchat ()"); //Initialize the array adapter for the conversation threadMconversationarrayadapter =NewArrayadapter<string> ( This, R.layout.message); Mconversationview=(ListView) Findviewbyid (r.id.in); Mconversationview.setadapter (Mconversationarrayadapter); //Initialize the Compose field with a listener for the return keyMoutedittext =(EditText) Findviewbyid (r.id.edit_text_out); Moutedittext.setoneditoractionlistener (Mwritelistener); //Initialize The Send button with a listener that for click eventsMsendbutton =(Button) Findviewbyid (r.id.button_send); Msendbutton.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {//Send a message using content of the edit text widgetTextView view =(TextView) Findviewbyid (r.id.edit_text_out); String message=View.gettext (). toString (); SendMessage (message); } }); //Initialize the Bluetoothchatservice to perform Bluetooth connectionsMchatservice =NewBluetoothchatservice ( This, Mhandler); //Initialize the buffer for outgoing messagesMoutstringbuffer =NewStringBuffer (""); }
4. Replication-related event response function:
@Override Public Booleanonoptionsitemselected (MenuItem item) {Intent serverintent=NULL; Switch(Item.getitemid ()) { CaseR.id.secure_connect_scan://Launch the devicelistactivity to see Devices and do scanServerintent =NewIntent ( This, Devicelistactivity.class); Startactivityforresult (Serverintent, request_connect_device_secure); return true; CaseR.id.insecure_connect_scan://Launch the devicelistactivity to see Devices and do scanServerintent =NewIntent ( This, Devicelistactivity.class); Startactivityforresult (Serverintent, request_connect_device_insecure); return true; Caser.id.discoverable://ensure this device was discoverable by othersensurediscoverable (); return true; } return false; }
Reference to the standard wording of Android activity