diff --git a/RUN_COMMAND-Intent.md b/RUN_COMMAND-Intent.md index e27856a..8c24201 100644 --- a/RUN_COMMAND-Intent.md +++ b/RUN_COMMAND-Intent.md @@ -149,6 +149,8 @@ import com.termux.shared.termux.TermuxConstants.TERMUX_APP.RUN_COMMAND_SERVICE; ... +String LOG_TAG = "MainActivity"; + Intent intent = new Intent(); intent.setClassName(TermuxConstants.TERMUX_PACKAGE_NAME, TermuxConstants.TERMUX_APP.RUN_COMMAND_SERVICE_NAME); intent.setAction(RUN_COMMAND_SERVICE.ACTION_RUN_COMMAND); @@ -160,19 +162,28 @@ intent.putExtra(RUN_COMMAND_SERVICE.EXTRA_SESSION_ACTION, "0"); intent.putExtra(RUN_COMMAND_SERVICE.EXTRA_COMMAND_LABEL, "top command"); intent.putExtra(RUN_COMMAND_SERVICE.EXTRA_COMMAND_DESCRIPTION, "Runs the top command to show processes using the most resources."); - // Create the intent for the IntentService class that should be sent the result by TermuxService Intent pluginResultsServiceIntent = new Intent(MainActivity.this, PluginResultsService.class); +// Generate a unique execution id for this execution command +int executionId = PluginResultsService.getNextExecutionId(); + // Optional put an extra that uniquely identifies the command internally for your app. // This can be an Intent extra as well with more extras instead of just an int. -pluginResultsServiceIntent.putExtra(PluginResultsService.EXTRA_EXECUTION_ID, PluginResultsService.getNextExecutionId()); +pluginResultsServiceIntent.putExtra(PluginResultsService.EXTRA_EXECUTION_ID, executionId); -// Create the PendingIntent that will be used by TermuxService to send result of commands back to the IntentService -PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 1, pluginResultsServiceIntent, PendingIntent.FLAG_ONE_SHOT); +// Create the PendingIntent that will be used by TermuxService to send result of +// commands back to the IntentService +// Note that the requestCode (currently executionId) must be unique for each pending +// intent, even if extras are different, otherwise only the result of only the first +// execution will be returned since pending intent will be cancelled by android +// after the first result has been sent back via the pending intent and termux +// will not be able to send more. +PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, executionId, pluginResultsServiceIntent, PendingIntent.FLAG_ONE_SHOT); intent.putExtra(RUN_COMMAND_SERVICE.EXTRA_PENDING_INTENT, pendingIntent); // Send command intent for execution +Log.d(LOG_TAG, "Sending execution command with id " + executionId); startService(intent); ``` @@ -201,9 +212,7 @@ public class PluginResultsService extends IntentService { protected void onHandleIntent(@Nullable Intent intent) { if (intent == null) return; - if(intent.getComponent() != null) - Log.d(LOG_TAG, PLUGIN_SERVICE_LABEL + " received execution result from " + intent.getComponent().toString()); - + Log.d(LOG_TAG, PLUGIN_SERVICE_LABEL + " received execution result"); final Bundle resultBundle = intent.getBundleExtra(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE); if (resultBundle == null) {