intentservice的用法

intentService与service相比内部开启子线程,把耗时操作放到onHandleIntent方法中,与Activity的通信使用Binder,没毛病。以后使用到服务直接用intentService就可以啦,省时省力。
PS:service两套生命周期实际开发中合为一套 oncreate() onstartcommand() onBind() onUnbind() onDestroy()

以下代码为activity和intentservice通信问题
Activity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

   private TextView mTv;
    private Button mBt1,mBt2,mBt3,mBt4;
    private Intent intent;







    private ServiceConnection mServiceConnection=new ServiceConnection() {
        @Override
        public void onServiceConnected(final ComponentName name, final IBinder service) {
            System.out.println("Activty启动,获取此时服务的数据");
            System.out.println("ComponentName = [" + name + "]");
            Log.e("1", "Activty启动,获取此时服务的数据 " );
            Log.e("1","ComponentName = [" + name + "]");

            MyIntentService2.MyBinder myBinder= (MyIntentService2.MyBinder) service;
            mTv.setText("这个服务的数字已经走到:"+  myBinder.getCount());
//            myBinder.dyfwff();
//            MyService myService=myBinder.mMyService;
//            myService.fwdff();
        }

        @Override
        public void onServiceDisconnected(final ComponentName name) {

        }
    };
    @Override
    public void onClick(View v) {
//        Intent mIntent=new Intent(this,TargetActivity.class);
//        mIntent.putExtra("person",mPerson);
//        startActivity(mIntent);

        switch (v.getId()){
//            case R.id.bt:
//                Intent mIntent=new Intent(this,TargetActivity.class);
//                mIntent.putExtra("mDog",mDog);
//                startActivity(mIntent);
//                break;
            case R.id.bt1:
                Log.e("1", "run:启动服务 " );

                startService(intent);
                break;
            case R.id.bt2:
                Log.e("1", "run:bind服务 " );
                bindService(intent,mServiceConnection, Service.BIND_AUTO_CREATE);
                break;
            case R.id.bt3:
                Log.e("1", "run:unbind服务 " );
                if(mServiceConnection!=null)
                unbindService(mServiceConnection);
                break;
            case R.id.bt4:
                Log.e("1", "run:stop服务 " );
                stopService(intent);
                break;
            default:

                break;

        }

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();

        try {
            unbindService(mServiceConnection);
        } catch (Exception e) {
            System.out.println("请先绑定服务再执行解绑");
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


//        mBt = (Button)getFragmentManager().findFragmentById(R.id.layout_home_activity_root_frag).getView().findViewById(R.id.bt);
        mBt = (Button) findViewById(R.id.bt);
        mBt.setOnClickListener(this);
        mBt1 = (Button) findViewById(R.id.bt1);
        mBt1.setOnClickListener(this);
        mBt2 = (Button) findViewById(R.id.bt2);
        mBt2.setOnClickListener(this);
        mBt3 = (Button) findViewById(R.id.bt3);
        mBt3.setOnClickListener(this);
        mBt4 = (Button) findViewById(R.id.bt4);
        mBt4.setOnClickListener(this);
        mTv = (TextView) findViewById(R.id.tv);
        intent=new Intent(this,MyIntentService2.class);
//        addFragment(mFragment2);
    }


}

intentService

public class MyIntentService2 extends IntentService {

    private int count;
    private boolean mBoolean=true;
    public MyIntentService2() {
        super("MyIntentService2");
    }

    @Nullable
    @Override
    public IBinder onBind(final Intent intent) {
//        return super.onBind(intent);
        return new MyBinder();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            while (mBoolean) {
                SystemClock.sleep(1000);
                Log.e("1", "当前数据已经递增到---->" + count);
                count++;
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBoolean=false;
    }

    protected class MyBinder extends Binder {
        public MyIntentService2 mMyService;

        public MyBinder() {
            mMyService = MyIntentService2.this;
        }

        public int getCount() {
            return count;
        }

    }

}

猜你喜欢

转载自blog.csdn.net/xiyangyang8110/article/details/80241442