I have an application where i need to create socket connection. My requirement is, once my socket connection is established it need to be alive until i personally close it. Every 3 minutes i have to send data packets to the other end. Can anyone provide me some code sample with will help me to do this?
相关问题
- How can I create this custom Bottom Navigation on
- Multiple sockets for clients to connect to
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Socket connections in Android are the same as in Java: http://www.oracle.com/technetwork/java/socket-140484.html
Things you need to be aware of:
Take a look at
AlarmManager
, if you need scheduled execution of your code.Do you need to run your code and receive data even if user does not use the app any more (i.e. app is inactive)?
Here, in this post you will find the detailed code for establishing socket between devices or between two application in the same mobile.
You have to create two application to test below code.
In both application's manifest file, add below permission
activity_main.xml
MainActivity.java
activity_main.xml
MainActivity.java
Simple socket server app example
I've already posted a client example at: https://stackoverflow.com/a/35971718/895245 , so here goes a server example.
This example app runs a server that returns a ROT-1 cypher of the input.
You would then need to add an
Exit
button + some sleep delays, but this should get you started.To play with it:
netcat $PHONE_IP 12345
Android sockets are the same as Java's, except we have to deal with some permission issues.
src/com/cirosantilli/android_cheat/socket
We need a
Service
or other background method or else: How to fix android.os.NetworkOnMainThreadException?AndroidManifest.xml
Must add:
<uses-permission android:name="android.permission.INTERNET" />
or else: Java socket IOException - permission deniedOn GitHub with a
build.xml
: https://github.com/cirosantilli/android-cheat/tree/92de020d0b708549a444ebd9f881de7b240b3fbc/socket