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>
Đăng ký:
Đăng Nhận xét (Atom)
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. ...
-
Language : Java. Version 1.0.0 Write by Sungha References code on the internet Source code : http://www.mediafire.com/?ec7632r731me7vv...
-
Code : http://www.mediafire.com/?z24113j1jzn8kaf
-
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. ...
Không có nhận xét nào:
Đăng nhận xét