Ricky Khatri Android Coddie
Wednesday 1 March 2017
Andorid marshmallow custom permission code
/**
// Open app settings screen to allow required permissions.
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Util.CAMERA_PERMISSION)) {
Friday 10 February 2017
Network provider checksum for android (phone/tablet)
It is important to check network availability before starting search for network services like jmdns search, network check before doing any kind of network operations.
1) Util class for network check
/**
* This class checks network state in mobile device
*/
public class NetworkUtil {
/**
* The refrence.
*/
public static NetworkUtil refrence = null;
/**
* Method returns class object.
*
* @return single instance of NetworkUtil
*/
public static NetworkUtil getInstance() {
if (null == refrence)
refrence = new NetworkUtil();
return refrence;
}
/**
* Check network state.
*
* @param context the context
* @return true, if successful
*/
public static boolean checkNetworkStatus(Context context) {
boolean hasConnectedWifi = false;
boolean hasConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
//For loop to get all data provide in mobile device
for (NetworkInfo ni : netInfo) {
//If wifi detected then check for connection state
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected()){
hasConnectedWifi = true;
}
//If mobile data detected then check for connection state
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
hasConnectedMobile = true;
}
return hasConnectedWifi || hasConnectedMobile;
}
}
Here is the code to access NetworkUtil class where you have to pass context of activity and will return true/false to show network availability.
boolean isNetworkAvailable=
NetwrokUtil.checkNetworkStatus(context);
Here we done with Network check in android device (phone/tablet).
Thanks...!!!
Friday 27 January 2017
Android JMDNS Search Tutorial.
A simple example to getting start with Zero configuration service integration with your android app. Lets start with some simple steps below:
public class JmdnsUtil {
public static final String SERVICE_TYPE="_http._tcp.local.";
public static final String SERVICE_NAME="AppName";
public static final int PORT=1234;
}
public class JmdnsHelper {
private String[] serviceNames = null;
/**
* This method registers jmdns service
*/
public void registerJmdnsService() {
try {
// Create a JmDNS instance
JmDNS jmdns = JmDNS.create(InetAddress.getLocalHost());
// Register a service
ServiceInfo serviceInfo = ServiceInfo.create(JmdnsUtil.SERVICE_TYPE,
JmdnsUtil.SERVICE_NAME,
JmdnsUtil.PORT, "path=index.html");
jmdns.registerService(serviceInfo);
// Wait a bit
Thread.sleep(100);//25000
//Call discover services method
discoverJmdnsService();
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* This method discovers jmdns services or
* connected devices in local network
*/
public String[] discoverJmdnsService() {
JmDNS jmdns = null;
try {
jmdns = JmDNS.create();
//while (true) {
ServiceInfo[] infos = jmdns.list(JmdnsUtil.SERVICE_TYPE);
serviceNames = new String[infos.length];
for (int i = 0; i < infos.length; i++) {
serviceNames[i] = infos[i].getName();
// Here we can fetch all required details of device.
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (jmdns != null) try {
jmdns.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}
if (serviceNames != null && serviceNames.length > 0) {
return serviceNames;
} else {
return null;
}
}
}
/**
* Jmdns Constructor
*/
public JmdnsService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startSearchTcpIpLocalServices();
return super.onStartCommand(intent, flags, startId);
}
/**
* This method starts searching of devices in local network
*/
private void startSearchTcpIpLocalServices() {
if (NetworkAvailablity.checkNetworkStatus(this)) {
try {
new Thread(new Runnable() {
@Override
public void run() {
//Do network search operation
JmdnsHelper jmdnsHelper = new JmdnsHelper();
final String serviceName[] = jmdnsHelper.discoverJmdnsService();
if (serviceName != null && serviceName.length > 0) {
JmdnsService.this.stopSelf();
} else {
}
}
}).start();
} catch (NullPointerException n) {
n.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}else{
JmdnsService.this.stopSelf();
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
Download JMDNS JAR
1) First of all create a simple JmdnsUtil class:
public class JmdnsUtil {
public static final String SERVICE_TYPE="_http._tcp.local.";
public static final String SERVICE_NAME="AppName";
public static final int PORT=1234;
}
2) In step second create a helper class that will contain all method stuffs:
public class JmdnsHelper {
private String[] serviceNames = null;
/**
* This method registers jmdns service
*/
public void registerJmdnsService() {
try {
// Create a JmDNS instance
JmDNS jmdns = JmDNS.create(InetAddress.getLocalHost());
// Register a service
ServiceInfo serviceInfo = ServiceInfo.create(JmdnsUtil.SERVICE_TYPE,
JmdnsUtil.SERVICE_NAME,
JmdnsUtil.PORT, "path=index.html");
jmdns.registerService(serviceInfo);
// Wait a bit
Thread.sleep(100);//25000
//Call discover services method
discoverJmdnsService();
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* This method discovers jmdns services or
* connected devices in local network
*/
public String[] discoverJmdnsService() {
JmDNS jmdns = null;
try {
jmdns = JmDNS.create();
//while (true) {
ServiceInfo[] infos = jmdns.list(JmdnsUtil.SERVICE_TYPE);
serviceNames = new String[infos.length];
for (int i = 0; i < infos.length; i++) {
serviceNames[i] = infos[i].getName();
// Here we can fetch all required details of device.
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (jmdns != null) try {
jmdns.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}
if (serviceNames != null && serviceNames.length > 0) {
return serviceNames;
} else {
return null;
}
}
}
3) Now create a service which runs in background to search for connected devices or registered services in local network.
public class JmdnsService extends Service {/**
* Jmdns Constructor
*/
public JmdnsService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startSearchTcpIpLocalServices();
return super.onStartCommand(intent, flags, startId);
}
/**
* This method starts searching of devices in local network
*/
private void startSearchTcpIpLocalServices() {
if (NetworkAvailablity.checkNetworkStatus(this)) {
try {
new Thread(new Runnable() {
@Override
public void run() {
//Do network search operation
JmdnsHelper jmdnsHelper = new JmdnsHelper();
final String serviceName[] = jmdnsHelper.discoverJmdnsService();
if (serviceName != null && serviceName.length > 0) {
JmdnsService.this.stopSelf();
} else {
}
}
}).start();
} catch (NullPointerException n) {
n.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}else{
JmdnsService.this.stopSelf();
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
Here we done with simple JMDNS search integration. You can find jmdns jar at the path below.
Thanks...!!!
Subscribe to:
Posts (Atom)
Andorid marshmallow custom permission code
As we all know android has put some restrictions for permission check for security reasons from android M (6.0) Marshmallow. Here we gon...
-
A simple example to getting start with Zero configuration service integration with your android app. Lets start with some simple steps belo...
-
It is important to check network availability before starting search for network services like jmdns search, network check before doing...
-
As we all know android has put some restrictions for permission check for security reasons from android M (6.0) Marshmallow. Here we gon...