Day la file Activity cua chung ta:
package example.bom.search;
import com.example.test.R;
import example.bom.search.ReceiveNofi.LocalBinder1;
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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class PlayMediaActivity extends Activity implements OnClickListener{
public boolean mBound = true;
ReceiveNofi myService;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.example_bom_search_playmedia);
tv = (TextView)findViewById(R.id.tv);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(this);
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if(mBound){
myService.unbindService(conn);
mBound = false;
}
}
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
mBound = false;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
LocalBinder1 local = (LocalBinder1) service;
myService = local.getReceiveNofi();
mBound = true;
}
};
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.button1){
if(mBound){
Intent i = new Intent(this,ReceiveNofi.class);
bindService(i,conn,Context.BIND_AUTO_CREATE );
int result = myService.sum(5,5);
tv.setText(String.valueOf(result));
}
}
}
}
Tiep den la giao dien:
<?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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take photo" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Day la text"/>
</LinearLayout>
Tiep den la Service:
package example.bom.search;
import com.example.test.R;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
public class ReceiveNofi extends Service{
private final IBinder ibinder = new LocalBinder1();
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return ibinder;
}
public int sum(int a,int b){
return a+b;
}
public class LocalBinder1 extends Binder{
ReceiveNofi getReceiveNofi(){
return ReceiveNofi.this;
}
}
}
Them vao AndroidManifest.xml:
<service android:name="example.bom.search.ReceiveNofi"></service>