package com.example.test5;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
private static String ACTION = "com.example.test5.action.MY_SERVICE";
private ServiceConnection scon = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
Log.i("bind成功","bind成功");
Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
Log.i("断开链接","断开连接");
Toast.makeText(MainActivity.this, "断开连接", Toast.LENGTH_SHORT).show();
}
};
private View.OnClickListener startListener = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(ACTION);
MainActivity.this.startService(intent);
}
};
private View.OnClickListener stopListener = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(ACTION);
MainActivity.this.stopService(intent);
}
};
private View.OnClickListener bindListener = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(ACTION);
MainActivity.this.bindService(intent, scon, Service.BIND_AUTO_CREATE);
}
};
private View.OnClickListener unbindListener = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
MainActivity.this.unbindService(scon);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button)findViewById(R.id.button1);
Button btn2 = (Button)findViewById(R.id.button2);
Button btn3 = (Button)findViewById(R.id.button3);
Button btn4 = (Button)findViewById(R.id.button4);
btn1.setOnClickListener(startListener);
btn2.setOnClickListener(stopListener);
btn3.setOnClickListener(bindListener);
btn4.setOnClickListener(unbindListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
=============================================================
package com.example.test5;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Log.i("onBind","onBind");
Toast.makeText(this, "OnBind", Toast.LENGTH_SHORT).show();
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i("onCreate","onCreate");
Toast.makeText(this, "OnCreate", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("onDestroy","onDestroy");
Toast.makeText(this, "OnDestroy", Toast.LENGTH_SHORT).show();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.i("onStart","onStart");
Toast.makeText(this, "OnStart", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Log.i("onUnBind","onUnBind");
Toast.makeText(this, "OnUnBind", Toast.LENGTH_SHORT).show();
return super.onUnbind(intent);
}
}