如何从 Android 中不在同一项目中的活动启动服务?
这可能是一个非常简单的问题,但由于我是 Android 开发新手...
我尝试使用以下代码从不在同一个包中的活动启动服务(远程服务) :
Intent i = new Intent("com.vasilis.service.GPSService");
i.putExtra("com.vasilis.service.GPSEnable", true);
this.startService(i);
但是这段代码没有任何反应!
服务项目/包的清单...
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vasilis.service" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".serv_activity" 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:name=".GPSService" android:exported="true" android:enabled="true"></service>
<activity android:name=".incomingCallActivity" android:label="@string/app_name"
android:theme="@android:style/Theme.Dialog"></activity>
<receiver android:name="OnBootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
</manifest>
我缺少一些东西!有什么帮助吗?
It's probably a very simple question but since I'm new to android development...
I am trying to start a service from an activity that is not in the same package (remote service) with the following code:
Intent i = new Intent("com.vasilis.service.GPSService");
i.putExtra("com.vasilis.service.GPSEnable", true);
this.startService(i);
but nothing happens with this code!
the manifest of the service project/package...
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vasilis.service" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".serv_activity" 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:name=".GPSService" android:exported="true" android:enabled="true"></service>
<activity android:name=".incomingCallActivity" android:label="@string/app_name"
android:theme="@android:style/Theme.Dialog"></activity>
<receiver android:name="OnBootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
</manifest>
I am missing something! Any help ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里提出了类似的问题: 如何启动在不同包中定义的服务?
几乎您只需将其添加到manifest.xml
或无论类的名称是什么;)如果这似乎不起作用,文档位于 Service 有很多好的建议。
A similar question was asked here: How do I start a service which is defined in a different package?
Pretty much you just need to add this to the manifest.xml
Or whatever the names of the classes are ;) If this doesn't seem to work, the documentation located at Service has a lot of good advice.