onStart() 已弃用,由onStartCommand() 代替
@Override public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
设置两个按钮的点击事件:
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
}
再次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
https://www.quora.com/How-do-I-get-started-with-Android-application-development?redirected_qid=4510292