Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Service vs Intent Service

The Service can be used in tasks with no UI, but shouldn't be too long. If you need to perform long tasks, you must use threads within Service.

The IntentService can be used in long tasks usually with no communication to Main Thread. If communication is required, can use Main Thread handler or broadcast intents. Another case of use is when callbacks are needed (Intent triggered tasks
Comment

Service vs IntentService.

package com.journaldev.androidintentservices;

import android.app.IntentService;
import android.content.Intent;
import android.os.SystemClock;
import android.support.v4.content.LocalBroadcastManager;

public class MyService extends IntentService {

    public MyService() {
        super("MyService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        String message = intent.getStringExtra("message");
        intent.setAction(MainActivity.FILTER_ACTION_KEY);
        SystemClock.sleep(3000);
        String echoMessage = "IntentService after a pause of 3 seconds echoes " + message;
        LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent.putExtra("broadcastMessage", echoMessage));
    }
}
Comment

Service vs IntentService.

package com.journaldev.androidintentservices;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView textView;
    Button button;
    EditText editText;
    MyReceiver myReceiver;

    public static final String FILTER_ACTION_KEY = "any_key";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
        button = findViewById(R.id.button);
        editText = findViewById(R.id.inputText);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message = editText.getText().toString();
                Intent intent = new Intent(MainActivity.this, MyService.class);
                intent.putExtra("message", message);
                startService(intent);
            }
        });
    }

    private void setReceiver() {
        myReceiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(FILTER_ACTION_KEY);

        LocalBroadcastManager.getInstance(this).registerReceiver(myReceiver, intentFilter);
    }

    @Override
    protected void onStart() {
        setReceiver();
        super.onStart();
    }

    @Override
    protected void onStop() {
        unregisterReceiver(myReceiver);
        super.onStop();
    }

    private class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String message = intent.getStringExtra("broadcastMessage");
            textView.setText(textView.getText() + "
" + message);
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java compare strings alphabetically 
Java :: rewrite file java 
Java :: how to truncate decimals in java 
Java :: java split for multiple characters 
Java :: java print two dimensional array 
Java :: how to count an replace substring string in java 
Java :: create a toast message in android 
Java :: java get current time in seconds 
Java :: what is the use of println 
Java :: java while schleife 
Java :: java reverse loop 
Java :: get random word from array java 
Java :: java array to arraylist 
Java :: get type of variable java 
Java :: how to convert bitmap to uri in android 
Java :: Loop Structure in Java 
Java :: convert int to string java 
Java :: Error inflating class ImageView 
Java :: cosinus-1 java 
Java :: java selenium send keys number 
Java :: java encrypt string 
Java :: javafx how to change shape color 
Java :: java isolate the numbers from string 
Java :: float maximum value java 
Java :: java make all interfaces implement another 
Java :: java Modulo 10^9+7 (1000000007) 
Java :: java home mac 
Java :: linux command to see all the java version installed 
Java :: for loop java 
Java :: java regex 10 digit number 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =