Service与Activity之间通信除了,通过Binder对象、Broadcast,还有ResultReceiver,使用起来你会觉得非常好用。
下面我写一个小例子:
ActivityResultReceiver.java
package com.ixiaoyu.demo.ui.activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.support.v4.os.ResultReceiver; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.ixiaoyu.demo.R; import com.ixiaoyu.demo.ui.service.ServiceResultReceiver; /** * Created by yuzhenbei on 2016/2/4. */ public class ActivityResultReceiver extends AppCompatActivity implements View.OnClickListener{ /** */ private Button mBtnStartService; /** */ private TextView mTxtResult; /** */ private ResultReceiver mResultReceiver; /** */ public static String KEY_RESULT_RECEIVER = "KEY_RESULT_RECEIVER"; /** */ public static String KEY_NUM_1 = "KEY_NUM_1"; /** */ public static String KEY_NUM_2 = "KEY_NUM_2"; /** */ public static String KEY_RESULT = "KEY_RESULT"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_result_receiver); //初始化 initView(); //InitResultReceiver initResultReceiver(); } /** * 初始化 */ private void initView() { mBtnStartService = (Button)findViewById(R.id.btn_open_service); mBtnStartService.setOnClickListener(this); mTxtResult = (TextView)findViewById(R.id.txt_result); } /** * set */ private void initResultReceiver(){ mResultReceiver = new ResultReceiver(new Handler()){ @Override protected void onReceiveResult(int resultCode, Bundle resultData) { super.onReceiveResult(resultCode, resultData); if (null != resultData){ int _Result = resultData.getInt(KEY_RESULT); mTxtResult.setText("收到:" + _Result); } } }; } @Override protected void onResume() { super.onResume(); } @Override protected void onPause() { super.onPause(); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_open_service: Intent _Intent = new Intent(); _Intent.putExtra(KEY_RESULT_RECEIVER, mResultReceiver); _Intent.putExtra(KEY_NUM_1, 6); _Intent.putExtra(KEY_NUM_2, 9); _Intent.setClass(this, ServiceResultReceiver.class); startService(_Intent); break; } } }
ServiceResultReceiver.java
package com.ixiaoyu.demo.ui.service; import android.app.Activity; import android.app.IntentService; import android.content.Intent; import android.os.Bundle; import android.support.v4.os.ResultReceiver; import com.ixiaoyu.demo.ui.activity.ActivityResultReceiver; /** * Created by yuzhenbei on 2016/2/4. */ public class ServiceResultReceiver extends IntentService{ /** TAG */ private static final String TAG = ServiceResultReceiver.class.getSimpleName(); /** Used to name the worker thread */ public static final String WORKER_NAME = TAG + "Worker_Thread"; public ServiceResultReceiver() { super(WORKER_NAME); } @Override protected void onHandleIntent(Intent intent) { ResultReceiver _rec = intent.getParcelableExtra(ActivityResultReceiver.KEY_RESULT_RECEIVER); int _num1 = intent.getIntExtra(ActivityResultReceiver.KEY_NUM_1, 0); int _num2 = intent.getIntExtra(ActivityResultReceiver.KEY_NUM_2, 0); int _result = _num1 + _num2; Bundle _b = new Bundle(); _b.putInt(ActivityResultReceiver.KEY_RESULT, _result); _rec.send(Activity.RESULT_OK, _b); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } _result = _num1 * _num2; _b.putInt(ActivityResultReceiver.KEY_RESULT, _result); _rec.send(Activity.RESULT_OK, _b); } } 效果:
文章评论
That's a clever answer to a tricky quseiton