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

这行不通。说“无法启动服务意图”我可以知道我哪里出错了。

4

2 回答 2

0

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

于 2011-01-12T03:21:56.160 回答
0

令我惊讶的是,出现了这个 SecurityException: Not allowed to start service Intent without permission syh.permission.STARTMYSERVICE虽然它已被明确授予!

于 2010-07-28T12:16:07.330 回答