zoukankan      html  css  js  c++  java
  • Android Service学习

    还有一个IntentService!

    AndroidManifest.xml

    注册service

    <service  
        android:name=".LocalService"  
        android:enabled="true" >  
        <intent-filter>  
            <action android:name="com.test.service" />  
        </intent-filter>  
    </service>  

    Activity中启动服务(注:LocalService.java继承于Service)

    方法1

    //启动服务
             Intent intent=new Intent(this,LocalService.class);
             startService(intent);

    方法2

    LocalService localService=null;
        //用bindService方法启动服务
        private void BinderService(){
             Intent intent=new Intent(this,LocalService.class);
             bindService(intent, new ServiceConnection(){
                @Override
                public void onServiceConnected(ComponentName componentName, IBinder binder) {
                    //调用bindService方法启动服务时候,如果服务需要与activity交互,
                    //则通过onBind方法返回IBinder并返回当前本地服务
                    localService=((LocalService.LocalBinder)binder).getService();
                    //这里可以提示用户,或者调用服务的某些方法
                }
                @Override
                public void onServiceDisconnected(ComponentName componentName) {
                    localService=null;
                    //这里可以提示用户
                }     
             }, Context.BIND_AUTO_CREATE);
        }

     LocalService.java

                              作者:xubuhang                出处:http://www.cnblogs.com/xubuhang/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 

     
查看全文
  • 相关阅读:
    盒模型(框模型)
    边框
    尺寸及溢出处理
    HTML标签分类
    尺寸单位和颜色的取值
    选择器的优先级
    C++ 代码模板
    LC 425. Word Squares 【lock,hard】
    LC 660. Remove 9 【lock, hard】
    LC 759. Employee Free Time 【lock, hard】
  • 原文地址:https://www.cnblogs.com/xubuhang/p/4178691.html
  • Copyright © 2011-2022 走看看