Android Service服务使用方法

启动服务的方法

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

java 复制代码
        <service android:name=".MyAsdlService">
            <intent-filter>
                <action android:name="com.example.myasdlservice" />
            </intent-filter>
        </service>

然后在Activity中启动service

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

跨应用启动服务

java 复制代码
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);

具体例子如下:

java 复制代码
    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却没有相关配置。因此当程序启动时服务是找不到的,通过配置以下属性解决问题:

java 复制代码
    <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就会出现这个异常。

java 复制代码
<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) {

}

}

相关推荐
珹洺14 小时前
Java-Spring入门指南(二十五)Android 的历史,认识移动应用和Android 基础知识
android·java·spring
大白的编程日记.15 小时前
【MySQL】数据库表的CURD(二)
android·数据库·mysql
介一安全15 小时前
【Frida Android】基础篇4:Java层Hook基础——调用静态方法
android·网络安全·逆向·安全性测试·frida
怪兽201415 小时前
主线程 MainLooper 和一般 Looper 的异同?
android·面试
洋不写bug16 小时前
数据库的创建,查看,修改,删除,字符集编码和校验操作
android·数据库·adb
2501_9159090617 小时前
iOS App 上架全流程详解:证书配置、打包上传、审核技巧与跨平台上架工具 开心上架 实践
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063217 小时前
iOS 26 系统流畅度测试实战分享,多工具组合辅助策略
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_9159184117 小时前
开发 iOS 应用全流程指南,环境搭建、证书配置与跨平台使用 开心上架 上架AppStore
android·ios·小程序·https·uni-app·iphone·webview
灵芸小骏17 小时前
Rokid应用实践:基于CXR-M与CXR-S SDK,打造眼镜与手机协同的‘智能随行记录仪’
android
奔跑中的蜗牛66617 小时前
直播 QoE 监控体系设计与落地(三):原生卡顿优化实践
android