启动Android服务

时间:2015-03-29 17:04:57

标签: java android android-intent background-service

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

/**
 * 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
        ...
    }
}
`

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

这个问题会被关闭并且可能会被关闭,因为它是用简单的Google Search找到的基本信息。请查看评论中链接的文档,然后查看有关如何发送请求的next step ...这是Android文档。

摘录:

为IntentService创建一个名为RSSPullService的新的显式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.
    }
}

答案 1 :(得分:0)

我改为使用extends Intent并覆盖方法onStartCommand

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


    return 1;
}

您可以从活动发送的某个命令或发送给应用程序的某些通知启动服务。