当前位置: 动力学知识库 > 问答 > 编程问答 >

android intent service in loop

问题描述:

I want to send data to server using intent service. What I have done is I start the intent service and in that class I take the data from local db and in a for loop I send the data to the server. It works. But my question is ,should I use for loop to start the intent service i.e. for example

public void onReceive(Context context, Intent intent) {

for (int i=0;i<5;i++){

Intent startService = new Intent(context, SampleService.class);

startWakefulService(context, startService);

}

}

I am using an alarm manager to trigger the service. What is the correct way? please help. Thanks! . This is my code ` public class ScheduleAlarmReciever extends WakefulBroadcastReceiver{

private AlarmManager alarmManager;

private PendingIntent pendingIntent;

final int REQUEST_CODE=10;

SessionManager manager;

@Override

public void onReceive(Context context, Intent intent) {

manager=new SessionManager(context);

Log.e("ScheduleAlarmReceiver","New intent");

Log.e("ScheduleAlarmReceiver","Service is set");

Intent startService = new Intent(context, SampleService.class);

startWakefulService(context, startService);

}

public void setAlarm(Context context){

Intent intent=new Intent(context,ScheduleAlarmReciever.class);

pendingIntent=PendingIntent.getBroadcast(context,REQUEST_CODE,intent, PendingIntent.FLAG_UPDATE_CURRENT);

long firstMillis=System.currentTimeMillis();

long INTERVAL_IN_MILLIS=2*60*1000; //two minutes interval

alarmManager= (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,firstMillis,INTERVAL_IN_MILLIS,pendingIntent);

}

}

this is how i am setting the alarm alarmReciever.setAlarm(MainActivity.this);and this is my intent service class public class SampleService extends IntentService{

/**

* Creates an IntentService. Invoked by your subclass's constructor.

*

* @param name Used to name the worker thread, important only for debugging.

*/

AsyncHttpClient asyncHttpClient;

DbUtil dbUtil;

NotificationManager notificationManager;

NotificationCompat.Builder mBuilder;

int id=1;

public static final String API="http://customer-data.com/api/insertblevital";

public SampleService() {

super("sampleservice");

}

@Override

public void onCreate() {

super.onCreate();

asyncHttpClient=new SyncHttpClient();

dbUtil=DbUtil.getInstance(getApplicationContext());

notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

}

@Override

protected void onHandleIntent(Intent intent) {

if(0!=dbUtil.getAllData().size()) {

mBuilder= new NotificationCompat.Builder(getApplicationContext());

mBuilder.setContentText("Uploading data")

.setContentTitle("Sample Upload")

.setSmallIcon(R.drawable.ic_launcher);

mBuilder.setProgress(0, 0, true);

notificationManager.notify(id,mBuilder.build());

for(int i=0;i<dbUtil.getAllData().size();i++){

Log.e("Sample email",dbUtil.getAllData().get(i).getEmail());

}

ArrayList<Sample> sampleArrayList = dbUtil.getAllData();

for (Sample sample : sampleArrayList) {

postData(sample);

}

mBuilder.setContentText("upload Complete");

mBuilder.setProgress(0, 0, false);

notificationManager.notify(id,mBuilder.build());

}

}

public void postData(Sample sample){

RequestParams requestParams=new RequestParams();

requestParams.add("email",sample.getEmail());

requestParams.add("Name",sample.getName());

requestParams.add("Height",sample.getHeight());

requestParams.add("Gender",sample.getGender());

asyncHttpClient.post(API,requestParams,new PostResponseHandler(sample.getEmail()));

}

public class PostResponseHandler extends TextHttpResponseHandler{

String email;

public PostResponseHandler(String email){

this.email=email;

}

@Override

public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

Log.e("failure","failure");

}

@Override

public void onSuccess(int statusCode, Header[] headers, String responseString) {

Log.e("responseString",responseString);

ArrayList<String> stringArrayList=new ArrayList<>();

stringArrayList.add(email);

dbUtil.UpdateFlag(stringArrayList);

}

}

}

`

网友答案:

An alarm manager to trigger the service is right way to do this. Why are you use for loop ?

分享给朋友:
您可能感兴趣的文章:
随机阅读: