使用异步任务的服务无法启动

时间:2016-05-29 09:57:33

标签: android asynchronous foreground-service

我正在使用以下代码来启动服务。此服务应启动检查mysql并提供通知的异步任务。服务没有启动,我找不到原因?我没有从服务中收到任何日志。 (当我在没有服务的情况下直接从主活动调用Async任务时,它可以正常工作)。

主要活动代码:

Intent startIntent = new Intent(MainActivity.this, ForegroundService.class);
MainActivity.this.startService(startIntent);

服务类代码:

 public class ForegroundService extends Service {
    private static final String LOG_TAG = "ForegroundService";

    @Override
    public void onCreate() {
        Log.i(LOG_TAG, "On Create");

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(LOG_TAG, "On Start");
        new Thread(new Runnable() {

            @Override
            public void run() {
                String newoperator = "Go";
                TimeSync ReportCheck= new TimeSync(getApplicationContext());
                ReportCheck.execute(newoperator);
            }

        }).start();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(LOG_TAG, "In onDestroy");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }
}

我还发现了一些在主活动中添加以下命令的解决方案,但该命令不适用于我的android工作室。

startIntent.setAction(Constants.ACTION.STARTFOREGROUND_ACTION);

1 个答案:

答案 0 :(得分:1)

你使用的是什么版本的android studio?您是否尝试更新到最新版本?

Download Android Studio 2.1

相关问题