DEV Community

lyc233333
lyc233333

Posted on

Hongmeng ExtensionAbility: "Universal Supporting Character" for Application Function Extension🎭

hello!I am Xiao L, the female programmer who "plays function extension" in Hongmeng application~ Do you know?On the stage of Hongmeng, UIAbility is the "protagonist" responsible for the front-end interaction, and ExtensionAbility is the "universal supporting role" silently supported - from service cards to input methods, from background services to cross-application data sharing, wherever extension functions are needed, it will be there!Today, let’s talk about this component of “Eighteen Martial Arts” and see how it can “extend infinitely” the application functions~

1. What is ExtensionAbility?"Swiss Army Knife" with extended function 🔧

Core Positioning:

  • Extension components specifically designed to implement "non-interface core functions"
  • Run in the background or system-level scenario, not directly facing the user interface
  • Support independent process isolation to improve security and stability

Typical application scenarios:
| Scenario classification | Specific cases | Technical implementation |
|----------------|-----------------------------------|---------------------------|
| System Integration | InputMethodExtensionAbility |
| Service Enhancement | Music Application Backend Playback Service | ServiceExtensionAbility |
| Card Expansion | Desktop Service Card for Weather Applications | FeatureExtensionAbility |
| Data Sharing | Cross-application data synchronization component | DataExtensionAbility |

2. Life cycle management: "stage schedule" for supporting roles📅

(I) Four core states

Status Trigger Time Corresponding Callback Function Typical Operation
Create When component instance is created onCreate() Resource initialization (such as database connection)
Foreground Switch the component to the foreground (if called by the system) onForeground() Activate real-time data update
Background Component switch to the background (such as the user leaves the scene) onBackground() Pause non-essential tasks
Destroy When component is destroyed onDestroy() Free resources (close files, network connections)

(II) Code example: Lifecycle processing of background services

import { ServiceExtensionAbility, Want } from '@ohos.app.ability';

export default class MusicService extends ServiceExtensionAbility {
  private isPlaying: boolean = false;

// Start background playback when creating
  onCreate(want: Want) {
    super.onCreate(want);
    this.initMusicPlayer();
    console.log('[MusicService] onCreate');
  }

// Display notification bar when front desk status
  onForeground() {
    this.showNotification();
    console.log('[MusicService] onForeground');
  }

// Reduce CPU usage during background status
  onBackground() {
    this.player.pause();
    console.log('[MusicService] onBackground');
  }

// Release resources during destruction
  onDestroy() {
    this.player.destroy();
    console.log('[MusicService] onDestroy');
  }
}
Enter fullscreen mode Exit fullscreen mode

Analysis of the three and four types: "Division of Roles" of Supporting Characters🎭

(I) ServiceExtensionAbility

Positioning: Background service extension (such as music playback, data synchronization)
Features:

  • Long life cycle, supporting cross-process communication
  • Can be started by UIAbility via startAbility

Code example: Start the background download service

// Start the service in UIAbility
const startDownload = () => {
  const want = {
    bundleName: 'com.example.downloader',
    abilityName: 'DownloadService'
  };
  this.context.startAbility(want);
};
Enter fullscreen mode Exit fullscreen mode

(II) FeatureExtensionAbility

Positioning: Service card extension (such as desktop widgets)
Features:

  • Unified management by the system card framework
  • Need to implement the onCreateContent interface rendering card interface

Code example: Weather card rendering

import { FeatureExtensionAbility, ExtensionItem } from '@ohos.app.ability';

export default class WeatherCard extends FeatureExtensionAbility {
  onCreateContent(want: Want): ExtensionItem {
    return {
label: 'Real-time weather',
      icon: 'weather_icon.png',
      content: {
data: { temperature: '25℃', condition: 'Xing' },
template: 'weather_template.json' // Card template path
      }
    };
  }
}
Enter fullscreen mode Exit fullscreen mode

(III) InputMethodExtensionAbility

Positioning: Input method extension (such as custom keyboard)
Features:

  • Implement the input method protocol (IME Protocol)
  • In-depth integration with system input method management services

Key interface:

export default class CustomIM extends InputMethodExtensionAbility {
  onKeyEvent(event: KeyEvent): boolean {
// Handle key events (such as pinyin input to Chinese characters)
    return super.onKeyEvent(event);
  }

  onCandidateSelected(candidate: string): void {
// Triggered when selecting candidate words
    this.sendTextToHost(candidate);
  }
}
Enter fullscreen mode Exit fullscreen mode

(IV) DataExtensionAbility

Positioning: Data sharing extension (such as cross-application data provided)
Features:

  • DataAbility framework based on OpenHarmony
  • Support CRUD operation and URI permission control

Typical Scenario:

