0

I'm building an enterprise application that needs to get some information about the employees' rooted phones to do corporate management. This thread needs to run each five minutes. I'm using an Activity that is started by a broadcast(BOOT_COMPLETED) when the android boots up, and it starts an infinite thread to send this information to server. My current problem is my application is being killed by android after the user opens a lot of others apps. What would be the better way to keep a thread running in background to send this information to server?

Main Application Class

public static void startService(Context mContext){

    try{
        //Schedule Service.
        scheduleService(mContext);

        //Call onUpdate.
        onUpdate();

    }catch (Exception o){
        Utilities.log(o.toString());
    }

}



public static void scheduleService(Context mContext){

    try{

        final int NOTIFICATION_INTERVAL = 5 * 60 * 1000;
        Intent mIntent = new Intent(mContext, ServiceReceiver.class);
        AlarmManager mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);

        PendingIntent mPendingIntent = PendingIntent.getBroadcast(mContext, 1, mIntent, 0);

        mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), NOTIFICATION_INTERVAL, mPendingIntent);

    }catch (Exception o){
        Utilities.log(o.toString());
    }
}

ServiceReceiver

public class ServiceReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context mContext, Intent intent) {

    Utilities.log("Service Received");

    //Start Service.
    MyApplication.startService(mContext);

 }
}

AndroidManifest

<receiver
 android:name=".BootUpReceiver"
 android:enabled="true">
 <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED"/>
    <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    <action android:name="android.intent.action.REBOOT"/>

    <category android:name="android.intent.category.DEFAULT"/>
 </intent-filter>
</receiver>


<receiver android:name=".ServiceReceiver"/>    

BootUpReceiver

public class BootUpReceiver extends BroadcastReceiver
{ 

     public void onReceive(Context mContext, Intent mIntent){

         Utilities.log("BootUp Received.");

        //Start Service.
        MyApplication.startService(mContext);
     }
}
Fábio Filho
  • 111
  • 1
  • 10
  • Why don't you use a service? – Hermann Klecker Oct 30 '15 at 18:25
  • use sync adapter or service – Vishal Gaur Oct 30 '15 at 18:33
  • but can i use service to start an infinite thread ? – Fábio Filho Oct 30 '15 at 18:45
  • if the user opens a lot of apps, that thread will be killed, right? how can i run it again? – Fábio Filho Oct 30 '15 at 18:47
  • Create a sticky service . If this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service. http://stackoverflow.com/questions/9093271/start-sticky-and-start-not-sticky – Vishal Gaur Oct 30 '15 at 18:52
  • 1
    Don't use an "infinite" thread - use a [repeating alarm](http://developer.android.com/training/scheduling/alarms.html) to schedule your service to run every 5 mins. – adelphus Oct 30 '15 at 19:06
  • i tried to use the Service with START_STICKY return, some minutes passed but the service wasn't restarted yet. – Fábio Filho Oct 30 '15 at 20:30

2 Answers2

3

create a static broadcast receiver for Repeating Alarms and start Intent Service from broadcast don't use infinite Thread

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        NotificationStatus.setupNotification(context); // if you restart your phone
    }

}

class NotificationStatus{
    //Call only one time from app from any activity
    public static void setupNotification(Context context) {
        final int NOTIFICATION_INTERVAL = 5 * 60 * 1000;
        Intent myIntent1 = new Intent(context, NotificationReceiver.class);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 1, myIntent1, 0);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), NOTIFICATION_INTERVAL, pendingIntent1);

    }
}


public class NotificationReceiver extends BroadcastReceiver {
    private static final int mNotificationId = 0;
    @Override
    public void onReceive(Context context, Intent intent) {
        //start your services here for sending data
Intent intent1 = new Intent(context, SyncService.class);
    context.startService(intent1);
    }
}



public class SyncService extends IntentService {

public SyncService(String name) {
    super(name);
}

@Override
protected void onHandleIntent(Intent intent) {
    //Write code here for sending data to server
}

}

AndroidManifest

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

Define Service in Manifest

<service android:name=".SyncService"/>
Vishal Gaur
  • 658
  • 6
  • 18
0

You need to make your application an Android service.

JJF
  • 2,681
  • 2
  • 18
  • 31