### Programmatic Ad Blocking Setup
Source: https://context7.com/m66b/netguard/llms.txt
Configure ad blocking preferences and trigger the hosts file download service programmatically using SharedPreferences and Intents.
```java
// Programmatic ad blocking setup
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit()
.putBoolean("filter", true)
.putBoolean("use_hosts", true)
.putBoolean("manage_system", true)
.putString("hosts_url", "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts")
.apply();
// Trigger hosts file download
Intent intent = new Intent("eu.faircode.netguard.DOWNLOAD_HOSTS_FILE");
intent.setPackage("eu.faircode.netguard");
ContextCompat.startForegroundService(context, intent);
```
--------------------------------
### Download Hosts File using ADB
Source: https://github.com/m66b/netguard/blob/master/ADBLOCKING.md
Use this ADB command to trigger the automatic download of a hosts file. Ensure NetGuard is installed and running.
```bash
adb shell am startservice -a eu.faircode.netguard.DOWNLOAD_HOSTS_FILE
```
--------------------------------
### Configure Port Forwarding
Source: https://context7.com/m66b/netguard/llms.txt
Start or stop port forwarding by launching the corresponding NetGuard activity.
```java
// Start port forwarding via Intent
Intent forward = new Intent("eu.faircode.netguard.START_PORT_FORWARD");
forward.setPackage("eu.faircode.netguard");
// Configure forwarding parameters in the approval activity
startActivity(forward);
// Stop port forwarding
Intent stopForward = new Intent("eu.faircode.netguard.STOP_PORT_FORWARD");
stopForward.setPackage("eu.faircode.netguard");
startActivity(stopForward);
```
--------------------------------
### Port Forwarding API
Source: https://context7.com/m66b/netguard/llms.txt
Intents to start and stop port forwarding configurations.
```APIDOC
## Port Forwarding API
### Description
Manages port forwarding redirection. Requires filtering mode to be enabled.
### Actions
- `eu.faircode.netguard.START_PORT_FORWARD`: Start forwarding
- `eu.faircode.netguard.STOP_PORT_FORWARD`: Stop forwarding
```
--------------------------------
### Enable Ad Blocking via ADB
Source: https://context7.com/m66b/netguard/llms.txt
Enable traffic filtering and hosts file blocking for ad blocking using ADB commands. This requires installing NetGuard from GitHub releases and enabling specific settings.
```bash
# Step 1: Install NetGuard from GitHub releases (not Play Store)
# Step 2: Enable required settings via ADB or app UI
# Enable traffic filtering
adb shell settings put secure netguard_filter 1
# Enable hosts file blocking
adb shell settings put secure netguard_use_hosts 1
```
--------------------------------
### Configure NetGuard via ADB
Source: https://context7.com/m66b/netguard/llms.txt
Use ADB commands to enable system app management, trigger hosts file downloads, and verify ad blocking functionality.
```bash
adb shell settings put secure netguard_manage_system 1
adb shell am startservice -a eu.faircode.netguard.DOWNLOAD_HOSTS_FILE
# Test ad blocking by visiting: http://www.netguard.me/test
```
--------------------------------
### Define Quick Settings Tiles
Source: https://context7.com/m66b/netguard/llms.txt
Register NetGuard tiles in the Android manifest to enable quick access from the notification shade.
```xml
```
--------------------------------
### Enable VPN Dialogs Package
Source: https://github.com/m66b/netguard/blob/master/README.md
Use this command to re-enable the com.android.vpndialogs package if it has been removed or disabled, which is necessary for NetGuard to function correctly. Requires root permissions.
```bash
adb shell pm enable --user 0 com.android.vpndialogs
```
--------------------------------
### Configure NetGuard Library Build
Source: https://github.com/m66b/netguard/blob/master/app/CMakeLists.txt
Defines the NetGuard shared library, lists its source files, includes necessary directories, finds and links the log library, and sets a specific linker option for maximum page size.
```cmake
cmake_minimum_required(VERSION 3.4.1)
project(netguard)
add_library( netguard
SHARED
src/main/jni/netguard/netguard.c
src/main/jni/netguard/session.c
src/main/jni/netguard/ip.c
src/main/jni/netguard/tcp.c
src/main/jni/netguard/udp.c
src/main/jni/netguard/icmp.c
src/main/jni/netguard/tls.c
src/main/jni/netguard/dns.c
src/main/jni/netguard/dhcp.c
src/main/jni/netguard/pcap.c
src/main/jni/netguard/util.c )
include_directories( src/main/jni/netguard/ )
find_library( log-lib
log )
target_link_libraries( netguard
${log-lib} )
target_link_options(netguard PRIVATE "-Wl,-z,max-page-size=16384")
```
--------------------------------
### Automate NetGuard via Broadcast Intents
Source: https://context7.com/m66b/netguard/llms.txt
Use ADB broadcast commands to toggle firewall states. Requires the eu.faircode.netguard.permission.ADMIN permission.
```bash
# Enable the firewall
adb shell am broadcast -a eu.faircode.netguard.ON
# Disable the firewall
adb shell am broadcast -a eu.faircode.netguard.OFF
# Enable lockdown mode (blocks all traffic except allowed apps)
adb shell am broadcast -a eu.faircode.netguard.LOCKDOWN_ON
# Disable lockdown mode
adb shell am broadcast -a eu.faircode.netguard.LOCKDOWN_OFF
```
--------------------------------
### Manage VPN Service Integration
Source: https://context7.com/m66b/netguard/llms.txt
Handle VPN permission requests and service startup using the Android VpnService API.
```java
// Check if VPN permission is granted
Intent prepare = VpnService.prepare(context);
if (prepare != null) {
// Launch VPN permission dialog
startActivityForResult(prepare, REQUEST_VPN);
} else {
// VPN already prepared, start service
ServiceSinkhole.start("vpn_ready", context);
}
// In onActivityResult, handle VPN approval
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_VPN && resultCode == RESULT_OK) {
ServiceSinkhole.start("vpn_approved", context);
}
}
```
--------------------------------
### Manage Hosts File Downloads
Source: https://context7.com/m66b/netguard/llms.txt
Trigger the hosts file download service via ADB or programmatically using an Intent.
```bash
# Download hosts file via ADB
adb shell am startservice -a eu.faircode.netguard.DOWNLOAD_HOSTS_FILE
# The service downloads from the configured URL (default: StevenBlack hosts)
# Default URL: https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
```
```java
// Programmatically trigger hosts file download
Intent intent = new Intent("eu.faircode.netguard.DOWNLOAD_HOSTS_FILE");
intent.setPackage("eu.faircode.netguard");
context.startService(intent);
```
--------------------------------
### Configure NetGuard SharedPreferences
Source: https://context7.com/m66b/netguard/llms.txt
Modify key NetGuard settings like firewall, ad blocking, DNS, and proxy configurations using SharedPreferences. For per-app rules, use package-specific SharedPreferences files named 'wifi' and 'other'.
```java
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
// Enable/disable firewall
prefs.edit().putBoolean("enabled", true).apply();
// Enable traffic filtering (required for advanced features)
prefs.edit().putBoolean("filter", true).apply();
// Enable ad blocking via hosts file
prefs.edit().putBoolean("use_hosts", true).apply();
// Enable lockdown mode
prefs.edit().putBoolean("lockdown", true).apply();
// Configure custom DNS servers
prefs.edit().putString("dns", "8.8.8.8").apply();
prefs.edit().putString("dns2", "8.8.4.4").apply();
// Enable SOCKS5 proxy
prefs.edit().putBoolean("socks5_enabled", true).apply();
prefs.edit().putString("socks5_addr", "127.0.0.1").apply();
prefs.edit().putString("socks5_port", "9050").apply();
// Per-app Wi-Fi rules (package-specific SharedPreferences)
SharedPreferences wifiPrefs = context.getSharedPreferences("wifi", Context.MODE_PRIVATE);
wifiPrefs.edit().putBoolean("com.example.app", false).apply(); // Block Wi-Fi for app
// Per-app mobile data rules
SharedPreferences otherPrefs = context.getSharedPreferences("other", Context.MODE_PRIVATE);
otherPrefs.edit().putBoolean("com.example.app", false).apply(); // Block mobile for app
```
--------------------------------
### Service Intent for Downloading Hosts File
Source: https://github.com/m66b/netguard/blob/master/ADBLOCKING.md
This is the service intent string used to initiate the download of a hosts file. It can be used with automation tools like Tasker.
```text
eu.faircode.netguard.DOWNLOAD_HOSTS_FILE
```
--------------------------------
### Hosts File Download Service
Source: https://context7.com/m66b/netguard/llms.txt
Service to trigger the download of hosts files for ad blocking (GitHub version only).
```APIDOC
## Hosts File Download Service
### Description
Triggers the download of hosts files for ad blocking. Only available in the GitHub version.
### Action
- `eu.faircode.netguard.DOWNLOAD_HOSTS_FILE`
```
--------------------------------
### Service Control API
Source: https://context7.com/m66b/netguard/llms.txt
Methods to manage the core VPN firewall service lifecycle and rule reloading.
```APIDOC
## Service Control API
### Description
Controls the core VPN firewall service, including starting, stopping, and reloading rules or statistics.
### Methods
- ServiceSinkhole.start(reason, context)
- ServiceSinkhole.stop(reason, context, force)
- ServiceSinkhole.reload(reason, context, interactive)
- ServiceSinkhole.reloadStats(reason, context)
- ServiceSinkhole.run(reason, context)
```
--------------------------------
### Control NetGuard Service via Java
Source: https://context7.com/m66b/netguard/llms.txt
Use the ServiceSinkhole class to manage the firewall service lifecycle and refresh rules or statistics.
```java
// Start the firewall service
ServiceSinkhole.start("app_launch", context);
// Stop the firewall service
ServiceSinkhole.stop("user_request", context, false);
// Reload rules after configuration changes
ServiceSinkhole.reload("settings_changed", context, false);
// Reload with interactive state (screen on/off rules)
ServiceSinkhole.reload("screen_state", context, true);
// Reload network statistics display
ServiceSinkhole.reloadStats("stats_refresh", context);
// Run service without state change (keepalive)
ServiceSinkhole.run("keepalive", context);
```
--------------------------------
### Update NetGuard Widgets Programmatically
Source: https://context7.com/m66b/netguard/llms.txt
Update the main toggle and lockdown toggle widgets programmatically by calling their respective update methods.
```java
// Update all widgets programmatically
WidgetMain.updateWidgets(context);
WidgetLockdown.updateWidgets(context);
```
--------------------------------
### Broadcast Intent API
Source: https://context7.com/m66b/netguard/llms.txt
Broadcast intents for external automation tools to toggle firewall states.
```APIDOC
## Broadcast Intent API
### Description
Exposes broadcast intents for external automation tools like Tasker. Requires signature-level permission `eu.faircode.netguard.permission.ADMIN`.
### Actions
- `eu.faircode.netguard.ON`: Enable firewall
- `eu.faircode.netguard.OFF`: Disable firewall
- `eu.faircode.netguard.LOCKDOWN_ON`: Enable lockdown mode
- `eu.faircode.netguard.LOCKDOWN_OFF`: Disable lockdown mode
```
--------------------------------
### Configure PCAP Traffic Capture
Source: https://context7.com/m66b/netguard/llms.txt
Enable and configure PCAP traffic capture settings using SharedPreferences. The PCAP file is stored in the app's data directory. PCAP can also be controlled programmatically via ServiceSinkhole.
```java
// Enable PCAP capture
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putBoolean("pcap", true).apply();
// Configure PCAP settings
prefs.edit().putString("pcap_record_size", "64").apply(); // Bytes per record
prefs.edit().putString("pcap_file_size", "2").apply(); // Max file size in MB
// The PCAP file is stored at:
// /data/data/eu.faircode.netguard/app_data/netguard.pcap
// Programmatically enable/disable PCAP
ServiceSinkhole.setPcap(true, context); // Enable
ServiceSinkhole.setPcap(false, context); // Disable
```
--------------------------------
### NetGuard Native JNI Interface Methods
Source: https://context7.com/m66b/netguard/llms.txt
Internal native methods used by ServiceSinkhole for efficient packet processing, initialization, and configuration. These methods provide low-level control over network traffic handling.
```java
// Native methods in ServiceSinkhole (internal use)
private native long jni_init(int sdk); // Initialize native context
private native void jni_start(long context, int loglevel); // Start packet processing
private native void jni_run(long context, int tun, boolean fwd53, int rcode); // Main loop
private native void jni_stop(long context); // Stop processing
private native void jni_clear(long context); // Clear state
private native int jni_get_mtu(); // Get MTU value
private native int[] jni_get_stats(long context); // Get session statistics
private static native void jni_pcap(String name, int record_size, int file_size); // Configure PCAP
private native void jni_socks5(String addr, int port, String username, String password); // Configure SOCKS5
private native void jni_done(long context); // Cleanup native resources
```
--------------------------------
### Listen for NetGuard Rule Changes
Source: https://context7.com/m66b/netguard/llms.txt
Register a BroadcastReceiver to listen for local broadcasts indicating changes in NetGuard rules, such as rule updates or command queue modifications. The receiver can update the UI based on connection state and queue size.
```java
// Listen for rule changes
IntentFilter filter = new IntentFilter(ActivityMain.ACTION_RULES_CHANGED);
LocalBroadcastManager.getInstance(context).registerReceiver(receiver, filter);
// Broadcast receiver implementation
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean connected = intent.getBooleanExtra(ActivityMain.EXTRA_CONNECTED, false);
boolean metered = intent.getBooleanExtra(ActivityMain.EXTRA_METERED, false);
// Update UI based on connection state
}
};
// Listen for command queue changes
IntentFilter queueFilter = new IntentFilter(ActivityMain.ACTION_QUEUE_CHANGED);
// EXTRA_SIZE contains the current queue size
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.