// Provide contact data interface
export default class ContactDataExt extends DataExtensionAbility {
  query(uri: Uri, columns: string[]): DataResultSet {
// Query contact data
    return this.contactDB.query(uri, columns);
  }
}
Enter fullscreen mode Exit fullscreen mode

4. Cross-process communication: "Tactic cooperation" between supporting characters and protagonists🤝

(I) EventHub event bus

Scenario: UIAbility and ExtensionAbility bidirectional communication

// Publish events in ExtensionAbility
import { EventHub } from '@ohos.app.ability';

const eventHub = EventHub.create('music_service');
eventHub.publish('playStateChange', { isPlaying: true });

// Subscribe to events in UIAbility
eventHub.on('playStateChange', (data) => {
  this.updatePlayButton(data.isPlaying);
});
Enter fullscreen mode Exit fullscreen mode

(II) AppStorage global storage

Scenario: Lightweight data sharing (such as user configuration)

//Storing data in ExtensionAbility
AppStorage.SetOrCreate('userConfig', { theme: 'dark' });

// Get data in UIAbility
const theme = AppStorage.Get('userConfig')?.theme;
Enter fullscreen mode Exit fullscreen mode

(III) Remote Service

Scenario: Complex object delivery (such as audio and video stream control)

// Define AIDL interface
interface MusicControl {
  play(url: string): void;
  pause(): void;
}

// Implement interface in ExtensionAbility
export default class MusicService extends ServiceExtensionAbility
  implements MusicControl {
play(url: string) { /* Implement playback logic */ }
}
Enter fullscreen mode Exit fullscreen mode

5. Independent sandbox mechanism: "Safety Isolation Cabin" for supporting roles🚢

(I) Sandbox Features (API12+)

  1. Process Isolation: Independent PID, crash does not affect the main application
  2. Memory isolation: Independent JVM heap to avoid memory leaks and spread
  3. Permission Isolation: Independent permission list, you need to explicitly apply for shared permissions

(II) Turn on sandbox configuration

{
  "abilities": [
    {
      "name": "SensitiveDataExt",
      "type": "dataExtension",
"process": "com.example.ext_sandbox", // Independent process name
      "permissions": [
"ohos.permission.READ_SECURE_DATA" // Independent permissions in the sandbox
      ]
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

(III) Sandbox communication restrictions

Communication method Support in the sandbox Alternatives
Shared Memory Not Supported File Mapping (mmap)
Direct process call Not supported IPC (AIDL/MessageParcel)
Global Static Variables Isolated Environment Invisible Distributed Data Services (DDS)

6. Avoiding Pits: "Show Taboos" for Supporting Characters ⚠️

(I) Don't operate the UI in ExtensionAbility

// ❌ Error: Modify UI components directly in the background service
export default class DataExt extends ServiceExtensionAbility {
  onCreate() {
new MainAbility().updateUI(); // UI thread cannot be accessed
  }
}

// ✅ Correct: Notify UIAbility updates via events
EventHub.create('data_update').publish('refreshUI');
Enter fullscreen mode Exit fullscreen mode

(II) Pay attention to resource access in the sandbox

limit:

  • Cannot directly access the private directory of the main application
  • Public data needs to be accessed through ohos.data.DataAbilityHelper

Correct way:

// Access public files in the sandbox
const fileDescriptor = this.context.getExternalFilesDir();
const inputStream = fileDescriptor.openRead('public_data.txt');
Enter fullscreen mode Exit fullscreen mode

(III) Processing system resource recycling

Scenario: ExtensionAbility may be killed first when the system is insufficient
Coping strategies:

  • Release cached resources in onMemoryLevel callback
  • Use startForegroundService to enhance service priority

7. Future trend: "Gorgeous Upgrade" of Supporting Characters🌟

(I) In-depth integration of "atomic services"

ExtensionAbility will become the core carrier of atomized services, such as:

  • A translation atomization service can provide cross-application text translation interface through ExtensionAbility
  • Call services in any supported app without installing a complete application

(II) Intelligent expansion driven by AI

In the future, it may support dynamic loading of ExtensionAbility through AI models, such as:

  • The system automatically enables intelligent recommendation components according to user habits
  • Voice Assistant calls third-party tools in real time through ExtensionAbility

(III) Cross-device expansion collaboration

In distributed scenarios, ExtensionAbility can be seamlessly migrated across devices:

  • Automatically synchronize the clipboard extension components on your phone to the tablet
  • The control components of smart home devices can be loaded remotely to the mobile phone

Summary: ExtensionAbility's "Supporting Character Philosophy" 📚

Application scalability = (Component type matching degree × Communication efficiency) ÷ Resource consumption

  • Backend Logic → Use ServiceExtensionAbility
  • System Integration → Use InputMethodExtensionAbility
  • Lightweight extension → Priority to sandbox mode

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.