首页 > 其他分享 >Android Service服务使用方法

Android Service服务使用方法

时间:2024-09-13 12:53:31浏览次数:3  
标签:服务 startIntent Service ComponentName xxx Intent new Android com

启动服务的方法

我们要隐式启动一个Service,首先我们需要配置AndroidMainfest.xml

        <service android:name=".MyAsdlService">
            <intent-filter>
                <action android:name="com.example.myasdlservice" />
            </intent-filter>
        </service>

然后在Activity中启动service

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        intent.setAction("com.example.myasdlservice");
        startService(intent);
    }

跨应用启动服务

Intent intent = new Intent();  
intent.setAction("ccom.example.myasdlservice");  
//两种方式设置
//第一种,直接设置包名
intent.setPackage("这里输入包名");  

//第二种
ComponentName mComponentName = new ComponentName("包名", "类名");
intent.setComponent(mComponentName);


//启动Service
startService(intent);  
// context.startServiceAsUser(startIntent, UserHandle.SYSTEM);
//context.startServiceAsUser(startIntent, UserHandle.CURRENT);

具体例子如下:

    Intent startIntent = new Intent("com.xxx.action.myService");
    ComponentName mComponentName = new ComponentName("com.xxx.myApp", "com.xxx.myApp.myService");
    startIntent.setComponent(mComponentName);
    context.startServiceAsUser(startIntent,	UserHandle.CURRENT);
    //context.bindServiceAsUser(startIntent, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT, UserHandle.CURRENT);
    private class myServiceConnection implements ServiceConnection {
        public void onServiceConnected(ComponentName componentName, IBinder service) {
        }
        public void onServiceDisconnected(ComponentName componentName) {
        }
    }    

 

android启动服务失败

提示:Unable to start service Intent { cmp=xxx/.xxx} U=0: not found

最终发现清单文件里application配置了directBootAware属性,意思是允许程序在系统未启动完成时启动(解锁阶段),但是TestService却没有相关配置。因此当程序启动时服务是找不到的,通过配置以下属性解决问题:

    <application
        android:name=".MyApplication"

        android:directBootAware="true"
        android:supportsRtl="true">

         <service android:name=".TestService"
            android:directBootAware="true"
            android:enabled="true"/>
  • android:directBootAware:是否允许系统解锁设备之前运行服务,默认false
  • android:enabled:系统是否可实例化service,默认true

另外出现的一个异常:

java.lang.IllegalStateException: SharedPreferences in credential encrypted storage are not available until after user is unlocked

这个错误会导致程序崩溃,原因设备未解锁前不可读取SharedPreferences数据。在配置了directBootAware属性后,在程序启动的时候、系统没准备好前去操作sp就会出现这个异常。

<application
        android:defaultToDeviceProtectedStorage="true"
        。。。。。/>

 

     Intent startIntent = new Intent("com.xxx.action.myService");
    ComponentName mComponentName = new ComponentName("com.xxx.myApp", "com.xxx.myApp.myService");
    startIntent.setComponent(mComponentName);
    context.startServiceAsUser(startIntent,    UserHandle.CURRENT);
    //context.bindServiceAsUser(startIntent, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT, UserHandle.CURRENT);
    private class myServiceConnection implements ServiceConnection {
        public void onServiceConnected(ComponentName componentName, IBinder service) {
        }
        public void onServiceDisconnected(ComponentName componentName) {
        }
    }    

    Intent startIntent = new Intent("com.xxx.action.myService");
    ComponentName mComponentName = new ComponentName("com.xxx.myApp", "com.xxx.myApp.myService");
    startIntent.setComponent(mComponentName);
    context.startServiceAsUser(startIntent,    UserHandle.CURRENT);
    //context.bindServiceAsUser(startIntent, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT, UserHandle.CURRENT);
    private class myServiceConnection implements ServiceConnection {
        public void onServiceConnected(ComponentName componentName, IBinder service) {
        }
        public void onServiceDisconnected(ComponentName componentName) {
        }
    }    

标签:服务,startIntent,Service,ComponentName,xxx,Intent,new,Android,com
From: https://blog.csdn.net/wenwang88/article/details/142066545

