Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.IntentService;
- import android.content.Intent;
- import android.support.annotation.Nullable;
- import android.support.v4.content.LocalBroadcastManager;
- import android.util.Log;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.MalformedURLException;
- import java.net.URL;
- public class MyService extends IntentService {
- /**
- * Creates an IntentService. Invoked by your subclass's constructor.
- */
- public MyService() {
- super("MyService");
- }
- @Override
- protected void onHandleIntent(@Nullable Intent intent) {
- try {
- URL myUrl = new URL("https://opentdb.com/api.php?amount=1&category=32&difficulty=hard&type=multiple");
- InputStream stream = myUrl.openConnection().getInputStream();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
- StringBuilder builder = new StringBuilder();
- String line;
- while ((line = bufferedReader.readLine()) != null) {
- builder.append(line).append('\n');
- }
- LocalBroadcastManager localBroadcastManager =LocalBroadcastManager.getInstance(this);
- Log.d("Service", "onReceive: Avem intrebarea!");
- Intent myIntent = new Intent("send_intrebare");
- myIntent.putExtra("intrebare", builder.toString());
- localBroadcastManager.sendBroadcast(myIntent);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement