2010年4月12日 星期一

連繫或解繫本地服務應用程式

在昨天我們談到啟動或停止本地服務應用程式,今天我們來看看另一種方式:連繫或解繫本地服務應用程式。

請大家注意連繫或解繫動作的順序。

1. 執行畫面

2. 按下bind按鈕


3.按下sum按鈕


4.按下Unbind按鈕
連繫或解繫的活動類別之程式列表如下:
package com.example.localservice;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class LocalServiceBinder extends Activity {
/** Called when the activity is first created. */
private LocalService mBoundService;
private boolean mIsBound;
private ServiceConnection mConnection = new ServiceConnection(){

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
mBoundService = ((LocalService.LocalBinder)service).getService();
Toast.makeText(LocalServiceBinder.this, "service connected",
Toast.LENGTH_SHORT).show();
setTitle("connection");
}

@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
mBoundService =null;
Toast.makeText(LocalServiceBinder.this, "service disconnected",
Toast.LENGTH_SHORT).show();

}

};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button startbutton = (Button) findViewById(R.id.start);
startbutton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
bindService(new Intent(LocalServiceBinder.this,
LocalService.class), mConnection , Context.BIND_AUTO_CREATE);
mIsBound = true;
}

});
Button stopbutton = (Button) findViewById(R.id.stop);
stopbutton.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mIsBound) {
// Detach our existing connection.
unbindService(mConnection);
mIsBound = false;
}
}

});
final TextView tv = (TextView) findViewById(R.id.TextView01);
Button sum= (Button) findViewById(R.id.sum);
sum.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int c = mBoundService.sum(3, 5);
tv.setText(Integer.toString(c));
}

});
}
}
本地服務之程式列表:
package com.example.localservice;


import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;

public class LocalService extends Service {


public class LocalBinder extends Binder {

public LocalService getService() {
// TODO Auto-generated method stub
return LocalService.this;
}
}

private IBinder mBinder = new LocalBinder();

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(this, "service created", Toast.LENGTH_SHORT).show();
// The PendingIntent to launch our activity if the user selects this notification
}
public int sum(int a, int b)
{
return a+b;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "service stoped", Toast.LENGTH_SHORT).show();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
}

@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "service unbinded", Toast.LENGTH_SHORT).show();
return super.onUnbind(intent);
}

@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(this, "service started", Toast.LENGTH_SHORT).show();

}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "service binded", Toast.LENGTH_SHORT).show();
return mBinder;
}

}

沒有留言:

張貼留言