gpt4 book ai didi

java - 按下按钮时服务未启动?

转载 作者:行者123 更新时间:2023-12-02 05:36:10 24 4
gpt4 key购买 nike

我知道这个问题之前已经被问过一百万次了,但没有任何效果对我有用。我在一个单独的类中有一个服务,在从 LaunchIntent 启动应用程序后,按下按钮时需要启动该服务。

长话短说,这是我的目标:

运行命令>等待三秒让命令运行>启动应用程序>启动服务

该服务用于监视 CONFIGURATION_CHANGED 广播。

list (重要的部分):

</activity>

<receiver android:name="MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.CONFIGURATION_CHANGED" >
</action>
</intent-filter>
</receiver>

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

</application>

</manifest>

MyService.java:

import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import android.app.Service;

public class MyService extends Service {

String[] commandsdefault = {"/x"};

public void onCreate() {
Toast.makeText(this, "x", Toast.LENGTH_SHORT);
}

public void onDestroy() {
Toast.makeText(this, "x", Toast.LENGTH_SHORT);
}

public void onReceive(Context context, Intent intent) {
MainActivity ogres = new MainActivity();
ogres.RunAsRoot(commandsdefault);
}

@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "x", Toast.LENGTH_SHORT);
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

}

然后我只需在 MainActivity.java 中添加以下行来调用服务来启动:

startService(new Intent(getApplicationContext(), MyService.class));

我比模特店里的蚊子还困惑。除了 u=0 not found 之外,LogCat 绝对没有返回任何有用的信息。

我这里有什么不正确的地方吗?我什至没有看到服务启动时的 toast 。

最佳答案

确保您的服务已在 Android list 中声明

关于java - 按下按钮时服务未启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24943724/

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