Android 应用界面开发笔记 Service

Service

- 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);

}


-新建一个MusicServiceActivity,里面包含Start和Stop两个按钮
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 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值