Advertisement
hamzaalloush

Service_HTTP_URL.java

May 1st, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. package com.example.hassansbeity.testingsevices;
  2.  
  3. import android.app.IntentService;
  4. import android.content.Intent;
  5. import android.support.annotation.Nullable;
  6. import android.util.Log;
  7.  
  8. import java.io.BufferedInputStream;
  9. import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.InputStreamReader;
  14. import java.io.OutputStream;
  15. import java.io.PrintWriter;
  16. import java.net.HttpURLConnection;
  17. import java.net.URL;
  18. import java.net.URLConnection;
  19.  
  20. /**
  21.  * Created by hassansbeity on 4/23/17.
  22.  * Service_HTTP_URL: Retrieve an HTML page from the internet and store it locally
  23.  */
  24.  
  25. public class Service_HTTP_URL  extends IntentService{
  26.  
  27.     public static final String CONSTANT_COM="com.Sbeyti.URL";
  28.     public Service_HTTP_URL(){super("'HTTP_URL");}
  29.     @Override
  30.     protected void onHandleIntent(@Nullable Intent intent) {
  31.  
  32.         Log.v("ServiceStatus","Service Started");
  33.  
  34.         final String message= intent.getStringExtra("data");
  35.         Thread thread = new Thread(new Runnable() {
  36.             public void run() {
  37.                 // do the work that the service needs to do
  38.                 doInBackground();
  39.                 Intent backIntent= new Intent();
  40.                 backIntent.putExtra("data",message+" Back from Service");
  41.                 backIntent.setAction(CONSTANT_COM);
  42.                 sendBroadcast(backIntent);
  43.             }
  44.         });
  45.         thread.start();
  46.     }
  47.  
  48.     public void doInBackground() {
  49.         URL url;
  50.         HttpURLConnection urlConnection = null;
  51.         File myFile=null;
  52.         PrintWriter pr=null;
  53.         try {
  54.             url = new URL("http://www.android.com/");
  55.             Log.v("ServiceStatus","Trying to Connect");
  56.  
  57.            urlConnection = (HttpURLConnection) url.openConnection();
  58.             Log.v("ServiceStatus","Connected");
  59.  
  60.            InputStream in = urlConnection.getInputStream();
  61.  
  62.             InputStreamReader isw = new InputStreamReader(in);
  63.  
  64.            try {
  65.                myFile = new File(getFilesDir()+"/dataRecieved.txt");
  66.                pr = new PrintWriter(myFile);
  67.             }catch(IOException e){
  68.                 Log.v("ServiceStatus",e.getMessage());
  69.             }
  70.             int data = isw.read();
  71.             Log.v("ServiceStatus","downloading");
  72.             while (data != -1) {
  73.                 char current = (char) data;
  74.                 data = isw.read();
  75.                pr.print(current);
  76.             }
  77.         } catch (Exception e) {
  78.             Log.v("ServiceStatus",e.getMessage());
  79.         } finally {
  80.             if (urlConnection != null) {
  81.                 urlConnection.disconnect();
  82.                 pr.close();
  83.            }
  84.        }
  85.  
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement