25 thg 5, 2012

Làm việc với Service trong Android.

Bài viết nhằm mục đích hiểu về gọi đến 1 service theo con đường gọi phương thức bindService() trong lập trình Android.
Mục tiêu : Tạo 1 service có chức năng tính toán và trả về kết quả của phương thức cộng 2 số nguyên.

Màn hình chương trình khi gọi phương thức cong2so(5,5) từ 1 service như sau :

Hàm onCreate(bundle) : nhận các đối tượng về từ giao diện :

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.tv);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }

Trước hết, tạo 1 lớp có tên là MyService mở rộng từ lớp Service như sau :
public class MyService extends Service {

    // Binder cho cac client
    private final IBinder mBinder = new LocalBinder();
   
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return mBinder;
    }
    //phương thức cho các client có thể gọi.
    public int cong2So(int a, int b){
        return a + b ;
    }
   
    public class LocalBinder extends Binder {
        // Trả về đối tượng MyService để cho client có thể gọi public method
        MyService getService(){
            return MyService.this;
           
        }       
    }
}

Trở lại Activity chính của ứng dụng :

Trong hàm OnStart() chúng ta dùng phương thức bindService(parameter) để gọi đến MyService thông qua đối tượng Intent .
 
@Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        // Bind to MyService
        Intent i = new Intent(this, MyService.class);
        bindService(i, mConnection, Context.BIND_AUTO_CREATE) 
    }

Trong hàm onStop() : Ngắt kết nối đến MyService
    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        //Unbind from Service
        if(mBound){
            mService.unbindService(mConnection);
            mBound = false ;
           
        }
    }

Tạo 1 biến mConnection từ lớp ServiceConnection nhằm Overide 2 phương thức sau nhằm bound đến MyService.
private ServiceConnection mConnection = new ServiceConnection() {
       
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mBound = false;
        }
       
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // Bound to MyService.
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            mBound = true;
        }
    };;;

Cuối cùng trong phương thức onClick của button ta gọi đến hàm cong2so(int,int) và hiển thị kết quả của phương thức ra 1 textview như sau :
@Override
    public void onClick(View v) {
        if(v.getId() == R.id.button){
            if(mBound){
                int result = mService.cong2So(5, 5);
                tv.setText(new String().valueOf(result));
            }
        }
    }

Trong Android manifesh.xml thêm dòng này :  <service android:name="MyService"></service> trong thẻ <application>

file main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/button" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>



23 thg 5, 2012

Souce code client server trên Android qua tcp socket programing

Mục tiêu :
- Demo cách thức hoạt động của client server qua giao thức TCP.
- Gửi nhận dữ liệu giữa Server và Client.
- Xây dựng các ứng dụng client/server trên các thiết bị chạy android độc lập, không phụ thuộc vào pc.

Ý tưởng :
- Gửi dữ liệu từ client lên server.
- Hiển thị dữ liệu ra editText.
- Thông báo toast notification khi dữ liệu được gửi đến.

Cần biết :
- Socket và SocketServer.
- Toast Notification.
- Telnet Client.

 Code :
- Cài đặt dịch vụ telnet client trên máy tính.
- Khởi động 2 máy ảo emulator.
- Vào cdm gõ lệnh sau : telnet localhost 5554 .
- Tiếp tục gõ lệnh :  redir add tcp:9999:999 để cấu hình ánh xạ port giữa local port và server port cho máy ảo 5554 qua port 9999.

Làm đúng sẽ có hình như sau :

Bây giờ chúng ta chạy ứng dụng server trên máy ảo 5554, client trên máy ảo 5556.
Kết nối giữa client và server qua port 9999 và ip : 10.0.2.2 . 

Thực hiện đúng chúng ta sẽ có hình minh họa sau : 

 
 Bên trái là server, bên phải là client. Chú ý các phần được bôi khung vàng. Chúc vui.









Bản beta đầu tiên

Sau 6 tháng cả team cặm cụi làm việc điên cuồng, bản alpha cũng được giới thiệu ra toàn bộ công ty và được testing nội bộ công ty mà thôi. ...