I have an android application, when user installs it and when this app is to be run i want to be able to start an service if the device has internet access.
this is my mainfest.xml content for service and my broadcast
<receiver android:name="com.google.audiancelistening.WifiChangedBrodcast" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
For this solution when wifi is turned on this broadcast executes successfully,
but i want start it in first of my application so i added below code in my activity:
if(isInternetOn()){
Intent inten = new Intent(getApplicationContext(), ConnectionService.class);
getApplicationContext().startService(inten);
writeToSDFile();
android.os.Process.killProcess(android.os.Process.myPid());
} else{
writeToSDFile();
android.os.Process.killProcess(android.os.Process.myPid());
}
isInternetOn() method is an method that check internet access like below:
public final boolean isInternetOn() {
ConnectivityManager connec = null;
connec = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if ( connec != null){
NetworkInfo result = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (result != null && result.isConnectedOrConnecting());
return true;
}
return false;
}
but when my activity execute and internet for first is access this can not be able to run successfully and when i turn off and on my wifi service is start and work successfully.
:(
i dont know what is my solution but i dont have any exception.
Related
How to stack photo on Android and sending them after renew internet connection.?
have an application that takes pictures and sends them to the server.
What to do if there is no connection to the server.? I got the requirement for a project to stack photo on phone and sent them immediately after internet connection renew.
Please help anybody cause i don't have any idea for it.
The first step is save the photo locally in case you can't send it immediately. To archieve that, you can create a local sqlite database and save the photo. This steep is a little bit tricky, but you can follow this tutorial to get an idea of how to do that.
http://whats-online.info/science-and-tutorials/129/how-to-store-images-in-SQLite-database-in-Android-and-display-in-listview/
The next step is to listen when the smartphone gets conection to internet. This can be done with a BroadcastReceiver. here i show you how.
in the manifest
.....
<application
android:name=".aplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
...
...
<receiver android:name=".ConectivityChangeReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
...
</application>
And next you implemente your broadcastReceiver
public class ConectivityChangeReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
if (isConnected(context)) {
//if have conection to internet send the photo
}
}
public boolean isConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
}
}
And that is, good luck :D
I am making an android app which will maintain login and logout details of person using wifi connection. Person gets logged in whenever connect to WIFI and logout if connection lost.
I want to track wifi log details of android device and store in database. Is there any solution?? please help!
Add this permission
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Use BroadcastReceiver.
public class WifiReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMan.getActiveNetworkInfo();
if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI)
Log.d("WifiReceiver", "Have Wifi Connection"); // here login
else
Log.d("WifiReceiver", "Don't have Wifi Connection"); // here logout
}
};
and add this in manifest file.
<receiver android:name=".WifiReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
Why on receive called twice, when Network state changed.
Manifest:
<receiver android:name="tv.meterreading.network.NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" >
</action>
</intent-filter>
</receiver>
Receiving multiple broadcast is a device specific problem. Some phones just send one broadcast while other send 2 or 3. But there is a work around:
Assuming you get the disconnect message when the wifi is disconnected, I would guess the first one is the correct one and the other 2 are just echoes for some reason.
To know that the message has been called, you could have a static boolean that gets toggled between connect and disconnect and only call your sub-routines when you receive a connection and the boolean is true. Something like:
public class ConnectionChangeReceiver extends BroadcastReceiver {
private static boolean firstConnect = true;
#Override
public void onReceive(Context context, Intent intent) {
final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null) {
if(firstConnect) {
// do subroutines here
firstConnect = false;
}
}
else {
firstConnect= true;
}
}
}
How can I open my application when an user enters a zone that has wi-fi? Is this possible? Suppose my application is onPause() state (means My Device's homescreen). now when device connected with wifi. it will automatically open my application.
Try add broadcast receiver and listen network changes, when wi-fi connected start your activity. Something like this solution
public class ConnectivityReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (((null != wifi)&&(wifi.isAvailable())) || ((null != mobile)&&(mobile.isAvailable()))){
Intent uplIntent = new Intent(context, YourActivity.class);
uplIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(uplIntent);
}
}
}
And add to manifest
<receiver android:name=".receiver.ConnectivityReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
What I can imagine is an overwriting of the onPause() Method like this:
#Override
public void onPause() {
String conn_context = Context.WIFI_SERVICE;
final WifiManager wifi = (WifiManager) getSystemService(conn_context);
if (wifi.isWifiEnabled())
{
super.onResume();
}
else
{
super.onPause();
}
}
But you must also figure a way to handle the real onPause event.
Maybe doable with the Tasker app from Play Store (not free though). Or you can create a Service (http://developer.android.com/guide/components/services.html) that will have code outlined in the other answers and then launch your app (Activity) when wifi is available.
In my app I'm using internet connection.I want to check internet connection through my application so I'm using broadcast receiver for that.
i have register receiver in manifest but don't know why its not working.Can somebody help....is there any mistake in registering the receiver...i have put log in on receive method to check whether its getting register but that log never get print.and when i register broadcast receiver via code then its working fine...
Here is my code...
`
<receiver android:name="com.android.fishdemo.CheckInternetConnectionChangeReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" />
</intent-filter>
</receiver>`
Well According to Our Discussion ..
You need to Register your Receiver in your activity and make it global so that you can access it any where..
Since you also need to unregister the receiver when ever you are closing the application.
I recommend that you register the receiver in you main activity oncreate method and unregister it in the onKeyDown / onBackKeyPressed Method. when your application will be closed..
ook
Hope this helped
Thanks
sHaH...
Go through this working code for checking networks (both Mobile data and WIFI):
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
this.context = context;
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo WIFINetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
settings = context.getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
email = settings.getString("email", null);
if(mobNetInfo.isConnected() || WIFINetInfo.isConnected()){ // write your code here to handle something}