Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 启动其他服务中的服务不启动_Java_Android - Fatal编程技术网

Java 启动其他服务中的服务不启动

Java 启动其他服务中的服务不启动,java,android,Java,Android,我想从另一个不在活动中的服务启动一个服务,我执行了以下代码,但没有执行它执行块,但服务未启动 Myservice.class HttpPost post = new HttpPost(Constant1.URL_GET_COURSE_LIST); String result = HTTPadapter.getDetails(post); Log.e("resultofcourselist", "" + result); Intent i=new Intent(getApplicationCont

我想从另一个不在活动中的服务启动一个服务,我执行了以下代码,但没有执行它执行块,但服务未启动

Myservice.class

HttpPost post = new HttpPost(Constant1.URL_GET_COURSE_LIST);
String result = HTTPadapter.getDetails(post);
Log.e("resultofcourselist", "" + result);
Intent i=new Intent(getApplicationContext(),DemoService.class);
i.putExtra("result", result);
startService(i);
intent.getStringExtra("result");

try {
    jsonResponse = new JSONArray(result);
    jsonObject = jsonResponse.getJSONObject(0);
    jsonMainNode = jsonObject.optJSONArray("rowsResponse");

    //DbAdd dbAdd=new DbAdd();
    for (int i = 0; i < jsonMainNode.length(); i++) {
        JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
        result = jsonChildNode.optString("result").toString();

        if (result.equals("Success")) {
            jsonObject = jsonResponse.getJSONObject(1);
            jsonMainNode = jsonObject.optJSONArray("getCourseList");
            // dbAdd.setJsonMainNode(jsonMainNode);
            for (int j = 0; j < jsonMainNode.length(); j++) {
                // dbAdd.setJsonChildNode();
                JSONObject childnode = jsonMainNode.getJSONObject(j);
                Intent dbadd = new Intent(getApplicationContext(), Dbadd.class);
                dbadd.putExtra("mainnode", jsonMainNode.toString());
                dbadd.putExtra("childnode", childnode.toString());
                // dbadd.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startService(dbadd);
            }
            stopSelf();
        } else {
            //
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}
Demoservice.class

HttpPost post = new HttpPost(Constant1.URL_GET_COURSE_LIST);
String result = HTTPadapter.getDetails(post);
Log.e("resultofcourselist", "" + result);
Intent i=new Intent(getApplicationContext(),DemoService.class);
i.putExtra("result", result);
startService(i);
intent.getStringExtra("result");

try {
    jsonResponse = new JSONArray(result);
    jsonObject = jsonResponse.getJSONObject(0);
    jsonMainNode = jsonObject.optJSONArray("rowsResponse");

    //DbAdd dbAdd=new DbAdd();
    for (int i = 0; i < jsonMainNode.length(); i++) {
        JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
        result = jsonChildNode.optString("result").toString();

        if (result.equals("Success")) {
            jsonObject = jsonResponse.getJSONObject(1);
            jsonMainNode = jsonObject.optJSONArray("getCourseList");
            // dbAdd.setJsonMainNode(jsonMainNode);
            for (int j = 0; j < jsonMainNode.length(); j++) {
                // dbAdd.setJsonChildNode();
                JSONObject childnode = jsonMainNode.getJSONObject(j);
                Intent dbadd = new Intent(getApplicationContext(), Dbadd.class);
                dbadd.putExtra("mainnode", jsonMainNode.toString());
                dbadd.putExtra("childnode", childnode.toString());
                // dbadd.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startService(dbadd);
            }
            stopSelf();
        } else {
            //
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}

删除以下行,因为在启动服务后,您突然调用了停止服务的
stopSelf()

stopSelf();

使用意图服务。您不需要停止意图服务。任务完成后,它将自动停止

如何制作意向服务类:

public class MyIntentService extends IntentService {
/**
 * Creates an IntentService.  Invoked by your subclass's constructor.
 *
 * @param name Used to name the worker thread, important only for debugging.
 */
private static final String TAG = "MyIS";
private Context mContext;
public MyIntentService() {
    super(TAG);
    mContext = this;}

@Override
protected void onHandleIntent(Intent intent) {
    //Do Your Code Here.
}}
别忘了在舱单上登记:

 <service
        android:name=".MyIntentService "
        android:exported="false" />

需要更多帮助,然后发表评论。

@martin Demoservice从不通过我的服务启动这是我的服务problem@user3481301答案已被编辑,因此我的评论(以及你的)过时了^^我得到了McContext,但intentservice也是上下文的产物,所以它不需要mcontext实际上我从AsyncTask启动了Intent服务,所以我写了这个。谢谢你。
        Intent intent_update = new Intent(mContext, MyIntentService .class);
    mContext.startService(intent_update);