Service 需要在manifest.xml中注册,Service运行于主线程,和Thread没有任何关系,耗时操作需要再开线程
Service生命周期.png
startServicestart 方式开启一个Service
public class TestService extends Service { public static final String TAG = "TestService"; @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy"); } @Override public IBinder onBind(Intent intent) { return null; } }
<service android:name="com.speed.test.TestService"/>
Intent serviceIntent = new Intent(this, TestService.class); startService(serviceIntent); //stopService(serviceIntent); 调用了stop后Service会执行onDestory方法
当启动一个Service的时候,会调用该Service中的onCreate()和onStartCommand()方法,当字词启动同一个Service是不会再次调用onCreate,而是直接调用onStartCommand bindService
private TestBinder mBinder = new TestBinder(); @Override public IBinder onBind(Intent intent) { return mBinder ;} public class TestBinder extends Binder { public void show(){ Log.d("bind service", "show"); } }
private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { } @Override public void onServiceConnected(ComponentName name, IBinder service) { TestService.TestBinder binder = (TestBinder) service; binder.show(); } };
bindService(serviceIntent, connection, BIND_AUTO_CREATE); //unbindService(connection); 解除绑定后Service会执行onDestoty方法 如果既 start 又bind 则需要stop unbind 才能销毁Service IntentService
public class TestIntentService extends IntentService { public TestIntentService() { super("TestIntentService"); } @Override public void onCreate() { super.onCreate(); Log.d("TestIntentService", "onCreate"); } @Override public void onDestroy() { Log.i("TestIntentService", "onDestroy"); super.onDestroy(); } @Override protected void onHandleIntent(Intent intent) { getBaiduData(); } private void getBaiduData() { try { URL imageUrl = new URL("https://www.baidu.com/"); HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection(); conn.setConnectTimeout(10000); conn.setRequestMethod("GET"); if(conn.getResponseCode() == 200) { InputStream is = conn.getInputStream(); Log.d("TestIntentService", readInputStream(is)); } } catch (IOException e) { e.printStackTrace(); } } private String readInputStream(InputStream is) { InputStreamReader isReader = new InputStreamReader(is); BufferedReader bufferReader = new BufferedReader(isReader); String inputLine = null; StringBuffer result = new StringBuffer(); try { while ((inputLine = bufferReader.readLine()) != null) { result.append(inputLine); } } catch (IOException e) { e.printStackTrace(); } return result.toString(); }
<service android:name="com.speed.test.TestIntentService"/>
Intent intServiceIntent = new Intent(this, TestIntentService.class); startService(intServiceIntent); Service 和 Thread 的区别二者没有什么关系,Service是没有界面的后台服务,不能执行耗时操作,在Activity开启的子线程并不会自动随Activity的destroy而关闭。 (责任编辑:好模板) |