gpt4 book ai didi

android - 从不同的包启动服务

转载 作者:太空狗 更新时间:2023-10-29 12:51:40 25 4
gpt4 key购买 nike

我想做一个项目,我有一项 Activity 和一项来自不同包的服务。我把这两个包放在一个项目中。

我把 Activity 放在:com.idris.activity,我把服务放在:com.idris.activity.service

我尝试像这样从 MyActivity 中的按钮监听器调用 MyService :

Intent myIntent = new Intent(getApplicationContext(), MyService.class);
myIntent.putExtra("extraData", "somedata");
startService(myIntent);

应用程序 list

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="idris.parental.monitor"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MyActivity"
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=".MyService"
android:enabled="true" />
</application>
</manifest>

MyService 无法启动,我该怎么办?

最佳答案

在你的 list 中使用它:

<service
android:name=".service.MyService"
android:enabled="true" />

确保您在 manifest 文件中为 service 使用正确的 package name。如果 MyService.java 文件在 com.idris.activity.service 包中,则使用:

<service android:name="com.idris.activity.service.MyService" />

关于android - 从不同的包启动服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11615831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com