@linux1s1s
2019-02-14T07:26:03.000000Z
字数 4307
阅读 3344
AndroidComponent 2015-05
啥也不说直接上代码
public class AppService extends Service{private static final String TAG = "ExampleService";private final AppBinder mAppBinder = new AppService.AppBinder();@Overridepublic IBinder onBind(Intent intent){return mAppBinder;}@Overridepublic void onCreate(){Log.i(TAG, "ExampleService.onCreate()");super.onCreate();}@Overridepublic void onStart(Intent intent, int startId){Log.i(TAG, "ExampleService.onStart()");super.onStart(intent, startId);}@Overridepublic int onStartCommand(Intent intent, int flags, int startId){Log.i(TAG, "ExampleService.onStartCommand()");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy(){Log.i(TAG, "ExampleService.onDestroy()");super.onDestroy();}public class AppBinder extends Binder{AppService getService(){return AppService.this;}}}
上面代码片段是一个Service,在生命周期位置标记了一下,既然Service已经生成好了,下面看一下如何使用
public class MainActivity extends ActionBarActivity{private BroadcastReceiver mReceive;private final static String RECEIVE_KEY = "MY_RECEIVE_KEY";private final Intent mServiceIntent = new Intent(MainActivity.this, AppService.class);@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initContentView();mReceive = new MyReceive(mServiceIntent);IntentFilter filter = new IntentFilter();filter.addAction(RECEIVE_KEY);registerReceiver(mReceive, filter);}private static class MyReceive extends BroadcastReceiver{private Intent it;MyReceive(Intent it){this.it = it;}@Overridepublic void onReceive(Context context, Intent intent){if (intent.getAction().equalsIgnoreCase(RECEIVE_KEY)){context.startService(it);}}}@Overrideprotected void onDestroy(){super.onDestroy();if (mReceive != null){try{unregisterReceiver(mReceive);}catch (Exception e){e.printStackTrace();}mReceive = null;}stopService(mServiceIntent);}private void initContentView(){final View textView = findViewById(R.id.main_activity_text_view);if (textView != null){textView.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){Intent itReceive = new Intent(RECEIVE_KEY);sendBroadcast(itReceive);}});}}}
public class MainActivity extends ActionBarActivity{private static AppServiceConnection SC = new AppServiceConnection();private static class AppServiceConnection implements ServiceConnection{@Overridepublic void onServiceConnected(ComponentName name, IBinder service){AppService s = ((AppService.AppBinder) service).getService();//TODO}@Overridepublic void onServiceDisconnected(ComponentName name){//TO show a message to custom;}}private static class MyReceive extends BroadcastReceiver{private Intent it;MyReceive(Intent it){this.it = it;}@Overridepublic void onReceive(Context context, Intent intent){if (intent.getAction().equalsIgnoreCase(RECEIVE_KEY)){context.bindService(it, SC, Context.BIND_AUTO_CREATE);}}}@Overrideprotected void onDestroy(){super.onDestroy();if (SC != null){unbindService(SC);SC = null;}}}
<serviceandroid:name="studio.neulion.com.tasktest.AppService" />

如果我们使用第一种方式startService,而在Activity的时候没有主动stopService,当我们退出App的时候会怎样?
Service还在运行
然后回到我们的Activity,再次点击启动Service,控制台输出日志如下:

onCreate()方法没有被调用,说明它并没有重新被创建。
然后我们修改一下代码,在onDestory方法中添加stopService,再退出后输出日志如下:

startService()与bindService()区别:
另外说一下:开发人员需要在应用程序配置文件中声明全部的service,使用标签。
多说一句,对于Android的四大组件:
四大基本组件都需要注册才能使用,每个Activity、service、Content Provider都需要在AndroidManifest文件中进行配置。AndroidManifest文件中未进行声明的activity、服务以及内容提供者将不为系统所见,从而也就不可用。而broadcast receiver广播接收者的注册分静态注册(在AndroidManifest文件中进行配置)和通过代码动态创建并以调用Context.registerReceiver()的方式注册至系统。需要注意的是在AndroidManifest文件中进行配置的广播接收者会随系统的启动而一直处于活跃状态,只要接收到感兴趣的广播就会触发(即使程序未运行)。
最后说一下bindService需要的参数flag含义:
