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
Related
I have a printer inspection app on android phone, its basic inspection form
Inspector can work on a printer inspection even if he has no internet connection,
Once the phone is back with reception/internet, I would like to submit the inspection.
I was thinking to design the app using an android service
so it will save the inspection details using sqlite, then when there is internet connection to resubmit the inspection .
But this require service to periodically check for internet. and will consume significant battery juice.
Is there a hook I can register for my app to notify the app or the service on internet connection?
Simple check for both Wi-fi and Mobile internet as follows...
in Manifest.xml :
<receiver android:name=".com.yourapp.ConnectivityChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
make a new BroadcastReceiver :
public class ConnectivityChangeReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, final Intent intent) {
if(checkInternet(context))
{
Toast.makeText(context, "Network Available Do operations",Toast.LENGTH_LONG).show();
}
}
boolean checkInternet(Context context) {
ServiceManager serviceManager = new ServiceManager(context);
if (serviceManager.isNetworkAvailable()) {
return true;
} else {
return false;
}
}
}
and finally ServiceManager class :
public class ServiceManager {
Context context;
public ServiceManager(Context base) {
context = base;
}
public boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
}
** Don't forget to add permission to use the Internet in your manifest file :
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
Also check out this super cool article on vogella.com AndroidServices ...
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>
I'm trying to make an android application which listens to wifi change broadcast and do some action. But the problem is that it is not working when the process is killed. I found this question which says that it won't work without an activity
How to create BroadcastReceiver without Activity/Service?
So that is not an alternative. Hence I created an activity which is empty. I don't want to create a service which keeps running in the background. How can I make my application keep listening even when it is killed. I have registered the broadcast in the manifest.
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.background.service.BroadCastReceiver">
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
This is my class
public class BroadCastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//do some action
}
}
Looks like you have it correct defining in the manifest with one exception. The broadcast receiver will trigger an ANR if it doesn't complete in 10 secs. http://developer.android.com/training/articles/perf-anr.html
in your broadcast receiver simply start a service.
public class ReceiverUpload extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, ServiceUploader.class));
}
}
Then in your service start an asynctask because you don't want to be on the UI thread for example you start the same service from an activity in your app.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// start asynctask to get off this thread
new MyBackGround().execute(startId);
return Service.START_REDELIVER_INTENT;
}
First thing to do in the asynctask is check for wifi
Below is excerpt from a function I call to check network if it returns false the asynctask just finishes if it's true it does network stuff which hey has to be in the background anyways so asynctask makes even more sense.
// check for network connection
ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connMgr == null) {
return false;
}
// check ok to process
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo == null || !networkInfo.isConnected()) {
return false;
}
boolean isWifi = networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
if (!isWifi) {
return false;
}
return true;
Note in this example the startId passed to the asynctask is used to cancel OS redelivering the intent.
#Override
public void onPostExecute(Integer startId) {
if (startId != null) {
stopSelfResult(startId);
}
}
You already doing right with the Broadcast Receiver and declaring it the Manifest. That's all you need to do. No services running in the background are needed.
Just make sure you install and run the app at least once otherwise the broadcast receives won't be registered
The best that worked for me:
AndroidManifest
<receiver android:name="com.AEDesign.communication.WifiReceiver" >
<intent-filter android:priority="100">
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</receiver>
BroadcastReceiver class
public class WifiReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if(info != null) {
if(info.isConnected()) {
// Do your work.
// e.g. To check the Network Name or other info:
WifiManager wifiManager=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = wifiInfo.getSSID();
}
}
}
}
Permissions
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
best solution is to make a broadcast receiver , it will work
public class NetworkChangeReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, final Intent intent) {
final ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isAvailable() || mobile.isAvailable()) {
// Enjoy coding here
Log.d("Netowk Available ", "Flag No 1");
}
}}
in manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastreceiverforinternetconnection"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
</application>
Normally, if app is killed and broadcast comes your app process will be started by system for you. There is absolutely no need to keep something alive or do any additional manipulations (otherwise why would you need BroadcastReceiver if you can just use Service?)
Updated answer 05/2022
The docs on Broadcasts and Broadcast limitations inform that from Android 7 (Android 8 made it even more stringent)
you cannot use the manifest to declare a receiver for implicit broadcasts (broadcasts that do not target your app specifically), except for a few implicit broadcasts
That means, for most use cases manifest-declared Broadcast won't work anymore and must be replaced with scheduled jobs or services. Be aware, the latter suffers from being killed by the OS for battery optimization.
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.
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.