- 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