24 Hour Android app connection with PHP web server

*爱你&永不变心* 提交于 2020-01-06 06:59:54

问题


My goal is to download some information from a PHP web server. I have created a service to run 24 hours. App start this service using alaram manager class.

    Intent ishintent = new Intent(this, AlarmReceiver.class);
    PendingIntent pintent = PendingIntent.getBroadcast(this, 2523, ishintent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 15000, pintent);

AlarmReceiver.class

public class AlarmReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        SharedPreferences settings = context.getSharedPreferences("local", 0);
        boolean TaskEnabled = settings.getBoolean("taskenabled", false);
        if (!TaskEnabled) {
            Intent myService = new Intent(context, ServiceClass.class);
            context.startService(myService);
        }
    }
}

Service class have an AsyncTask which download data from internet using HttpURLConnection.

@Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

            new DownloadInfo().execute("http://example.com/xxx.php");
    }

Class DownloadInfo

 class DownloadInfo extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... url) {
            // constants
            try {

                System.setProperty("http.keepAlive", "false");
                HttpURLConnection connection = (HttpURLConnection) new URL(url[0]).openConnection();
                connection.setDoOutput(true);
                connection.setConnectTimeout(30000); // miliseconds
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Connection", "Keep-Alive");
                connection.setRequestProperty("Charset",  "UTF-8");
                connection.setRequestProperty("Content-Type",
                        "application/x-www-form-urlencoded;charset=" + "UTF-8");
                Uri.Builder builder = new Uri.Builder()
                        .appendQueryParameter("user", "1111")
                        .appendQueryParameter("pass", "1111");
                String query = builder.build().getEncodedQuery();
                OutputStream os = connection.getOutputStream();
                BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(os, "UTF-8"));
                writer.write(query);
                writer.flush();
                writer.close();
                os.close();
                InputStream is = connection.getInputStream();
                BufferedReader rd = new BufferedReader(new InputStreamReader(is));
                StringBuffer response = new StringBuffer();
                String line = "";
                while((line = rd.readLine()) != null) {
                    response.append(line);
                    response.append('\r');
                }
                rd.close();
                line = response.toString();
                return line;
            } catch (Exception e) {
                Log.w("MyApp", "Download Exception : " + e.toString());
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {

                if (result != null) {

                }
        }

I am observing this service from last 53 hours. Service is running fine (Settings>app>Running). I have tried different things to ensure the 24 hour connectivity of app which includes turning WiFi radio off and on, turning my WiFi router on and off, plugging out internet cable from router and then putting it back in. App was working fine. But after 28 hours when I checked my app, I found that app is not downloading data from internet. It seems that app was trying to connect with internet but after time out it downloaded nothing. I have checked the web page which app was trying to access via web browser of android device, it was running fine. I have turned wifi radio off and on and app immediately get connected with internet. Now what I can do to ensure that my app keeps it connection established with web server?

Update

AsyncTask download some information which is no more than 160 bytes in form of text. We have poultry business so this information contains new rates of local area.

App starts this service at regular intervals (15 sec) by scheduling task using AlaramManager class.

来源:https://stackoverflow.com/questions/31846657/24-hour-android-app-connection-with-php-web-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!