Should I use a Service or IntentService?

I need to create two Android apps.

App1 - displays a user message (ie, "Hello World") from the user, and App2 displays a message in the Console view through ADB Logcat. A message from App1 must be sent to App2 through Intents. App2 must be a service

I am not sure whether to use Serviceor IntentServicefor App2. If I create a service for App2. Will I be able to use it using Implicit Intent , like this:

Intent serviceIntent = new Intent("com.example.vinitanilgaikwad.app2");
bindService(serviceIntent, myConnection, Context.BIND_AUTO_CREATE);

Could you advise me how I should proceed?

My App1 has the following source code classes.

App1 : DisplayMessageActivity

public class DisplayMessageActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    RelativeLayout layout = (RelativeLayout) findViewById(R.id.content);
    layout.addView(textView);

    Intent serviceIntent = new Intent("com.example.vinitanilgaikwad.app2");

    bindService(serviceIntent, myConnection, Context.BIND_AUTO_CREATE);

    startService(serviceIntent);
  }

  Messenger myService = null;
  boolean isBound;

  private ServiceConnection myConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className,
                                   IBinder service) {
        myService = new Messenger(service);
        isBound = true;
    }

    public void onServiceDisconnected(ComponentName className) {
        myService = null;
        isBound = false;
    }
  };

  @Override
  public void onDestroy() {
    super.onDestroy();
    unbindService(myConnection);
  }

  public void sendMessage(View view) {
    // if (!isBound) return;
    Message msg = Message.obtain();
    Bundle bundle = new Bundle();
    bundle.putString("MyString", "Vinit");
    msg.setData(bundle);
    try {
     myService.send(msg);
    } catch (RemoteException e) {
     e.printStackTrace();
    }
  }
}

App2 .

App2: MessengerService

package com.example.vinitanilgaikwad.app2;

public class MessengerService extends Service {
  class IncomingHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
      Log.i("dddsds","fsdfdsfsfs");
      Bundle data = msg.getData();
      String dataString = data.getString("MyString");
      Toast.makeText(getApplicationContext(),
                    dataString, Toast.LENGTH_SHORT).show();
      Log.d("Me123",dataString);
    }
  }

  final Messenger myMessenger = new Messenger(new IncomingHandler());
    @Override
    public IBinder onBind(Intent intent) {
      return myMessenger.getBinder();
    }
  }

App2: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.vinitanilgaikwad.app2">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:name=".MessengerService"
          >
            <intent-filter>
                <action android:name="com.example.vinitanilgaikwad.app2"></action>
            </intent-filter>
        </service>


    </application>

</manifest>

App1 App2. Adb logcat .

- ? Android.

+4
3

Service IntentService?

:

Tejas Lagvankar . IntentService.

?

  • , . , .

  • IntentService , . , Main Thread . - ( Intent).

?

  • startService().

  • IntentService Intent, , onHandleIntent().

  • IntentService , .

  • , .

  • IntentService .

/

  • .

  • IntentService . , .

?

  • , , , stopSelf() stopService(). ( , ).

  • IntentService , , stopSelf().


UPDATE

?

- , LocalBroadcastManager. , .

, :


NEW UPDATE

@josemgu91, LocalBroadcastManager Activity .

, IPC, Messenger AIDL.

AIDL , , , Messenger, Handler.

:

+7

Service App2.

IntentService , . , - , .... Service.

App2 ( ), bind . :

Intent svc = new Intent();
svc.setComponent(new ComponentName(
   "com.wicked.cool.apps.app2",                    // App2 package
   "com.wicked.cool.apps.app2.svc.App2Service"));  // FQN of App2 service
svc.setStringExtra(STUFF_KEY, someStuff);
startService(svc)

... App1 App2.

, , IntentServices Messengers.

( , )

+2

? IntentService Service , . IntentService - , Intent . , . , , , ( ). , onStartCommand. , IPC (Messenger AIDL).

+1

Source: https://habr.com/ru/post/1648901/


All Articles