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/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 

     
查看全文
  • 相关阅读:
    LeetCode100-相同的树
    LeetCode66-加一
    LeetCode102-二叉树的层序遍历
    dubbo协议端口
    http错误-413 Request Entity Too Large
    【Jeecg Vue】通过getAction的finally来最大程度避免影响主数据呈现
    图片压缩,用这个就够了
    信息数据安全,日常办公你就要注意这些!
    java笔记:流式编程 数组与List集合互转
    改状态,你会改吗?你真的会改吗?
  • 原文地址:https://www.cnblogs.com/xubuhang/p/4178691.html
  • Copyright © 2011-2022 走看看