如何执行远程服务

发布于 2024-09-11 22:46:19 字数 2493 浏览 2 评论 0原文

我在应用程序“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梓梦 2024-09-18 22:46:20

令我惊讶的是,出现了这个 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!!

小情绪 2024-09-18 22:46:20

由于您的服务没有 Intent 过滤器,因此您需要在清单中的服务中将 exported 标志设置为 true

As your service doesn't have an intent filter, you will need to set exported flag to true in your service in manifest

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文