0

I wrote some application that take some folder that exist in my android phone and compressed the folder - and send it to some FTP server.

This application is work with no problem but this application contain GUI and to send activate it i need to press on some button on the activity.

But now, i want to write some other application and will run as service and will do the same - that mean that the service will run every 3 hours and will compressed some folder and will upload the compressed folder to the ftp.

I don't know how to do it ...

  1. How to create this service that will run with the phone is start
  2. How to upload to the FTP ? i could not upload to FTP without doing it from an Activity.

Thanks for any help.

Yanshof
  • 9,659
  • 21
  • 95
  • 195

2 Answers2

1

you need to use alarm manger for periodic call a service, here is your answer how to use alarm manger with service

http://khurramitdeveloper.blogspot.in/2013/06/android-alarm-manager-to-start-service.html

for start boot service here

http://khurramitdeveloper.blogspot.in/2013/06/start-activity-or-service-on-boot.html

Hardik
  • 17,179
  • 2
  • 35
  • 40
1

I assume you know how to create a service, but not sure how to start it up on boot up. This is how the solution is broken up:

  1. Register receiver to receive the boot up broadcast.
  2. From the receiver, start the service.
  3. Inside the service, use Alarm Manager to register for periodic wake ups.
  4. On being woken up by the Alarm Manager, run the file upload code.

To start after boot up:

First you have to create a receiver:

    public class AfterBootReceiver extends BroadcastReceiver {

       final static String TAG = "BootCompleted";

       @Override
       public void onReceive(Context context, Intent arg1) {
       Log.w(TAG, "about to start service...");
       context.startService(new Intent(context, YourService.class));
       //This line starts your service.
       }
    }

Then give the service the permission to start at boot up, in the Manifest.xml and register the above receiver.

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

   <receiver android:name=".AfterBootReceiver" >
       <intent-filter>
             <action android:name="android.intent.action.BOOT_COMPLETED" />
       </intent-filter>
   </receiver>

Don't put your app on the sd card if you want it to start at boot

To run periodic code in the service:

For the periodicity, you can use the Alarm Manager.

Examples: Start service every hour , Using alarm manager instead of timer task

Then, in the periodic code, the file upload task:

FTP file upload example

sanjeev mk
  • 4,276
  • 6
  • 44
  • 69
  • I think this solution requires the application to have GUI and service modes. (Cf. http://stackoverflow.com/questions/8492707/android-start-alarm-service-immediately) – user2768 Nov 12 '15 at 11:03