安卓 service
public class MyService extends Service { public MyService() { } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. //throw new UnsupportedOperationException("Not yet implemented"); return new Binder(); //这里必须要返回一个Binder实例 } // 服务启动 @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); } // 服务销毁 @Override public void onDestroy() { super.onDestroy(); } // 服务开始的时候 执行的方法 @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.print("服务正在执行....."); new Thread() { @Override public void run() { super.run(); while (true) { System.out.print("服务正在执行...."); try { sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); return super.onStartCommand(intent, flags, startId); } }
public class MainActivity extends AppCompatActivity implements ServiceConnection { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); // 创建视图 //setContentView(R.layout.my_layout); this.setContentView(R.layout.my_layout); // 启动服务 findViewById(R.id.btnStartServcie).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { System.out.print("服务开始执行......"); Intent i = new Intent(MainActivity.this, MyService.class); startService(i); } }); // 停止服务 this.findViewById(R.id.btnStopServcie).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this, MyService.class); stopService(i); } }); //btnBindServcie 绑定service this.findViewById(R.id.btnBindServcie).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this, MyService.class); // 这里的MainActivity必须要实现ServiceConnection 重写onServiceConnected 和 onServiceDisconnected 方法 bindService(i, MainActivity.this, Context.BIND_AUTO_CREATE); } }); //btnBindServcie 解除绑定service this.findViewById(R.id.btnUnBindServcie).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this, MyService.class); unbindService(MainActivity.this); } }); System.out.println("onCreate"); } @Override protected void onStart() { super.onStart(); System.out.println("onStart"); } @Override protected void onResume() { super.onResume(); System.out.println("onResume"); } @Override protected void onPause() { super.onPause(); System.out.println("onPause"); } @Override protected void onStop() { super.onStop(); System.out.println("onStop"); } @Override protected void onDestroy() { super.onDestroy(); System.out.println("onDestroy"); } @Override protected void onRestart() { super.onRestart(); System.out.println("onRestart"); } // 服务被绑定成功后被执行 @Override public void onServiceConnected(ComponentName name, IBinder service) { System.out.print("service connected"); } // 服务被杀掉后执行 @Override public void onServiceDisconnected(ComponentName name) { System.out.print("service DisConnected"); } }
这块不好理解,这个只能在以后的项目里面好好理解了。