- A service is a application component that can perform long-run operations in the background and does not provide a user interface.
- A service is not a separate process, nor a thread
2 forms: start; bind
* Service not newed as a service file needs to be declared in Manifest
service较之activity有更高优先级,并且可以在后台运行,不需要界面
-新建Service 可以new a service (系统会自动在manifest里注册,enabled, exported included), 自己new一个service class需要手动在manifest里注册
onStart() 已弃用,由onStartCommand() 代替
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
create an activity, implements OnClickListener()
在onClick()
设置两个按钮的点击事件:
public class MusicServiceActivity extends Activity implements View.OnClickListener{
private Button mStartButton;
private Button mStopButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music_service_activity);
mStartButton = (Button)findViewById(R.id.start);
mStopButton = (Button)findViewById(R.id.stop);
mStartButton.setOnClickListener(this);
mStartButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.start:
break;
case R.id.stop:
break;
}
}
}
自带startService() 和 stopService() 功能 @Override
public void onClick(View v) {
switch (v.getId()){
case R.id.start:
startService(new Intent(MusicServiceActivity.this,MusicService.class));
break;
case R.id.stop:
stopService(new Intent(MusicServiceActivity.this,MusicService.class));
break;
}
}
Run the project, "start" "stop" buttons clicked, no response
- go to service file, set TAG
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand"); //Q: 这句LOG的位置如果放在下面这句后面会提示"unreachable statement"
return super.onStartCommand(intent, flags, startId);
}
再次run之后运行停止,发现问题,activity未注册
03-21 07:23:52.494 396-396/naomi.edna.weekfourservice E/AndroidRuntime: android.content.ActivityNotFoundException: Unable to find explicit activity class {naomi.edna.weekfourservice/naomi.edna.weekfourservice.MusicServiceActivity}; have you declared this activity in your AndroidManifest.xml?
------26th March update
to change the font of the codes, -file-settings-editor-colors & fonts - font - click "save as", then create your own preference
* onCreate()方法里面含两个参数--TBD
在service下面放进去音乐之后,start() 音乐,.mp3 未关联,文件的关联可以通过file-settings-editor-file type来修改
--后来才知道,其实音乐文件关联,文件名全改成小写就可以解决问题
--manifest如果重复注册,会error
kill adb.exe 快捷键 ctrl + shift + A
Service life cycle:
startService() - onCreate() - onStartCommand() - ServiceRunning - onDestroy() [unbounded service]
bindService() - onCreate() - onBind() - Clients are bound to Service - onUnbind() - onDestroy() [bounded]
bind/unbind - to replace start n stop
helps to interact/communicate with service - activity
->iBinder, create an inner class
->lifecycle - a bound service can only be destroyed before unbind.
---------------补充阅读---------------
// processes vs. threads
http://developer.android.com/guide/components/processes-and-threads.html
// Thread
http://developer.android.com/reference/java/lang/Thread.html
// App Manifest intro:
http://developer.android.com/guide/topics/manifest/manifest-intro.html
//found some general answers on Quora, asking about how to become an Android developer from scratch, resources provided, could be useful:
https://www.quora.com/How-do-I-get-started-with-Android-application-development?redirected_qid=4510292