【问题标题】:difference between Service and IntentServiceService 和 IntentService 的区别
【发布时间】:2016-07-18 11:07:31
【问题描述】:

为了了解 Android 中 IntentService 和 Service 之间的区别,我创建了下面发布的 IntentService 类的小测试。 IntentService 类可以使用 startService(intent); 这将导致调用 nStartCommand(Intent intent, int flags, int startId)。还将值从 IntentService 类发送到 MainActivity 例如,我们应该通过sendBroadcast(intent); 发送它,并且 MainActivity 应该为该操作注册一个广播接收器,以便它可以接收通过发送的值

sendBroadcast(intent);

到目前为止,我看不出 Service 和 IntentService 之间有什么区别!!由于它们的启动方式和广播数据的方式相似,请您告诉我 它们在哪种情况下不同?

请告诉我为什么我会收到这些错误以及如何解决它

MainActivity

public class MainActivity extends AppCompatActivity {

    private final String TAG = this.getClass().getSimpleName();

    private Button mbtnSend = null;
    private int i = 0;

    private BroadcastReceiver mBCR_VALUE_SENT = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (action.equals(MyIntentService.INTENT_ACTION)) {
                int intnetValue = intent.getIntExtra(MyIntentService.INTENT_KEY, -1);
                Log.i(TAG, SubTag.bullet("mBCR_VALUE_SENT", "intnetValue: " + intnetValue));
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        registerReceiver(this.mBCR_VALUE_SENT, new IntentFilter(MyIntentService.INTENT_ACTION));

        this.mbtnSend = (Button) findViewById(R.id.btn_send);
        this.mbtnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), MyIntentService.class);
                intent.putExtra("intent_key", ++i);
                startService(intent);
            }
        });
    }
}

MyIntentService

public class MyIntentService extends IntentService {

private final String TAG = this.getClass().getSimpleName();
public final static String INTENT_ACTION = "ACTION_VALUE_SENT";
public final static String INTENT_KEY = "INTENT_KEY";

public MyIntentService() {
    super(null);
}

/**
 * Creates an IntentService.  Invoked by your subclass's constructor.
 *
 * @param name Used to name the worker thread, important only for debugging.
 */
public MyIntentService(String name) {
    super(name);
    setIntentRedelivery(true);
}

@Override
public void onCreate() {
    super.onCreate();
    Log.w(TAG, SubTag.msg("onCreate"));
}

@Override
protected void onHandleIntent(Intent intent) {
    Log.w(TAG, SubTag.msg("onHandleIntent"));

    int intent_value = intent.getIntExtra("intent_key", -1);
    Log.i(TAG, SubTag.bullet("", "intent_value: " + intent_value));

    Intent intent2 = new Intent();
    intent2.setAction(MyIntentService.INTENT_ACTION);
    intent2.putExtra(MyIntentService.INTENT_KEY, intent_value);
    sendBroadcast(intent2);

    SystemClock.sleep(3000);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.w(TAG, SubTag.msg("onStartCommand"));

    return super.onStartCommand(intent, flags, startId);
}

【问题讨论】:

  • Service 在调用它的同一线程上工作的主要区别。 IntentService 在后台线程上工作

标签: android service android-service intentservice android-intentservice


【解决方案1】:

简而言之,Service 是开发人员设置后台操作的更广泛的实现,而 IntentService 用于“即发即弃”操作,负责后台线程的创建和清理。

来自文档:

服务服务是一个应用程序组件,代表应用程序希望在不与用户交互的情况下执行更长时间运行的操作,或提供功能供其他应用程序使用。

IntentService Service 是 IntentService 服务的基类,可按需处理异步请求(表示为 Intent)。客户端通过 startService(Intent) 调用发送请求;服务根据需要启动,使用工作线程依次处理每个 Intent,并在工作结束时自行停止。

请参阅此文档 - http://developer.android.com/reference/android/app/IntentService.html

【讨论】:

    【解决方案2】:

    服务

    这是所有服务的基类。扩展此类时,创建一个新线程来完成所有服务的工作非常重要,因为默认情况下,服务使用应用程序的主线程,这可能会降低应用程序正在运行的任何活动的性能。

    意图服务

    这是 Service 的一个子类,它使用一个工作线程来处理所有启动请求,一次一个。如果您不要求您的服务同时处理多个请求,这是最佳选择。您需要做的就是实现 onHandleIntent(),它会接收每个启动请求的意图,以便您进行后台工作。

    以下是 Android 中 Service 和 IntentService 之间的一些主要区别。

    1) 什么时候使用?

    Service 可以在没有 UI 的任务中使用,但不应该太长。如果需要执行长任务,则必须在 Service 中使用线程。

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

    2) 如何触发?

    Service 被触发调用 onStartService() 方法。

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

    为了更清楚,请参阅此 http://www.onsandroid.com/2011/12/difference-between-android.html

    【讨论】:

    • 在第 1 点中,您表示在 Service 中无法操作 UI 线程……在 Intentservice 中可能吗??
    • 两者都没有与 UI 通信。但如果在意图服务中需要,我们必须使用主线程处理程序或广播意图。
    • 我可以在您对this one 的回答中看到很多相似之处。您能否使用引号突出显示(通过以> 开头的一行)来显示哪些行是您的话,哪些行是复制/粘贴的?这将防止其他人可能指责你抄袭。
    猜你喜欢
    • 2011-12-07
    • 1970-01-01
    • 2015-10-05
    • 2019-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多