我想知道何时进入 Activity Android 执行函数本身以及何时需要调用函数。例如,在我下载的以下脚本中,前 4 个方法在没有调用的情况下执行,但最后一个sendMessage()
需要调用:
public class BroadcastChat extends Activity {
// Debugging
private static final String TAG = "BcastChat";
private static final boolean D = true;
// Message types sent from the BluetoothChatService Handler
public static final int MESSAGE_READ = 1;
public static final int MESSAGE_WRITE = 2;
public static final int MESSAGE_TOAST = 3;
// Key names received from the BroadcastChatService Handler
public static final String TOAST = "toast";
// Layout Views
private ListView mConversationView;
private EditText mOutEditText;
private Button mSendButton;
// Array adapter for the conversation thread
private ArrayAdapter<String> mConversationArrayAdapter;
// String buffer for outgoing messages
private StringBuffer mOutStringBuffer;
// Member object for the chat services
private BroadcastChatService mChatService = null;
// The Handler that gets information back from the BluetoothChatService
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if(D) Log.e(TAG, "[handleMessage !!!!!!!!!!!! ]");
switch (msg.what) {
case MESSAGE_WRITE:
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
mConversationArrayAdapter.add("Me: " + writeMessage);
break;
case MESSAGE_READ:
String readBuf = (String) msg.obj;
mConversationArrayAdapter.add("You: " + readBuf);
break;
case MESSAGE_TOAST:
Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
Toast.LENGTH_SHORT).show();
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(D) Log.e(TAG, "+++ ON CREATE +++");
// Set up the window layout
setContentView(R.layout.main);
}
public void onStart() {
super.onStart();
if(D) Log.e(TAG, "++ ON START ++");
setupChat();
}
@Override
public synchronized void onResume() {
super.onResume();
if(D) Log.e(TAG, "+ ON RESUME +");
mChatService.start();
}
private void sendMessage(String message) {
if(D) Log.e(TAG, "[sendMessage]");
// Check that there's actually something to send
if (message.length() > 0 ) {
// Get the message bytes and tell the BluetoothChatService to write
byte[] send = message.getBytes();
mChatService.write(send);
// Reset out string buffer to zero and clear the edit text field
mOutStringBuffer.setLength(0);
mOutEditText.setText(mOutStringBuffer);
}
}
... Incomplete script, just a part shown for the question
}
所以我的问题是双重的:
1-Android Activity
方法是从第一行到最后一行顺序调用的方法吗?,是否有一个循环使“指针”在到达最后一行后回到第一行?
2-您如何确定哪些方法将自动执行(如onCreate()
),哪些方法将等到它们被脚本的另一个方法调用。
非常感谢您的宝贵时间