相关文章

  • Android生成C++ AIDL
    生成C++[Android]接口cpp和ndk的区别cpp:生成的代码是为了在Android源码中编译,代码中会调用Android源码中的native接口。例如,引用的头文件:,,,ndk:生成的代码是为了使用ndk独立编译,调用的是ndk的接口,例如,引用的头文件:–lang=cpp,参数指定生成Android源码下编译的C++接口文件......
  • Spring Cloud全解析:服务调用之Feign简介
    Feign简介Feign是Netflix开发的一个声明式的HTTP客户端(远程服务调用组件),只需要接口+注解即可完成对于微服务的调用,不需要使用RestTemplate+Ribbon来对微服务进行访问,简化了编程,其是基于动态代理机制,目标是减少HTTP调用的复杂性依赖<!--feign--><dependency><groupId>......
  • 高德地图SDK Android版开发 11 覆盖物示例 4 线
    高德地图SDKAndroid版开发11覆盖物示例4线前言界面布局MapPolyline类常量成员变量初始值创建覆盖物移除覆盖物设置属性加载地图和释放地图MapPolylineActivity类控件响应事件运行效果图前言文本通过创建多个不同线宽的折线和大地曲线,介绍Polyline的使用方法。......
  • Sulfo-Cy3-Mal;磺基-Cy3-马来酰亚胺; 标记服务:多肽、蛋白、抗体、小分子
         Sulfo-Cy3-Mal,也称为磺化Cy3马来酰亚胺,是一种水溶性荧光染料,其分子中含有马来酰亚胺(maleimide)官能团,这使得它能够与含有巯基(-SH)的生物分子通过亲和加成反应形成稳定的共价键。这种标记方法具有高度的选择性和特异性,常用于蛋白质、抗体、寡核苷酸等分子的标记。......
  • UDS 诊断 - RequestUpload(请求上传)(0x35)服务
    UDS诊断服务系列文章目录诊断和通信管理功能单元UDS诊断-DiagnosticSessionControl(诊断会话控制)(0x10)服务UDS诊断-ECUReset(ECU重置)(0x11)服务UDS诊断-SecurityAccess(安全访问)(0x27)服务UDS诊断-CommunicationControl(通信控制)(0x28)服务UDS诊断-TesterPresent......
  • UDS 诊断 - TransferData(传输数据)(0x36)服务
    UDS诊断服务系列文章目录诊断和通信管理功能单元UDS诊断-DiagnosticSessionControl(诊断会话控制)(0x10)服务UDS诊断-ECUReset(ECU重置)(0x11)服务UDS诊断-SecurityAccess(安全访问)(0x27)服务UDS诊断-CommunicationControl(通信控制)(0x28)服务UDS诊断-TesterPresent......
  • 解决Android Studio项目加载过慢问题
    解决方案替换掉谷歌原地址:(1)官网地址:https://services.gradle.org/distributions/(2)腾讯镜像Gradle下载地址:https://mirrors.cloud.tencent.com/gradle/(3)阿里云镜像Gradle下载地址:https://mirrors.aliyun.com/macports/distfiles/gradle/把官方下载地址替换成腾讯或者阿里云的......
  • 一个Android App最少有几个线程?实现多线程的方式有哪些?
    本文首发于公众号“AntDream”,欢迎微信搜索“AntDream”或扫描文章底部二维码关注,和我一起每天进步一点点多线程编程是Android应用开发中非常重要的一个环节,可以有效地提升应用的性能和用户体验。下面是对Android中多线程相关内容的详细介绍,包括app最少有几个线程。1.基本概念主线......
  • 【Java】Ruoyi(若依)——6.微服务版项目启动
    http://doc.ruoyi.vip/ruoyi-cloud/document/hjbs.html#%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C最早的时候,并没有打算写ruoyi框架的微服务版的安装和部署,原因如下:1.当时的项目中并没有用到微服务版。2.虽然微服务很有名,也是未来的发展趋势。但是我对微服务了解知之甚少,学起来......
  • centos7搭建DNS服务和DNS辅助服务
    主服务器准备二台linux虚拟机(一个服务器主服务器,一个为辅助服务器)[root@cs01~]#yuminstallbind-y#安装dns服务[root@cs01~]#systemctlstopfirewalld.service#关闭防火墙[root@cs01~]#setenforce0#设置宽容模式 [root@cs01~]#ls/etc/|grepnamed #查看DN......