应用关闭时从IntentService上传图片

时间:2016-02-21 13:46:30

标签: android service upload alarmmanager intentservice

我正在开发一个将您的本地日期与云同步的应用。所以我需要每10分钟自动检查一下我的本地数据,以便将新的相机文件上传到云端。

所以我使用的IntentService仅在应用程序在前台运行时才有效。如果我关闭它,我的服务不会上传任何内容。我希望我的内容服务在AlarmManager的后台工作。

我的IntentService在Manifest.xml中声明:

    <!-- Uploader and Deleter Files  Service -->
     <service android:name=".receiver.UploadDeleteService"   android:exported="false" />
    <receiver
        android:name=".receiver.AlarmReceiver"
        android:process=":remote" >
    </receiver>

My AlarmReceiver:

public class AlarmReceiver  extends BroadcastReceiver {
    public static final int REQUEST_CODE = 12345;
    public static final String ACTION = "com.codepath.example.servicesdemo.alarm";

    // Triggered by the Alarm periodically (starts the service to run task)
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, UploadDeleteService.class);
        context.startService(i);
    }

}

我的ServiceInteractor,我在AlarmManager中实例化我的AlarmReceiver:

public class ServiceInteractorImpl implements ServiceInteractor {

private Context context;

public ServiceInteractorImpl(Context context){
    this.context = context;
}

@Override
public void launchService() {

    // Construct an intent that will execute the AlarmReceiver
    Intent intent =  new Intent(context, AlarmReceiver.class);
    // Create a PendingIntent to be triggered when the alarm goes off
    final PendingIntent pIntent = PendingIntent.getBroadcast(context, AlarmReceiver.REQUEST_CODE,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
    // Setup periodic alarm every 5 seconds
    long firstMillis = System.currentTimeMillis(); // alarm is set right away
    AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    // First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP
    // Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_DAY
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MINUTE, 10);
    alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis,
            cal.getTimeInMillis(), pIntent);

}

}

我的UploadDeleteService,我调用了改造实施模块:

 public class UploadDeleteService extends IntentService implements ApiConnector.GetObjectListener {

    private RemoteInteractor remoteInteractor;

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

    public UploadDeleteService() {
        super("UpdateDeleteService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.i("SERVICE", "Service running");
        remoteInteractor = new RemoteInteractorImpl(getApplicationContext());
        remoteInteractor.checkNews(this);
    }

    @Override
    public void onImageUploaded(String type, JSONObject response) {
        Log.d("SERVICE", "  onImageUploaded  ");
       //REST OF THE STUFF....
    }
}

我需要一个帮助来解决这个问题。我需要它每10分钟工作一次虽然应用程序已关闭。谢谢!

1 个答案:

答案 0 :(得分:1)

停止服务:

将“cal.getTimeInMillis()”更改为“10 * 60 * 1000”

cal.add(Calendar.MINUTE, 10); //this will add 10 minute to current time

对于服务开始时停止打开的应用

通常它不会打开你的应用程序,你需要检查RemoteInteractorImpl.class中发生了什么

您在onHandleIntent

创建新实例
remoteInteractor = new RemoteInteractorImpl(getApplicationContext());
remoteInteractor.checkNews(this);