如何执行远程服务
我在应用程序“ServiceDemo”中有一个服务 MyService.java。该应用程序的清单看起来像这样
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.moto.dev"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Sample"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true"
android:name=".MyService"
android:permission="syh.permission.STARTMYSERVICE">
</service>
</application>
<permission
android:protectionLevel="normal"
android:label="Start My Receiver"
android:description="@string/startMyActivityDesc"
android:name="syh.permission.STARTMYSERVICE">
</permission>
<uses-sdk android:minSdkVersion="8" />
</manifest>
我有另一个应用程序“CallerService”清单看起来像这样
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.moto.dev"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".CallerService"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="syh.permission.STARTMYSERVICE"></uses-permission>
<uses-sdk android:minSdkVersion="8" />
"src/com/moto/dev/Sample.java"</manifest>
我有一个活动,我试图通过单击按钮来启动服务,
Intent intent = new Intent();
//path where my service is located in application "ServiceDemo"
intent.setClassName("com.moto.dev","com.moto.dev.MyService");
startService(intent);
但这不起作用。说“无法启动服务意图” 我可以知道我哪里出了问题吗?
I have a service MyService.java in Application "ServiceDemo". The manifest of this application looks like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.moto.dev"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Sample"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true"
android:name=".MyService"
android:permission="syh.permission.STARTMYSERVICE">
</service>
</application>
<permission
android:protectionLevel="normal"
android:label="Start My Receiver"
android:description="@string/startMyActivityDesc"
android:name="syh.permission.STARTMYSERVICE">
</permission>
<uses-sdk android:minSdkVersion="8" />
</manifest>
I have another application "CallerService" manifest looks like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.moto.dev"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".CallerService"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="syh.permission.STARTMYSERVICE"></uses-permission>
<uses-sdk android:minSdkVersion="8" />
"src/com/moto/dev/Sample.java"</manifest>
I have an activity from where I m trying to start the service on the click of the button
Intent intent = new Intent();
//path where my service is located in application "ServiceDemo"
intent.setClassName("com.moto.dev","com.moto.dev.MyService");
startService(intent);
This fails to work. Says "unable to start service intent"
May I know where I am going wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
令我惊讶的是,出现了这个 SecurityException: 不允许在没有权限 syh.permission.STARTMYSERVICE 的情况下启动服务意图,而它已被明确授予!
and to my wonder, there comes this SecurityException: Not allowed to start service Intent without permission syh.permission.STARTMYSERVICE while it has been explicitly granted!!
由于您的服务没有 Intent 过滤器,因此您需要在清单中的服务中将
exported
标志设置为true
As your service doesn't have an intent filter, you will need to set
exported
flag totrue
in your service in manifest