1 package com.lixu.jiaohu; 2 3 import com.lixu.jiaohu.MyAppService.Mybind; 4 5 import android.app.Activity; 6 import android.app.Service; 7 import android.content.ComponentName; 8 import android.content.Intent; 9 import android.content.ServiceConnection; 10 import android.os.Bundle; 11 import android.os.IBinder; 12 import android.view.Menu; 13 import android.view.MenuItem; 14 import android.view.View; 15 import android.view.View.OnClickListener; 16 import android.widget.Button; 17 import android.widget.Toast; 18 19 public class MainActivity extends Activity implements OnClickListener { 20 private ServiceConnection sc; 21 private MyAppService mMyAppService; 22 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.activity_main); 27 28 Button btn1 = (Button) findViewById(R.id.button1); 29 Button btn2 = (Button) findViewById(R.id.button2); 30 Button btn3 = (Button) findViewById(R.id.button3); 31 32 btn1.setOnClickListener(this); 33 btn2.setOnClickListener(this); 34 btn3.setOnClickListener(this); 35 // 建立service连接 36 sc = new ServiceConnection() { 37 38 @Override 39 public void onServiceDisconnected(ComponentName name) { 40 41 } 42 43 @Override 44 public void onServiceConnected(ComponentName name, IBinder service) { 45 Mybind mMybind = (Mybind) service; 46 mMyAppService = mMybind.getService();// 获取MyAppService的对象 47 } 48 }; 49 } 50 51 @Override 52 public void onClick(View v) { 53 switch (v.getId()) { 54 case R.id.button1: 55 startService(); 56 break; 57 case R.id.button2: 58 bindService(); 59 break; 60 case R.id.button3: 61 String str = mMyAppService.getServiceValue(); 62 Toast.makeText(this, str, 0).show(); 63 break; 64 default: 65 break; 66 } 67 68 } 69 70 public void startService() { 71 mMyAppService.setServiceValue("你是:");// 设置MyAppService里面的公共变量的值 72 Intent intent = new Intent(this, MyAppService.class); 73 startService(intent); 74 } 75 76 public void bindService() { 77 Intent it = new Intent(this, MyAppService.class); 78 bindService(it, sc, Service.BIND_AUTO_CREATE);// 建立service的bind连接 79 } 80 81 @Override 82 protected void onDestroy() { 83 super.onDestroy(); 84 Intent it = new Intent(this, MyAppService.class); 85 stopService(it);// 关闭服务 86 } 87 88 }
1 package com.lixu.jiaohu; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.Binder; 6 import android.os.IBinder; 7 import android.util.Log; 8 9 public class MyAppService extends Service { 10 private String str; 11 12 @Override 13 public void onCreate() { 14 Log.e("MyAppService", "onCreate开始了"); 15 super.onCreate(); 16 } 17 18 @Override 19 public int onStartCommand(Intent intent, int flags, int startId) { 20 str = str + "宝儿"; 21 return super.onStartCommand(intent, flags, startId); 22 } 23 24 // Activity bindService()方法开启后进入执行onBind方法 25 @Override 26 public IBinder onBind(Intent intent) { 27 28 return new Mybind(); 29 } 30 31 public String getServiceValue() { 32 return str; 33 } 34 35 public void setServiceValue(String str) { 36 this.str = str; 37 } 38 39 public class Mybind extends Binder { 40 public MyAppService getService() { 41 return MyAppService.this; 42 43 } 44 } 45 46 @Override 47 public boolean onUnbind(Intent intent) { 48 // TODO Auto-generated method stub 49 return super.onUnbind(intent); 50 } 51 52 }
时间: 2024-10-14 15:14:03