Android 在应用程序上下文中调用startService时未启动

Android 在应用程序上下文中调用startService时未启动,android,Android,startService未启动该服务。我是从应用程序上下文调用它的,但是尽管在startService之前从中获取了控制台日志消息,但我没有从服务内部获取任何消息 public class AntoxOnFriendRequestCallback implements OnFriendRequestCallback { private static final String TAG = "im.tox.antox.TAG"; public static final String FRIEND_

startService未启动该服务。我是从应用程序上下文调用它的,但是尽管在startService之前从中获取了控制台日志消息,但我没有从服务内部获取任何消息

public class AntoxOnFriendRequestCallback implements OnFriendRequestCallback {

private static final String TAG = "im.tox.antox.TAG";
public static final String FRIEND_KEY = "im.tox.antox.FRIEND_KEY";
public static final String FRIEND_MESSAGE = "im.tox.antox.FRIEND_MESSAGE";

private Context ctx;

public AntoxOnFriendRequestCallback(Context ctx) {
    this.ctx = ctx;
}

@Override
public void execute(String publicKey, String message){
    Log.d(TAG, "Friend request callback");
    Intent intent = new Intent(this.ctx, ToxService.class);
    intent.setAction(Constants.FRIEND_REQUEST);
    intent.putExtra(FRIEND_KEY, publicKey);
    intent.putExtra(FRIEND_MESSAGE, message);
    this.ctx.startService(intent);
}
}
要点如下:

AntoxFriendRequestCallback在ToxService的第61行使用getApplicationContext()调用

我在AntoxFriendRequestCallback第15行的日志中看到了“好友请求回调”

我在ToxService的第140行的日志中没有看到“Constants.FRIEND_REQUEST”,在MainActivity的第20行中也没有看到“test”

如果要查看完整文件,请访问以下位置:
找到了问题。startService是在应用程序中以DO_TOX意图调用的,在处理该服务的服务中,有一个无限循环打算重复调用某个东西,但这阻止了任何新的startService()在排队并需要最后一个完成时工作。我用ScheduledExecuterService替换了无限循环,现在它可以工作了。

您必须确保服务是否在“androidmanifest.xml”中声明,如:

<service
    android:name="[your service class]"
    android:enabled="true"
    android:icon="@drawable/ic_launcher" >
</service>

请确保,您的清单中有服务条目。忘记提到它在那里。此外,服务在代码中的其他位置成功启动。
Intent intent = new Intent(this.ctx, ToxService.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//add this line
intent.setAction(Constants.FRIEND_REQUEST);
intent.putExtra(FRIEND_KEY, publicKey);
intent.putExtra(FRIEND_MESSAGE, message);
this.ctx.startService(intent);