M
M
Maybe_V2016-04-22 17:17:12
Android
Maybe_V, 2016-04-22 17:17:12

What could be the problem when calling the service?

I am studying service, and a question arose about transferring data between the service and the activity. Found this tutorial , and did everything as described in it. But when calling the service, it simply throws it out of the program, while it does not write any exeption or errors in the logs. Just such a dialog, and the program closes:
57fe8967f9414f518c993f3cdbe76a59.pngHere is my code:
activation:

public class MainActivity extends AppCompatActivity {

    private final int TASK1_CODE = 1;
    private final int TASK2_CODE = 2;
    private final int TASK3_CODE = 3;

    public final static int STATUS_START = 100;
    public final static int STATUS_FINISH = 200;

    public static final String PARAM_TIME = "TIME";
    public static final String PARAM_PINTENT = "PINTENT";
    public static final String PARAM_RESULT = "RESULT";

    private TextView textTask1, textTask2, textTask3;
    private Button startService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textTask1 = (TextView) findViewById(R.id.task1);
        textTask1.setText("task 1 ");

        textTask2 = (TextView) findViewById(R.id.task2);
        textTask2.setText("task 2");

        textTask3 = (TextView) findViewById(R.id.task3);
        textTask3.setText("task 3");

        startService = (Button) findViewById(R.id.startButton);
        startService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickStart();
            }
        });


    }


    private void onClickStart(){
        PendingIntent pi;
        Intent intent;

        pi = createPendingResult(TASK1_CODE, null, 0);
        intent = new Intent(getApplicationContext(), MyService.class)
                .putExtra(PARAM_TIME, 7)
                .putExtra(PARAM_PINTENT, pi);
        startService(intent);

        pi = createPendingResult(TASK2_CODE, null, 0);
        intent = new Intent(getApplicationContext(), MyService.class)
                .putExtra(PARAM_TIME, 4)
                .putExtra(PARAM_PINTENT, pi);
        startService(intent);

        pi = createPendingResult(TASK3_CODE, null, 0);
        intent = new Intent(getApplicationContext(), MyService.class)
                .putExtra(PARAM_TIME, 6)
                .putExtra(PARAM_PINTENT, pi);
        startService(intent);

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == STATUS_START){
            switch (requestCode){
                case TASK1_CODE:{
                    textTask1.setText("task 1 start");
                    break;
                }
                case TASK2_CODE:{
                    textTask2.setText("task 2 start");
                    break;
                }
                case TASK3_CODE:{
                    textTask3.setText("task 3 start");
                    break;
                }
            }
        }
        if (resultCode == STATUS_FINISH){
            int result = data.getIntExtra(PARAM_RESULT, 0);
            switch (requestCode){
                case TASK1_CODE:{
                    textTask1.setText("task 1 finish, result = " + result );
                    break;
                }
                case TASK2_CODE:{
                    textTask2.setText("task 2 finish, result = " + result);
                    break;
                }
                case TASK3_CODE:{
                    textTask3.setText("task 3 finish , result = " + result);
                    break;
                }
            }
        }

    }


}

and service:
public class MyService extends Service {

    private final String LOG = "MYSERVICELOG";
    private ExecutorService executorService;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(LOG, "onCreate");
        executorService = Executors.newFixedThreadPool(2);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(LOG, "onDestroy");
    }



    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(LOG, "onStartCommand");
        int time = intent.getIntExtra(MainActivity.PARAM_TIME, 1);
        PendingIntent pi = intent.getParcelableExtra(MainActivity.PARAM_PINTENT);

        MyRun myRun = new MyRun(time, startId, pi);
        executorService.execute(myRun);

        return super.onStartCommand(intent, flags, startId);

    }

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


    class MyRun implements Runnable{

        private int time, startId;
        private PendingIntent pendingIntent;

        public MyRun(int time, int startId, PendingIntent pendingIntent){
            this.time = time;
            this.startId = startId;
            this.pendingIntent = pendingIntent;
            Log.d(LOG, "MyRun# " + startId + " created");
        }


        @Override
        public void run() {
            Log.d(LOG, "MyRun# " + startId + " time: " + time + " start");
            try {
                pendingIntent.send(MainActivity.STATUS_START);

                 TimeUnit.SECONDS.sleep(time);
                Intent intent = new Intent()
                        .putExtra(MainActivity.PARAM_RESULT, time *100);
                pendingIntent.send(MyService.this, MainActivity.STATUS_FINISH, intent);

            }catch (PendingIntent.CanceledException ce){
                ce.getStackTrace();
            }catch (InterruptedException ie){
                ie.getStackTrace();
            }
            stop();

        }

        private void stop(){
            Log.d(LOG, "MyService# " + startId + " stoped" );
            stopSelfResult(startId);
        }


    }

}

What could be the problem ??
Help I can't figure it out!!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question