Android 无法启动服务意图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3439356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to start Service Intent
提问by Vinay
I have a service class. I have exported this class to jar and I have embed the jar in my client app.
我有一个服务类。我已将此类导出到 jar 并将该 jar 嵌入到我的客户端应用程序中。
When needed, I call the service class. When I try to do this, I get the following error:
需要时,我会调用服务类。当我尝试这样做时,我收到以下错误:
Unable to start service Intent {comp={com.sample.service/com.sample.service.serviceClass}} : not found
I have other class apart from the service class, which I am able to access (create object of that class) which are inside the same jar.
除了服务类之外,我还有其他类,我可以访问(创建该类的对象),它们位于同一个 jar 中。
I feel I have missed out some thing in my configuration or manifest or so.
我觉得我在配置或清单中遗漏了一些东西。
Please help me identifying the same. My code is below:
请帮我识别相同的。我的代码如下:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent () ;
intent.setClassName("com.sample.service" ,"com.sample.service.serviceClass") ;
this.startService(intent) ; // when I call this line I get the message...
// binding other process continue here
}
Client manifest.xml
客户端清单.xml
<service android:name="com.sample.service.serviceClass"
android:exported="true" android:label="@string/app_name"
android:process=":remote">
<intent-filter><action android:name="com.sample.service.serviceClass"></action>
</intent-filter>
</service>
Thanks in advance,
Vinay
提前致谢,
维奈
采纳答案by CommonsWare
First, you do not need android:process=":remote"
, so please remove it, since all it will do is take up extra RAM for no benefit.
首先,您不需要android:process=":remote"
,所以请删除它,因为它只会占用额外的 RAM 而没有任何好处。
Second, since the <service>
element contains an action string, use it:
其次,由于<service>
元素包含一个动作字符串,请使用它:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=new Intent("com.sample.service.serviceClass");
this.startService(intent);
}
回答by Blundell
For anyone else coming across this thread I had this issue and was pulling my hair out. I had the service declaration OUTSIDE of the '< application>' end tag DUH!
对于遇到此线程的其他人,我遇到了这个问题并且正在拔头发。 我有“<应用程序>”结束标签的服务声明 DUH!
RIGHT:
对:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...>
...
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity ...>
...
</activity>
<service android:name=".Service"/>
<receiver android:name=".Receiver">
<intent-filter>
...
</intent-filter>
</receiver>
</application>
<uses-permission android:name="..." />
WRONG but still compiles without errors:
错误但仍然编译没有错误:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...>
...
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity ...>
...
</activity>
</application>
<service android:name=".Service"/>
<receiver android:name=".Receiver">
<intent-filter>
...
</intent-filter>
</receiver>
<uses-permission android:name="..." />
回答by Jianhong
1) check if service declaration in manifest is nested in application tag
1) 检查清单中的服务声明是否嵌套在应用程序标签中
<application>
<service android:name="" />
</application>
2) check if your service.java
is in the same package or diff package as the activity
2)检查您service.java
是否与活动在同一包或差异包中
<application>
<!-- service.java exists in diff package -->
<service android:name="com.package.helper.service" />
</application>
<application>
<!-- service.java exists in same package -->
<service android:name=".service" />
</application>
回答by sataniccrow
I hope I can help someone with this info as well: I moved my service class into another package and I fixed the references. The project was perfectly fine, BUT the service class could not be found by the activity.
我希望我也可以帮助提供此信息的人:我将我的服务类移到另一个包中并修复了引用。该项目非常好,但活动找不到服务类。
By watching the log in logcat I noticed the warning about the issue: the activity could not find the service class, but the funny thing was that the package was incorrect, it contained a "/" char. The compiler was looking for
通过观察 logcat 中的登录,我注意到有关该问题的警告:活动找不到服务类,但有趣的是包不正确,它包含一个“/”字符。编译器正在寻找
com.something./service.MyService
com.something./service.MyService
instead of
代替
com.something.service.MyService
com.something.service.MyService
I moved the service class out from the package and back in and everything worked just fine.
我将服务类从包中移出并移回,一切正常。
回答by marisxanis
I've found the same problem. I lost almost a day trying to start a service from OnClickListener
method - outside the onCreate
and after 1 day, I still failed!!!! Very frustrating!
I was looking at the sample example RemoteServiceController
. Theirs works, but my implementation does not work!
我发现了同样的问题。我花了将近一天的时间尝试从OnClickListener
方法启动服务-onCreate
在 1 天之后,我仍然失败了!!!!非常令人沮丧!我正在查看示例示例RemoteServiceController
。他们的工作,但我的实施不起作用!
The only way that was working for me, was from inside onCreate
method. None of the other variants worked and believe me I've tried them all.
对我有用的唯一方法是从内部onCreate
方法。其他变体都没有工作,相信我,我已经尝试过所有这些变体。
Conclusion:
结论:
- If you put your service class in different package than the mainActivity, I'll get all kind of errors
Also the one "/" couldn't find path to the service, tried starting with
Intent(package,className)
and nothing , also other type of Intent startingI moved the service class in the same package of the activity Final form that works
Hopefully this helps someone by defining the listerners
onClick
inside theonCreate
method like this:public void onCreate() { //some code...... Button btnStartSrv = (Button)findViewById(R.id.btnStartService); Button btnStopSrv = (Button)findViewById(R.id.btnStopService); btnStartSrv.setOnClickListener(new OnClickListener() { public void onClick(View v) { startService(new Intent("RM_SRV_AIDL")); } }); btnStopSrv.setOnClickListener(new OnClickListener() { public void onClick(View v) { stopService(new Intent("RM_SRV_AIDL")); } }); } // end onCreate
- 如果您将服务类放在与 mainActivity 不同的包中,我会收到各种错误
还有一个“/”找不到服务的路径,尝试从
Intent(package,className)
什么都没有开始,还有其他类型的 Intent 开始我将服务类移动到有效的活动最终表单的同一个包中
希望这可以通过
onClick
在onCreate
方法中定义侦听器来帮助某人,如下所示:public void onCreate() { //some code...... Button btnStartSrv = (Button)findViewById(R.id.btnStartService); Button btnStopSrv = (Button)findViewById(R.id.btnStopService); btnStartSrv.setOnClickListener(new OnClickListener() { public void onClick(View v) { startService(new Intent("RM_SRV_AIDL")); } }); btnStopSrv.setOnClickListener(new OnClickListener() { public void onClick(View v) { stopService(new Intent("RM_SRV_AIDL")); } }); } // end onCreate
Also very important for the Manifest file, be sure that service is child of application:
对于 Manifest 文件也非常重要,请确保服务是应用程序的子项:
<application ... >
<activity ... >
...
</activity>
<service
android:name="com.mainActivity.MyRemoteGPSService"
android:label="GPSService"
android:process=":remote">
<intent-filter>
<action android:name="RM_SRV_AIDL" />
</intent-filter>
</service>
</application>
回答by ranbuch
In my case the 1 MB maximum cap for data transport by Intent. I'll just use Cache or Storage.
在我的情况下,Intent 数据传输的最大上限为 1 MB。我只会使用缓存或存储。