我应该使用Service还是IntentService?

4

我需要创建两个安卓应用程序。

App1 - 由用户输入消息(例如“Hello World”),App2 将消息通过 Intents 发送到 App2 并在可通过 ADB Logcat 查看的控制台中打印出来。App2 应该是一个服务。

我不确定是否应该为 App2 使用 Service 还是 IntentService。如果我为 App2 创建一个服务,我是否能够像这样使用隐式 Intent

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

请问你能指导我如何继续吗?

我的应用1(App1)有以下源代码类。

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中的Service建立连接,Adb logcat没有输出该信息。

有人能帮忙吗?我是Android开发新手。

3个回答

7

我应该使用Service还是IntentService?

来自José Juan Sánchez答案

Tejas Lagvankar在这篇文章中写了关于此主题的内容。下面是Service和IntentService之间的一些关键区别。

何时使用?

  • Service可用于没有UI的任务,但不应过长。如果需要执行长时间任务,则必须在Service内使用线程。

  • IntentService通常用于无需与主线程通信的长任务。如果需要通信,则可以使用主线程处理程序或广播意图。另一种使用情况是需要回调(由Intent触发的任务)。

如何触发?

  • 通过调用startService()方法触发Service

  • 使用Intent触发IntentService,它会生成一个新的工作线程,并在该线程上调用onHandleIntent()方法。

触发自

  • ServiceIntentService可以从任何线程、活动或其他应用程序组件触发。

运行在

  • Service后台运行,但运行在应用程序的主线程上。

  • IntentService运行在单独的工作线程上。

限制/缺点

  • Service可能会阻塞应用程序的主线程。

  • IntentService无法并行运行任务。因此,所有连续的意图将进入工作线程的消息队列,并按顺序执行。

何时停止?

  • 如果您实现了一个Service,则需要通过调用stopSelf()stopService()来停止服务的工作(如果您仅想提供绑定,则不需要实现此方法)。

  • IntentService在处理完所有启动请求后停止服务,因此您永远不需要调用stopSelf()


更新

解决问题的最佳方法?

如果Activity或其他组件想要与服务进行通信,可以使用LocalBroadcastManager。服务可以通过本地广播发送消息,该消息将被Activity接收。

详情请阅读以下内容,这些应该会对您有所帮助:

  1. 与服务通信
  2. IntentService

新更新

正如@josemgu91所说,LocalBroadcastManager只能用于同一应用程序中的Activity和服务。

我们可以通过MessengerAIDL与其他应用程序或进程进行通信,称为IPC。

由于使用AIDL传递数据相当繁琐冗长,因此如果需要绑定通信,则更有效率的方法是使用方便的Messenger系统将绑定器包装成易于使用的Handler对象。

详情请阅读以下内容:

  1. 绑定服务
  2. 使用Messenger(远程绑定服务)进行Android进程间通信(IPC)

我在我的App2中使用了Service。然而,我认为我无法从App1连接到我的服务。你能帮忙吗? - Vinit Gaikwad
该服务在另一个进程(另一个应用程序)中运行,因此无法使用LocalBroadcastManager。 - josemgu91
@josemgu91 很好的发现,我从问题中得出了错误的解释。 - ישו אוהב אותך

2
你应该在App2中使用一个“Service”。
使用“IntentService”的唯一原因是在UI线程之外的线程上执行工作。根据你的帖子,你的要求是进程间通信,而不是工作线程。所以,你应该只使用一个“Service”。
如果你只是向App2发送信息(不期望返回),也没有必要“bind”其服务。只需使用:
Intent svc = new Intent();
svc.setComponent(new ComponentName(
   "com.wicked.cool.apps.app2",                    // App2's package
   "com.wicked.cool.apps.app2.svc.App2Service"));  // FQN of App2's service
svc.setStringExtra(STUFF_KEY, someStuff);
startService(svc)

如何从App1向App2发送一个意图。

无需使用意图过滤器、广播管理器、意图服务或信使。

(已添加显式意图)


你好,我需要有隐式意图对吧?如何在App1中使用App2Service.class呢? - Vinit Gaikwad
当然不行。让我修改一下答案。我认为Intent仍然应该被称为“显式”。 - G. Blake Meike

1
你想要做什么样的任务?IntentService和Service都可以处理你想要执行的Intent。IntentService是一种特殊的服务,当接收到一个Intent时,它会在另一个线程中执行任务,然后停止自己。如果你想要执行一个简单的一次性任务,那么可以使用它。另一方面,Service保持活动状态取决于你如何执行它(绑定或非绑定服务)。如果你想要更灵活的方法,那么可以使用Service并在onStartCommand方法中处理Intents。如果你想要处理复杂数据类型的更加灵活的方法,那么就需要使用IPC技术与你的服务进行通信(Messenger或AIDL)。

请帮我检查一下代码好吗?我不明白哪里错了。我已经编辑了问题。 - Vinit Gaikwad
您的app2的意图过滤器具有“com.example.vinitanilgaikwad.app2”操作,而您的app1正在发送一个意图“com.example.vinitanilgaikwad.app2_try2”。您的意图必须相同,因为当您的app1发送“com.example.vinitanilgaikwad.app2_try2”意图时,系统找不到与该意图匹配的服务(因为您的app2服务响应“com.example.vinitanilgaikwad.app2”意图)。 - josemgu91
对不起,先生。那是一个打字错误。即使我将其保留为“com.example.vinitanilgaikwad.app2”,它仍然存在一些问题。你能提供建议吗?我已经更正了打字错误。 - Vinit Gaikwad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接