我在应用A中有一个IntentService,我试图从应用B开始。运行时我得到
ActivityManager: Unable to start service Intent { cmp=com.xyz/.service.MyIntentService } U=0: not found
这是我应用程序A中的IntentService代码
package com.xyz.service;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
public class MyIntentService extends IntentService {
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.i("MyIntentService", "Intent Started");
}
}
在应用B的MainActivity中,我有以下内容
Intent i = new Intent();
i.setComponent(new ComponentName("com.xyz", "com.xyz.service.MyIntentService"));
startService(i);
我还在app A的AndroidManifest.xml中声明了该服务
<service android:name=".service.MyIntentService" android:enabled="true" android:exported="true"></service>
非常感谢任何帮助。
答案 0 :(得分:1)
在文件A的AndroidManifest.xml中,试试这个:
<service
android:enabled="true"
android:exported="true"
android:name=".service.MyIntentService">
<intent-filter>
<action android:name="com.xyz.START_SERVICE" />
</intent-filter>
</service>
并从B开始:
Intent intent=new Intent();
intent.setComponent(new ComponentName("com.xyz", "com.xyz.service.MyIntentService"));
intent.addAction("com.xyz.START_SERVICE")
startService(intent);
答案 1 :(得分:1)
您应该在A AndroidManifest.xml中声明权限
<permission android:name="com.xyz.permission.PERMISSION_NAME"
android:protectionLevel="signature"/>
<uses-permission android:name="com.xyz.permission.PERMISSION_NAME"/>
<service android:name=".service.MyIntentService"
android:enabled="true"
android:exported="true"
android:permission="com.xyz.permission.PERMISSION_NAME"/>
并在B AndroidManifest.xml中添加用户权限
<uses-permission android:name="com.xyz.permission.PERMISSION_NAME"/>