Tải bản đầy đủ (.doc) (6 trang)

Lập trình Android: Fast Dialer pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (112.47 KB, 6 trang )

Trung tâm Tin học – ĐH KHTN
Ứng dụng gọi điện
Sau đây mình sẽ tạo ra và chạy thử 1 ứng dụng gọi điện nho nhỏ trên Android:
1/ Các bạn tạo 1 project như sau:
Build Target: Android 2.2
Application Name: DialExample
Package Name: com.org. DialExample
Activity Name: DialExample
Min SDK Version: 8
2/ Các bạn tạo giao diện và xử lý bằng cách code trực tiếp trên file DialExample.java
như sau:
package com.org.DialExample;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class DialExample extends Activity {
EditText mEditText_number = null;
LinearLayout mLinearLayout_no_button = null;
Lập trình Android – Page 1
Trung tâm Tin học – ĐH KHTN
Button mButton_dial = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


mLinearLayout_no_button = new LinearLayout(this);
mEditText_number = new EditText(this);
mEditText_number.setText("01689938215");
mLinearLayout_no_button.addView(mEditText_number);
mButton_dial = new Button(this);
mButton_dial.setText("Goi !");
mLinearLayout_no_button.addView(mButton_dial);
mButton_dial.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
performDial();
}
});

setContentView(mLinearLayout_no_button);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
Lập trình Android – Page 2
Trung tâm Tin học – ĐH KHTN
if (keyCode == KeyEvent.KEYCODE_CALL)
{
performDial();
return true;
}
else if(keyCode == KeyEvent.KEYCODE_BACK)
{
finish();
return true;
}
else

return false;
}
public void performDial(){
if(mEditText_number!=null){
try {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" +
mEditText_number.getText())));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Lập trình Android – Page 3
Trung tâm Tin học – ĐH KHTN
Cuối cùng các bạn debug ứng dụng và sẽ đc như các hình sau:
Ban đầu:
Sau Khi bấm nút “Gọi”:
Lập trình Android – Page 4
Trung tâm Tin học – ĐH KHTN
Lập trình Android – Page 5
Trung tâm Tin học – ĐH KHTN
Mọi ý kiến đóng góp các bạn vui lòng post bài vào forum trang web
www.laptrinhdidong.vn . Rất mong nhận đc sự phản hồi của các bạn. Mình sẽ cập nhật
trang web thường xuyên
Lập trình Android – Page 6

×