我可以在没有活动或GUI的情况下启动android服务吗?

Swa*_*esh 5 android android-service

我的目标Android是4.1.2。我创建了一个简单的android服务,它将在启动时显示Toast。但是此应用程序不应具有任何GUI。我仅通过在启动时显示GUI的活动成功运行了该服务。

public class MyServices extends Service {
private MediaRecorder recorder = null;
@Override
public IBinder onBind(Intent intent) {

    return null;

}
public int onStartCommand(Intent intent, int flags, int StartId)
{

    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();

}
}
Run Code Online (Sandbox Code Playgroud)

Aja*_*y S 4

您可以从以下版本启动此服务,RebootReceiver但从 Android 3.0 开始,用户需要至少启动应用程序一次,然后您的应用程序才能接收android.intent.action.BOOT_COMPLETED事件。

重新启动接收器 ->启动时的 Android BroadcastReceiver - 当 Activity 处于后台时保持运行