安卓11以上版本远程启动服务启动其他应用的服务
一、前言
大家都知道,谷歌爷爷特别喜欢搞事情,越高的版本,对于开发着来说,越麻烦,以前的远程服务启动方式,从安卓11以上的版本开始就没用了。当然并不是完全没用,需要你额外去做一些事情。
首先说一下,提供服务的应用A为服务端,访问服务的应用B为客户端,我需要在客户端启动服务端的Service。修改xml时别搞混了。
二、配置
2.1 服务端应用A的androidmanifest.xml配置如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.servicedemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ServiceDemo">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService"
android:exported="true"
android:enabled="true">
<intent-filter>
<!--指定服务的action-->
<action android:name="com.example.lyyservice"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
</application>
</manifest>
2.2 客户端应用B的androidmanifest.xml文件配置如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aidldemo">
<!--这里是关键-->
<queries>
<package android:name="com.example.servicedemo"/>
<intent>
<action android:name="com.example.lyyservice" />
</intent>
</queries>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AIDLDemo">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
三、客户端应用B的启动逻辑代码
Intent intent = new Intent();
intent.setAction("com.example.lyyservice");//服务端的Service的action名
intent.setPackage("com.example.servicedemo");//服务端的包名
startService(intent);
四、总结
queries这个标签是写在客户端里面的,别搞混了,一开始我就是错误地把它写到服务端里了,导致启动不了。
有些人说服务端应用A还需要在系统设置里改成自启动,并且授予“后台弹出界面”的权限,才能实现远程启动,不过我在小米10上测的,并不需要这两点,大家可以试一试。
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgfahhb
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01