service(服务)
一、服务是android中实现程序后台运行的解决方案,它非常适合去执行那些不需要和用户交互而且还要求长期运行的任务。
二、service常见的方法:onBind()、onCreate()、onStartCommand()和onDestroy()。onCreate()方法是在服务第一次创建的时候调用的,而onStartCommand()方法则在每次启动服务的时候都会调用。onDestroy()方法会在服务销毁的时候调用。通常情况下,如果我们希望服务一旦启动久立刻去执行某个动作,就可以将逻辑写在onStartCommand()方法里。
三、如果需要活动和服务进行通信,则需要onBind()方法。(以下实现通信的实例)
xml布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/start_service" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启服务"/> <Button android:id="@+id/stop_service" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关闭服务"/> <Button android:id="@+id/bind_service" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绑定服务"/> <Button android:id="@+id/unbind_service" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="解除服务"/> <Button android:id="@+id/get_service" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取服务"/> </LinearLayout>
服务端代码:
public class MyService extends Service { private static final String TAG = "MyService"; private Timer timer; private TimerTask timerTask; private int index; //得到binder对象 MyBinder binder = new MyBinder(); //自定义MyBinder继承Binder实现通信 class MyBinder extends Binder{ //获得当前service的状态 public MyService getService(){ return MyService.this; } } //与activity进行绑定 @Override public IBinder onBind(Intent intent) { return binder; } @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { startTimer(); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); stopTimer(); } public void startTimer(){ timer = new Timer(); timerTask = new TimerTask() { @Override public void run() { index++; Log.d(TAG, "run: "+index); } }; timer.schedule(timerTask,1000,1000); } public void stopTimer(){ timer.cancel(); } public int getIndex(){ return index; } }
活动端代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button startService; private Button stopService; private Button bindService; private Button unbindService; private Button getService; private Intent i; private MyService.MyBinder mBinder; private MyService my; private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { mBinder = (MyService.MyBinder) iBinder; my = mBinder.getService(); } @Override public void onServiceDisconnected(ComponentName componentName) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService = (Button) findViewById(R.id.start_service); stopService = (Button) findViewById(R.id.stop_service); bindService = (Button) findViewById(R.id.bind_service); unbindService = (Button) findViewById(R.id.unbind_service); getService = (Button) findViewById(R.id.get_service); startService.setOnClickListener(this); stopService.setOnClickListener(this); bindService.setOnClickListener(this); unbindService.setOnClickListener(this); getService.setOnClickListener(this); i = new Intent(MainActivity.this,MyService.class); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.start_service: startService(i); break; case R.id.stop_service: stopService(i); break; case R.id.bind_service: bindService(i,mConnection,BIND_AUTO_CREATE); break; case R.id.unbind_service: unbindService(mConnection); break; case R.id.get_service: int data = my.getIndex(); Toast.makeText(MainActivity.this, "当前时间:"+data,Toast.LENGTH_SHORT).show(); break; } } }
                    
                
                
            
        
浙公网安备 33010602011771号