0

I have to implement downloading videos during night time or when WiFi is ON in Android Downloading should be done in background even when my App is not open. Please some one let me know how to implement this. I browsed on net but didn't find exact solution Can i implement this using download manager or services? Sharing any piece of code or links will be helpful

krishnamn
  • 151
  • 3
  • 11

1 Answers1

0

=> I think you can go with service to download. as the service needs no UI it will run on background. Services

=> to start your download while WIFI in turned ON; then you can use the help of broadcast receivers Broadcast Receiver

some sample codes:

to Manifest

<receiver android:name="com.AEDesign.communication.WifiReceiver" >
   <intent-filter android:priority="100">
      <action android:name="android.net.wifi.STATE_CHANGE" />
   </intent-filter>
</receiver>


<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

to 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()) {

            //  start your service to download! use your service to download file. 



         }
      }
   }
}
Blue_Alien
  • 2,148
  • 2
  • 25
  • 29
  • This is a nice solution but it only solves one half of the problem (downloading when wifi is on). The other half can be done using a similar approach and having an alarm service (manager) which ticks at nights, which also checks if the wifi is on. Both of these should respect the condition that (1) the wifi is on and (2) it's night time. Since you can lose wifi access when it's night or you can have wifi and it's not night time, you need to listen to both the time events and wifi events and make sure you only run your code when both conditions are met. – kha Jan 21 '15 at 08:18
  • Thanks for the reply bachu...how about download manager ? can i use it to implement this in place of services. – krishnamn Jan 22 '15 at 04:47
  • yes, you can go with download manager, and check this link , it will help you to do the same: http://www.sanfoundry.com/java-android-program-demonstrate-download-manager/ – Blue_Alien Jan 23 '15 at 11:03
  • Thanks for all your replies...sorry for responding late... i go with useing Alaram service and Download manager to achieve this task. – krishnamn Feb 24 '15 at 05:47
  • Hi...how to call a activity method from receiver ? i want to call a method from receiver to start downloading videos . i checked the following links but didn't worked for me. is it possible to call main activity method from receiver ? if not please suggest me how to do this. http://stackoverflow.com/questions/7495686/how-to-call-an-activity-method-from-a-broadcastreceiver , http://stackoverflow.com/questions/16934425/call-an-activity-method-from-a-broadcastreceiver-class – krishnamn Feb 24 '15 at 06:55