Search code examples
javaandroidweb-servicesalarmmanager

How to put in webservice with alarm manager in Android


I don't have any idea on how i will put data from webservice and at the same time it will work in alarm manager to send data every set of time. I know how to put data from webservice but with alarm manager I don't have any idea. I try the process of putting data from webservice but it takes an error because the class has extends BroadcastReceiver. This is how i put in the webservice.

public void passdata(View View){
String a = name.getText().toString();
RequestParams params = new RequestParams();

if(a != null){
params.put("name", a);
WebService(params);
}
}

public void WebService(RequestParams params) {
        progressDialog.show();

        AsyncHttpClient client = new AsyncHttpClient();
        client.get("http://192.168.8.100:8080/taxisafe3/webService/login", params, new AsyncHttpResponseHandler() {
            public void onSuccess(String response) {
                progressDialog.hide();
                try {
                    JSONObject object = new JSONObject(response);

                    if (object.getBoolean("status")) {
                        Toast.makeText(getApplicationContext(), "Login Successful!", Toast.LENGTH_LONG).show();
                        finish();
                        gotoHome();
                    } else {
                        Toast.makeText(getApplicationContext(), "Username or Password is incorrect!", Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(int statusCode, Throwable error, String content) {
                progressDialog.hide();

                if (statusCode == 404) {
                    Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
                } else if (statusCode == 500) {
                    Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Unexpected Error Occured! (No Internet Connection)", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

How can i do this putting of data in webservice with alarm manager? So that I can send data from the WebService every minute for example. Please help.

UPDATED from Joseph Answer:

    public class Web extends IntentService {
    String msg = "aw";
    public Web(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        RequestParams params = new RequestParams();
        params.put("message", msg);
        WebService(params);

        Intent in = new Intent(Web.this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(Web.this, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(calendar.MILLISECOND, 1);
        alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 1000 * 60 * 1, pendingIntent);
    }
    public void WebService(RequestParams params) {

        AsyncHttpClient client = new AsyncHttpClient();
        client.get("http://192.168.254.105:8080/taxisafe3/webService/emergency", params, new AsyncHttpResponseHandler() {
            public void onSuccess(String response) {
                try {
                    JSONObject object = new JSONObject(response);

                    if (object.getBoolean("status")) {
                        Toast.makeText(getApplicationContext(), "Emergency Sent to Server", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "Emergency not Sent to Server", Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(int statusCode, Throwable error, String content) {

                if (statusCode == 404) {
                    Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
                } else if (statusCode == 500) {
                    Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Unexpected Error Occured! (No Internet Connection)", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

Receiver Class

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent)
    {        
        Intent i = new Intent(context, Web.class);
        context.startService(i);
}
}

Solution

  • I find the answer. In AlarmReceiver initialize the Request params then remove the parameters inside the WebService then put the message coming from another class. This is the code.

    public class AlarmReceiver extends BroadcastReceiver {
        RequestParams params = new RequestParams();
        @Override
        public void onReceive(final Context context, Intent intent)
        {
    //This is the message come from another class
    String msg = bundle.getString("mess");
    
    //check if the message is not null
            if(PatternChecker.isNotNull(msg)) {
                params.put("message",msg);
                Webservice();
            }
    }
    
    WebService method here to perform the sending data to the webservice.