启动 Android 服务

Starting an Android Service

抱歉,这是一个愚蠢的问题,但是如何启动 android 服务?我无法找出 public class scilentservice extends IntentService { 上的错误代码(请参阅下面的代码)。我认为这可能与意图有关,但我并不肯定。我想要的是让我的应用程序启动服务,然后在特定时间自动科学 phone。 这是我的代码

/**
 * Created by Abe on 3/29/2015.
 */

import android.app.IntentService;
import android.content.Intent;

public class scilentservice extends IntentService {
    @Override
    protected void onHandleIntent(Intent workIntent) {
        // Gets data from the incoming Intent
        String dataString = workIntent.getDataString();
        ...
        // Do work here, based on the contents of dataString
        ...
    }
}
`

在此先感谢您的帮助。

我改用了 extends Intent 并覆盖了方法 onStartCommand

@Override
public int onStartCommand(Intent intent, int flags, int startId) {


    return 1;
}

并且您可以根据 activity 发送的某些命令或发送给应用程序的某些通知来启动服务。

这个问题将被否决并可能被关闭,因为它是通过简单 Google Search. Please look at the documentation linked in the comments and then the next step 找到的关于如何发送请求的基本信息……这是 Android 文档。

片段:

为名为 RSSPullService 的 IntentService 创建一个新的显式 Intent。

/*
 * Creates a new Intent to start the RSSPullService
 * IntentService. Passes a URI in the
 * Intent's "data" field.
 */
mServiceIntent = new Intent(getActivity(), RSSPullService.class);
mServiceIntent.setData(Uri.parse(dataUrl));

调用 startService()

// Starts the IntentService
getActivity().startService(mServiceIntent);

intentService代码:

public class RSSPullService extends IntentService {

    public RSSPullService() {
        super("RSSPullService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        //Do the work here.
    }
}