Android Service服务使用方法

启动服务的方法

我们要隐式启动一个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) {
        }
    }    

  • 16
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薛文旺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值