How To Build A File Sharing Android App Like Xender - Hive Programmers
Greetings to my favorite science community online, StemSocial.
It's @skyehi and in today's episode on my series Android App Development Tutorials for Beginners, we're going to learn how a file sharing App like Xender or Flash Share is built.
Original Image Source by RosZie from Pixabay
Xender is a file sharing App on Both android and IOS where the users can share files using WIFI.
WiFi networks seem stronger than Bluetooth with a wider area range which makes sharing files very quick and convenient. This is what makes Xender so successful.
People like to share their files really quickly. In today's tutorial, I wanted to share with readers and followers a model of such an App.
After this tutorial, you would know about some of the basics APIs and logic used in such an App.
Of course I didn't hack the App to show you this guys. This is merely a model on how such an App can be developed.
I hope you're excited about this, let's get started with our work for the day shall we.
Prerequisites
For the benefit of newcomers to Android App development or my series, I'll share with you the required softwares you need to be able to go through with this tutorial.
Android Studio IDE - the platform on which we'll develop this and all other Android Apps.
Java Development Kit, JDK - You need to install JDK correctly on your PC for your PC to be able to execute Java commands or codes.
Physical Android Device or Emulator - After building the Files sharing App, we'll need to test it either on an emulator or a physical Android device.
WiFi Direct Support - Please make sure that your Android device or emulator supports WiFi Direct. Of course most modern devices have this feature.
If you have all these checked out, you're absolutely ready to go through with this tutorial
Creating a new Android Project Setup
Now that we're ready to develop our WiFi file sharing App, we'll start by opening the Android Studio application and clicking on "Create A New Project".
You will have a few project template options to choose from. Please select "Empty Activity" as the template of your project.
I always recommend this template in my series to keep things simple and basic for beginners
When you're through with selecting the template, click on the "Next" button and set both the App name and package name of your App. I'll recommend using a very unique name like Xender did.
As always guys, since we're still using Java Programming language as the language for our backend, please ensure that Java is selected and not Kotlin.
When you're satisfied with the project configuration, click on the "Finish" button and wait for Android Studio to load or prepare your new project space.
Requesting Permissions
Now guys, since our file sharing App would be using WiFi service of the Android device for file sharing, we need to write the code to request permission to access the WiFi service.
We would also request permissions like the internet access and WiFi state.
Of course our permission request code will be written inside our AndroidManifest.xml
file. So go ahead and open that file and include the necessary permissions for the App.
Here's how your Permission request codes should look like
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
These permissions are very important and are required for accessing WiFi state and enabling WiFi on the device. Without these permissions, our App would not work at all.
Setting Up WiFi Direct in our App
Now guys we need to write the code to set up WiFi direct feature in our App so our App could use the WiFi to transfer or share files with other devices.
Thankfully, Android Studio has inbuilt APIs for WiFi Direct features. In our WiFi Direct code we'll include APIs like WifiP2pConfig
and WifiP2pManager
The WiFi Direct setup up will be done inside our MainActivity.java
file.
Here's how your code should look like
// Add necessary imports at the beginning of your file
import android.net.wifi.p2p.WifiP2pConfig;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
public class MainActivity extends AppCompatActivity {
private WifiP2pManager wifiP2pManager;
private Channel channel;
private BroadcastReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize WiFi Direct Manager and Channel
wifiP2pManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
channel = wifiP2pManager.initialize(this, getMainLooper(), null);
// Set up broadcast receiver for device discovery
receiver = new WiFiDirectBroadcastReceiver(wifiP2pManager, channel, this);
registerReceiver(receiver, getWifiDirectIntentFilter());
}
// ... (Other methods and UI setup)
}
As you can see in the above code, it's basically a model code I wrote and you are free to include other methods and User Interface set-up.
This is ideally the foundation on which Apps like Xender and Flash Share are built.
Device Discovery and Connection
After setting up the WiFi Direct feature, we also need to work on Device Discovery.
This is more like the part where your file sharing App scans for available devices and you get to pick which device you want to share a file with or receive a file from.
Thanks to the WiFi connection speeds, it's usually very quick. Modern file sharing Apps now heavily rely on barcode scanners which seem even quicker to choose the device you want to connect to.
However in our basic file sharing App, we would implement a simple device discovery and connection feature that would use WiFi to connect to devices that would be sharing files amongst themselves.
To do this, we have to create an entire java class for broadcast receiving.
So I created a Java class and named it WiFiDirectBroadcastReceiver.java
file
I believe this may be the first time I'm introducing the BroadcastReceiver
class in this series. Don't worry guys, I'll be making a dedicated post on broadcast receivers pretty soon.
Here's how your code should look like
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.p2p.WifiP2pDeviceList;
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
private WifiP2pManager wifiP2pManager;
private WifiP2pManager.Channel channel;
private MainActivity activity;
public WiFiDirectBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel, MainActivity activity) {
super();
this.wifiP2pManager = manager;
this.channel = channel;
this.activity = activity;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// Handle WiFi Direct state changes
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
// Request available peers from the WiFi P2P manager
if (wifiP2pManager != null) {
wifiP2pManager.requestPeers(channel, activity.peerListListener);
}
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
// Handle connection changes
if (wifiP2pManager == null) {
return;
}
NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
if (networkInfo.isConnected()) {
// Connected to another device
wifiP2pManager.requestConnectionInfo(channel, activity.connectionInfoListener);
} else {
// Disconnected
}
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
// Handle this device's configuration changes
}
}
}
File Selection
As a model App, I decided to just show you the steps involved. The next thing you would want to work on is a file selection code, where the user can have access to their storage and select the files.
To avoid making this blog way too long, I decided to just briefly list the steps with everyone.
We're be working on File selection codes tomorrow guys, so please stay tuned if your intention is to build a complete file sharing App.
So in this step, you have to write the code that would allow users to select files for sharing. When you're through with it, you can update your MainActivity.java
:
File Transfer Logic
The next thing you'll work on after the file sharing step is to implement the logic for sending and receiving files. When you're through with that, you will also have to update your MainActivity.java
This is more like part 1 of building a file sharing App like Xender. If I had to put all into one blog post it would be ridiculously long so I have decided to do a part two of this tomorrow guys, so definitely stay tuned if you want to complete the App.
However, this is a great and complete model of a file sharing App which uses WiFi Direct for sharing files. Of course, images, videos and audio can be shared across different operating systems like from Android to iOS or vice versa.
However sharing Apps will not be possible because it's different operating systems. If you even manage to get an APK to an iPhone, you can't really use that file for anything. It simply would not install.
Thank you so much for taking the time to read today's blog. We're getting really close to intermediary levels of this series.
Soon I'll have to separate these blogs into different parts like I did today because my objective is to help beginners develop full apps.
We still have more basic programming to do so stay tuned for more guys.
Have A Great Day And Catch You Next Time On StemSocial. Goodbye 👨💻
You Can Follow Me @skyehi For More Like This And Others
Thanks very helpful
Always welcome 🥰
Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!
Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).
Thanks for including @stemsocial as a beneficiary, which gives you stronger support.