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

}

}

相关推荐
m0_748233644 分钟前
RabbitMQ 进阶
android·前端·后端
Mr.pyZhang4 小时前
Android构建系统 - 04 编译产物
android·linux
守城小轩10 小时前
Brave 132 编译指南 Android 篇 - 编译准备:系统要求与推荐工具 (三)
android·chrome·chrome devtools·指纹浏览器·浏览器开发·brave
m0_7482384210 小时前
MySQL Workbench菜单汉化为中文
android·数据库·mysql
程序员江同学11 小时前
Kotlin 技术月报 | 2025 年 2 月
android·kotlin
甜于酸13 小时前
MuMu模拟器Pro for Mac 安卓手机平板模拟器
android·macos·智能手机
顾林海15 小时前
深入解析 Android Activity 生命周期
android
XuanRanDev17 小时前
【构建工具】Gradle Kotlin DSL中的大小写陷阱:BuildConfigField
android·开发语言·kotlin
Nathan2024061620 小时前
Room - 基本使用及使用问题
android·面试·android jetpack
攻城狮_Dream21 小时前
基于 Python 的项目管理系统开发
android·数据库·python