Skip to content

duongpt582/IPC-Android

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Android IPC (Inter-Process Communication) Demo

A demonstration project showcasing Android's Inter-Process Communication (IPC) capabilities using AIDL (Android Interface Definition Language). This project consists of two separate Android applications: an IPC server that provides a calculator service and an IPC client that consumes this service.

📋 Project Overview

Inter-Process Communication (IPC) in Android allows different applications or processes to communicate with each other securely. This demo implements a simple calculator service that can be accessed by external applications through Android's Binder mechanism.

Applications Included:

  • IPC Server (com.duongpt.ipcserver) - Provides calculator functionality as a service
  • IPC Client (com.duongpt.ipcclient) - Consumes the calculator service and demonstrates remote method calls

🏗️ Architecture & Components

IPC Server Application

com.duongpt.ipcserver/
├── MainActivity.java          # Main activity with basic UI
├── CalculatorService.java     # Service implementing calculator functionality
└── ICalculator.aidl          # AIDL interface definition

Key Components:

  • MainActivity: Basic Android activity serving as the app entry point
  • CalculatorService: Android Service that implements the ICalculator interface and handles remote method calls
  • ICalculator.aidl: AIDL interface defining the contract for calculator operations

IPC Client Application

com.duongpt.ipcclient/
├── MainActivity.java          # Main activity with calculator UI and service binding
└── ICalculator.aidl          # Copy of server's AIDL interface for binding

Key Components:

  • MainActivity: Contains UI elements and service connection logic to interact with the remote calculator service
  • ICalculator.aidl: Identical copy of the server's AIDL interface required for client-side binding

✨ Features

  • Cross-Application Communication: Demonstrates communication between two separate Android apps
  • AIDL Interface: Uses Android Interface Definition Language for defining service contracts
  • Service Binding: Implements proper service binding and connection management
  • Remote Method Invocation: Shows how to call methods on remote services
  • Connection State Handling: Includes proper handling of service connection and disconnection
  • Security: Implements package queries for Android 11+ compatibility

🛠️ Technical Specifications

  • Target SDK: 36
  • Minimum SDK: 24 (Android 7.0)
  • Language: Java
  • Build System: Gradle
  • Architecture: Client-Server IPC using Android Binder
  • Communication Protocol: AIDL (Android Interface Definition Language)

📱 Setup & Installation

Prerequisites

  • Android Studio (latest version recommended)
  • Android SDK API 24 or higher
  • Android device or emulator running Android 7.0+

Installation Steps

  1. Clone the repository

    git clone <repository-url>
    cd IPCAndroid
  2. Open both projects in Android Studio

    • Open IPCServer project
    • Open IPCClient project in a separate window
  3. Build both applications

    # Build IPC Server
    cd IPCServer
    ./gradlew build
    
    # Build IPC Client
    cd ../IPCClient
    ./gradlew build
  4. Install both apps on your device/emulator

    • Install IPC Server app first
    • Install IPC Client app second

Running the Demo

  1. Start the IPC Server

    • Launch the IPC Server app
    • The app will register the calculator service
  2. Use the IPC Client

    • Launch the IPC Client app
    • The app will automatically attempt to bind to the calculator service
    • Click "Add 5 + 3" button to test the remote calculation
    • The result should display: "Result: 8"

🔧 How It Works

AIDL Interface

The ICalculator.aidl file defines the contract:

interface ICalculator {
    int add(int x, int y);
}

Service Implementation

The server implements the AIDL interface:

final ICalculator.Stub mBinder = new ICalculator.Stub() {
    @Override
    public int add(int x, int y) throws RemoteException {
        return x + y;
    }
};

Client Binding

The client binds to the service using an explicit intent:

Intent intent = new Intent("com.duongpt.ipcserver.RUN_SERVICE");
intent.setPackage("com.duongpt.ipcserver");
bindService(intent, serviceConnection, BIND_AUTO_CREATE);

Communication Flow

  1. Client app creates an intent targeting the server's service
  2. Android system establishes a Binder connection
  3. Client receives an IBinder object through ServiceConnection callback
  4. Client converts IBinder to ICalculator interface using ICalculator.Stub.asInterface()
  5. Client can now call remote methods as if they were local

📁 Project Structure

IPCAndroid/
├── IPCServer/                 # Server application
│   ├── app/
│   │   ├── src/main/
│   │   │   ├── java/com/duongpt/ipcserver/
│   │   │   │   ├── MainActivity.java
│   │   │   │   └── CalculatorService.java
│   │   │   ├── aidl/com/duongpt/ipcserver/
│   │   │   │   └── ICalculator.aidl
│   │   │   └── AndroidManifest.xml
│   │   └── build.gradle
│   └── settings.gradle
├── IPCClient/                 # Client application
│   ├── app/
│   │   ├── src/main/
│   │   │   ├── java/com/duongpt/ipcclient/
│   │   │   │   └── MainActivity.java
│   │   │   ├── aidl/com/duongpt/ipcserver/
│   │   │   │   └── ICalculator.aidl
│   │   │   └── AndroidManifest.xml
│   │   └── build.gradle.kts
│   └── settings.gradle.kts
└── README.md

🔒 Security Considerations

  • Package Queries: The client app includes <queries> element in AndroidManifest.xml for Android 11+ package visibility
  • Exported Service: The server service is marked as exported="true" to allow external access
  • Intent Filtering: Server service uses intent filters for controlled access
  • Package Targeting: Client uses explicit package targeting to prevent intent hijacking

🚀 Extending the Project

You can extend this project by:

  • Adding more mathematical operations (subtract, multiply, divide)
  • Implementing data transfer objects using Parcelable
  • Adding authentication/authorization mechanisms
  • Creating more complex service interactions
  • Implementing callbacks from server to client

📚 Learning Resources

👨‍💻 Author

duongpt - Initial work

📄 License

This project is open source and available under the MIT License.


This project demonstrates fundamental Android IPC concepts and serves as a learning resource for Android developers interested in inter-process communication.

About

Android IPC (Inter-Process Communication) Demo

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published