有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在没有活动的情况下执行Android前台服务

我试图在没有活动的情况下执行服务,但ADV emulator似乎没有响应我的应用程序。正如在标题中所写,我希望服务应用程序能够在没有UI/活动的情况下持续运行

这是我的密码: AndroidManifest。xml:

    <uses-permission 安卓:name="安卓.permission.FOREGROUND_SERVICE"></uses-permission>
    <uses-permission 安卓:name="安卓.permission.WAKE_LOCK" />

    <application
        安卓:label="@string/app_name"
        安卓:icon="@mipmap/ic_launcher"
        安卓:allowBackup="false" >

        <service 安卓:name=".service.PublicStartService" 安卓:exported="true" />

        </application>

</manifest> 

Java文件

public class PublicStartService extends Service {

    private static final String TAG = "ServiceLog";

// The onCreate gets called only one time when the service starts.
@Override
public void onCreate() {
    Log.i(TAG, "onCreate ");
}

// The onStartCommand gets called each time after the startService gets called.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    String param = intent.getStringExtra("PARAM");

    ExecutorService executorService = Executors.newFixedThreadPool(4);
    Runnable worker = new MyRunnable();
    executorService.execute(worker);

    return Service.START_STICKY;
}



// The onDestroy gets called only one time when the service stops.
@Override
public void onDestroy() {
    Log.i(TAG, "onDestroy ");
}

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

class MyRunnable implements Runnable {

    @Override
    public void run() {

        while (true){

            Log.i(TAG, "THREAD THREAD THREAD ");
            try {
                sleep(500);
            } catch (InterruptedException ignored) {

            }
        }

    }
}

}

你知道让它执行会错过什么吗

谢谢


共 (0) 个答案