温故而知新 梳理一下android知识
Service的基本用法
启动和关闭服务有两种方法代码如下:
package servicetest.baoxin.com.myapplication; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btn1).setOnClickListener(this); findViewById(R.id.btn2).setOnClickListener(this); findViewById(R.id.btn3).setOnClickListener(this); findViewById(R.id.btn4).setOnClickListener(this); } ServiceConnection connection = new ServiceConnection(){ @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { MyServiece.MyBind bind = (MyServiece.MyBind) iBinder; bind.download("下载"); } @Override public void onServiceDisconnected(ComponentName componentName) { } }; @Override public void onClick(View view) { switch (view.getId()){ case R.id.btn1: Intent intent = new Intent(this,MyServiece.class); startService(intent); break; case R.id.btn2: Intent intent2 = new Intent(this,MyServiece.class); stopService(intent2); break; case R.id.btn3: Intent intent3 = new Intent(this,MyServiece.class); bindService(intent3,connection, BIND_AUTO_CREATE); break; case R.id.btn4: unbindService(connection); break; default: break; } } }
package servicetest.baoxin.com.myapplication; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.support.annotation.IntDef; import android.support.annotation.Nullable; import android.util.Log; /** * Created by weibinbin on 2017/8/28. */ public class MyServiece extends Service { private static final String TAG = "MyService"; @Override public void onCreate() { super.onCreate(); Log.e(TAG,"...onCreate..."); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e(TAG,"...onStartCommand..."); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.e(TAG,"...onDestroy..."); super.onDestroy(); } @Override public IBinder onBind(Intent intent) { return new MyBind(); } class MyBind extends Binder{ public void download(String p){ Log.e(TAG,"...download..."+p); } } }
使用第一种启动方式,第一次启动走onCreate和onStartCommand,启动后再次点击启动只走,onStartCommand
//第一种service启动方式 Intent intent = new Intent(this,MyServiece.class); startService(intent);
使用第一种关闭方式,根据日志看到走onDestory方法
Intent intent2 = new Intent(this,MyServiece.class); stopService(intent2);
使用第二种启动方式,根据日志可知,走了onCreate和onBind方法,再次点击启动,如果已经启动不做任何方法了
Intent intent2 = new Intent(this,MyServiece.class); stopService(intent2);使用第二种关闭方式,根据日志可知,走了onDestory方法
unbindService(connection);
如果需要和Service通信,使用第二种方法
Servic后台运行和Thread线程容易产生的误会
其实service和Thread没有任何关系,Service也是在主线程跑的,注意:不要把Service也看成一个特殊线程。
Service一个标准用法
@Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e(TAG,"...onStartCommand..."); new Thread(new Runnable() { @Override public void run() { Log.e(TAG,"...执行任务..."); } }); return super.onStartCommand(intent, flags, startId); } class MyBind extends Binder{ public void download(String p){ new Thread(new Runnable() { @Override public void run() { Log.e(TAG,"...执行任务..."); } }); Log.e(TAG,"...download..."+p); } }