我正在开发一个需要启动后台服务的 AIR 本机扩展。
我尝试了不同的启动服务的方法。
这是我的 AIR Android 清单部分(来自我的 -app.xml 文件)
//AIR android manifest section
<manifest android:installLocation="auto">
<application>
<activity android:name=".TestActivity">
<intent-filter>
<action android:name=".TestActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<service android:name=".TestService">
<intent-filter>
<action android:name="test.default.service"/>
</intent-filter>
</service>
</application>
</manifest>
服务类与活动在同一个包中。Activity 运行得很好,在 Activity 的 onStart() 方法中,我尝试使用以下方法启动服务:
方法一:
import android.app.Activity;
public class TestActivity extends Activity
{
@Override
protected void onStart()
{
super.onStart();
Intent intent = new Intent("test.default.service");
//clearly the classes are available because this compiles!
intent.setClass(TestActivity.this, TestService.class);
startService(intent);
}
}
结果:
Unable to start service Intent { act=test.default.service cmp=air.com.my.company/com.my.company.TestService } U=0: not found
方法二:
import android.app.Activity;
public class TestActivity extends Activity
{
@Override
protected void onStart()
{
super.onStart();
//start a service with intent
startService(new Intent("test.default.service"));
}
}
结果:
FATAL EXCEPTION: main
java.lang.RuntimeExeption: Unable to instantiate service air.com.my.company.TestService:
java.lang.ClassNotFoundException:
Didn't find class "air.com.my.company.TestService" on path: /data/app/air.com.my.company-1.apk
问题似乎是那个“空气”。尝试启动服务时,在包名称前加上前缀。在运行我自己的自定义服务时,如何解决包名称的这种混乱?
我一直在讨论堆栈溢出,以及 Adobe 的论坛,似乎无法找到解决这个问题的完整方法。
还有一些其他的帖子涉及这个主题,但没有一个提供真正的运行解决方案和源代码。
我已将其缩减为最简单的情况,以确保没有导致问题的额外移动部件。
有关如何从在 AIR 中运行的扩展上下文启动 Android 服务的任何官方信息都会有所帮助。似乎连 Adobe 都对这个问题保持沉默。