I study the service, and the question arose about the transfer of data between the service and activation. Found this lesson , and did everything as described in it. But when calling a service, it simply throws it out of the program, and does not write any exeption or errors in the logs. Just such a dialogue, and the program closes:

enter image description here

Here is my code:

Activate:

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 not understand !!

  • It's not entirely clear why you run the service three times in a row. And most likely PendingIntent cannot be transferred through the Bundle. - Yuriy SPb 6:43 pm
  • And what in logs? - Eugene Krivenja
  • @EugeneKrivenja, that just when throwing from the program does not print the stack trace. So I can not understand what the problem is. - Maybe_V
  • @YriySPb, I just did the example (I brought the link in question), and there it was done to demonstrate (most likely) how the stopSelfResult (startId) method works. But for some reason my program crashes, when I start the service, it does not print the stack trace in the log ... and therefore I cannot understand what the error is. - Maybe_V
  • Well ... Maybe your service is not registered in the manifest ... Well, try to isolate the problem - remove all unnecessary from the service and leave only its launch without any data. And then add the code. See where it starts to fall. Now the code looks strange, but there are no obvious errors. - Yuriy SPb

1 answer 1

The problem was in the initialization of PendingIntent , more precisely in the fact that I passed null instead of intent to the createPendingResult(TASK1_CODE, null, 0) method createPendingResult(TASK1_CODE, null, 0) .

The problem was solved after replacing the code:

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

on code:

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