mirror of https://github.com/termux/termux-app
Updated RUN_COMMAND Intent (markdown)
parent
06f1de1b26
commit
d8fc8b7f14
|
@ -149,6 +149,8 @@ import com.termux.shared.termux.TermuxConstants.TERMUX_APP.RUN_COMMAND_SERVICE;
|
||||||
|
|
||||||
...
|
...
|
||||||
|
|
||||||
|
String LOG_TAG = "MainActivity";
|
||||||
|
|
||||||
Intent intent = new Intent();
|
Intent intent = new Intent();
|
||||||
intent.setClassName(TermuxConstants.TERMUX_PACKAGE_NAME, TermuxConstants.TERMUX_APP.RUN_COMMAND_SERVICE_NAME);
|
intent.setClassName(TermuxConstants.TERMUX_PACKAGE_NAME, TermuxConstants.TERMUX_APP.RUN_COMMAND_SERVICE_NAME);
|
||||||
intent.setAction(RUN_COMMAND_SERVICE.ACTION_RUN_COMMAND);
|
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_LABEL, "top command");
|
||||||
intent.putExtra(RUN_COMMAND_SERVICE.EXTRA_COMMAND_DESCRIPTION, "Runs the top command to show processes using the most resources.");
|
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
|
// Create the intent for the IntentService class that should be sent the result by TermuxService
|
||||||
Intent pluginResultsServiceIntent = new Intent(MainActivity.this, PluginResultsService.class);
|
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.
|
// 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.
|
// 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
|
// Create the PendingIntent that will be used by TermuxService to send result of
|
||||||
PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 1, pluginResultsServiceIntent, PendingIntent.FLAG_ONE_SHOT);
|
// 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);
|
intent.putExtra(RUN_COMMAND_SERVICE.EXTRA_PENDING_INTENT, pendingIntent);
|
||||||
|
|
||||||
// Send command intent for execution
|
// Send command intent for execution
|
||||||
|
Log.d(LOG_TAG, "Sending execution command with id " + executionId);
|
||||||
startService(intent);
|
startService(intent);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -201,9 +212,7 @@ public class PluginResultsService extends IntentService {
|
||||||
protected void onHandleIntent(@Nullable Intent intent) {
|
protected void onHandleIntent(@Nullable Intent intent) {
|
||||||
if (intent == null) return;
|
if (intent == null) return;
|
||||||
|
|
||||||
if(intent.getComponent() != null)
|
Log.d(LOG_TAG, PLUGIN_SERVICE_LABEL + " received execution result");
|
||||||
Log.d(LOG_TAG, PLUGIN_SERVICE_LABEL + " received execution result from " + intent.getComponent().toString());
|
|
||||||
|
|
||||||
|
|
||||||
final Bundle resultBundle = intent.getBundleExtra(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE);
|
final Bundle resultBundle = intent.getBundleExtra(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE);
|
||||||
if (resultBundle == null) {
|
if (resultBundle == null) {
|
||||||
|
|
Loading…
Reference in New Issue