简体   繁体   中英

onActivityResult from service to fragment

I have an activity which starts my service. After it starts, the service should send data in a fragment. On broad gullies shows that service was started, has made operations, but the fragment has not given out broad gullies showing that it has obtained data. How can I make such a chain activity-> service-> a fragment?

main.java (Activity)

kill = new Intent(this, timer.class);

        pi = createPendingResult(TASK1_CODE, kill, 0);

        intent = new Intent(this, timer.class).putExtra(PARAM_TIME, 7)
                .putExtra(PARAM_PINTENT, pi);

        startService(intent);

        pi = createPendingResult(TASK2_CODE, kill, 0);
        intent = new Intent(this, timer.class).putExtra(PARAM_TIME, 4)
                .putExtra(PARAM_PINTENT, pi);
        startService(intent);

        pi = createPendingResult(TASK3_CODE, kill, 0);
        intent = new Intent(this, timer.class).putExtra(PARAM_TIME, 6)
                .putExtra(PARAM_PINTENT, pi);
        startService(intent);

timer.java (Service)

 public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(LOG_TAG, "MyService onStartCommand");
        int time = intent.getIntExtra(user_rassp.PARAM_TIME, 1);
        PendingIntent pi = intent.getParcelableExtra(user_rassp.PARAM_PINTENT);

    MyRun mr = new MyRun(time, startId, pi);
    es.execute(mr);
    return START_REDELIVER_INTENT;
}

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

class MyRun implements Runnable {

    int time;
    int startId;
    PendingIntent pi;

    public MyRun(int time, int startId, PendingIntent pi) {
        this.time = time;
        this.startId = startId;
        this.pi = pi;
        Log.d(LOG_TAG, "MyRun#" + startId + " create");
    }

    public void run() {
        Log.d(LOG_TAG, "MyRun#" + startId + " start, time = " + time);
        try {

            pi.send(user_rassp.STATUS_START);

            TimeUnit.SECONDS.sleep(time);


            Intent intent = new Intent().putExtra(user_rassp.PARAM_RESULT, time * 100);
            Log.d(LOG_TAG, "конец");
            pi.send(timer.this, user_rassp.STATUS_FINISH, intent);

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (PendingIntent.CanceledException e) {
            e.printStackTrace();
        }
        stop();
    }

    void stop() {
        Log.d(LOG_TAG, "MyRun#" + startId + " end, stopSelfResult("
                + startId + ") = " + stopSelfResult(startId));
    }
}

user_rasp.java (fragment)

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(LOG_TAG, "requestCode = " + requestCode + ", resultCode = "
            + resultCode);

    // Ловим сообщения о старте задач
    if (resultCode == STATUS_START) {
        switch (requestCode) {
            case TASK1_CODE:
                Log.d("task1","start");
                break;
            case TASK2_CODE:
                Log.d("task1", "start");
                break;
            case TASK3_CODE:
                Log.d("task1", "start");
                break;
        }
    }

    // Ловим сообщения об окончании задач
    if (resultCode == STATUS_FINISH) {
        int result = data.getIntExtra(PARAM_RESULT, 0);
        switch (requestCode) {
            case TASK1_CODE:
                Log.d("Task1", "finish, result = " + result);
                break;
            case TASK2_CODE:
                Log.d("Task2", "finish, result = " + result);
                break;
            case TASK3_CODE:
                Log.d("Task3", "finish, result = " + result);
                break;
        }
    }
}

Register a BroadCastReceiver in Fragment. You will need to use an action for it.

From Service send broadcastMessage with above Intent Action. You can pass data via Intent extras

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM