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

}

}

相关推荐
灿烂阳光g2 小时前
domain_auto_trans,source_domain,untrusted_app
android·linux
低调小一3 小时前
Android传统开发 vs Android Compose vs HarmonyOS ArkUI 对照表
android·华为·harmonyos
雨白3 小时前
Java 多线程指南:从基础用法到线程安全
android·java
00后程序员张4 小时前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
程序员江同学6 小时前
ovCompose + AI 开发跨三端的 Now in Kotlin App
android·kotlin·harmonyos
2501_915106326 小时前
Xcode 上传 ipa 全流程详解 App Store 上架流程、uni-app 生成 ipa 文件上传与审核指南
android·macos·ios·小程序·uni-app·iphone·xcode
消失的旧时光-19436 小时前
Kotlinx.serialization 使用讲解
android·数据结构·android jetpack
灿烂阳光g7 小时前
SELinux 策略文件编写
android·linux
.豆鲨包7 小时前
【Android】Viewpager2实现无限轮播图
android·java