• Service


    目录

    • 多线程
      • android 线程
      • 异步消息处理机制(message handler)
      • AsyncTask
    • Service方法
    • startService(),stopService()
    • bindService(),unBindService()
    • startService()与bindService()启动service区别
    • IntentService
    • 进程间通信

    android 线程

    _android_主线程是UI thread,UI线程5秒没有反应程序崩溃,所以耗时操作放在子线程中进行,然后把结果传递给UI线程。

    service方法

    onCreate()onStartCommand()onDestry()onBind()onUnbind()

    startService(),stopService()

    public class MyService extends Service {  
    
       public static final String TAG = "MyService";  
    
       //创建服务时调用
       @ Override  
       public void onCreate() {  
           super.onCreate();  
           Log.d(TAG, "onCreate");  
       }  
    
       //服务执行的操作
       @ Override  
       public int onStartCommand(Intent intent, int flags, int startId) {  
           Log.d(TAG, "onStartCommand");  
           return super.onStartCommand(intent, flags, startId);  
       }  
    
       //销毁服务时调用
       @ Override  
       public void onDestroy() {  
           super.onDestroy();  
           Log.d(TAG, "onDestroy");  
       }  
    
       //onBind()方法是Service中唯一的一个抽象方法,所以必须要在子类里实现。
       //Service有两种启动方式:一种是startService(),另一种是bindService()。第二种启动方式才会用到onBind()方法。
       //我们这先用第一种方式定义Service,所以暂时忽略onBind()方法。
       @ Override  
       public IBinder onBind(Intent intent) {  
           return null;  
       }
    
    public class MainActivity extends Activity implements OnClickListener {  
       private Button button1_start_service;  
       private Button button2_stop_service; 
       @ Override  
       protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
           button1_start_service = (Button)findViewById(R.id.button1_start_service);  
           button2_stop_service = (Button)findViewById(R.id.button2_stop_service);  
           button1_start_service.setOnClickListener(this);  
           button2_stop_service.setOnClickListener(this);  
       }  
    
       @ Override  
       public void onClick(View v) {  
           switch (v.getId()) {  
           case R.id.button1_start_service:  
               Intent startIntent = new Intent(this, MyService.class);  
               startService(startIntent);  
               break;  
           case R.id.button2_stop_service:  
               Intent stopIntent = new Intent(this, MyService.class);  
               stopService(stopIntent);  
               break;  
           default:  
               break;  
           }  
       }  
    }
    
    

    startService()和stopService()方法都是定义在Context类当中的,所以可以在MainActivity中直接调用这两个方法。

    • 操作一:点击start 执行方法onCreate onStartCommand
    • 操作二:不断点击start 执行方法 onStartCommand onStartCommand
    • 操作三:点击stop 执行方法 onStop

    bindService(),unBindService()

    public class MyBindService01 extends Service {
      public static final String TAG = "MyBindService01";
      private MyBinder mBinder = new MyBinder();
      @ Override
      public void onCreate() {
          super.onCreate();
          Log.d(TAG, "onCreate");
      }
    
      @ Override
      public IBinder onBind(Intent intent) {
          return mBinder;  //在这里返回新建的MyBinder类
      }
    
      @ Override
      public boolean onUnbind(Intent intent) {
          Log.d(TAG, "onUnbind");
          return super.onUnbind(intent);
      }
    
      @ Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          Log.d(TAG, "onStartCommand");
          return super.onStartCommand(intent, flags, startId);
      }
    
      @ Override
      public void onDestroy() {
          super.onDestroy();
          Log.d(TAG, "onDestroy");
      }
    
      //MyBinder类,继承Binder:让里面的方法执行下载任务,并获取下载进度
      class MyBinder extends Binder {
          public void startDownload() {
              Log.d("TAG", "startDownload() executed");
              // 执行具体的下载任务  
          }
          public int getProgress(){
              Log.d("TAG", "getProgress() executed");
              return 0;
          }
      }
    }
    
    
    public class MainActivity extends Activity implements OnClickListener {
      private Button button1_bind_service;
      private Button button2_unbind_service;
      private MyBindService01.MyBinder myBinder;
    
      boolean mBound = false; //一开始,并没有和Service绑定.这个参数是用来显示绑定状态
    
      //匿名内部类:服务连接对象
      private ServiceConnection connection = new ServiceConnection() {
    
          //当服务异常终止时会调用。注意,解除绑定服务时不会调用
          @ Override
          public void onServiceDisconnected(ComponentName name) {
              mBound = false; //服务异常终止时,状态为未绑定
              //解决了多次执行unbindService()方法引发的异常问题
          }
    
          //和服务绑定成功后,服务会回调该方法
          @ Override
          public void onServiceConnected(ComponentName name, IBinder service) {
              myBinder = (MyBindService01.MyBinder) service;
              //在Activity中调用Service里面的方法
              myBinder.startDownload();
              myBinder.getProgress();
              mBound = true; //true说明是绑定状态
          }
      };
      @ Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          button1_bind_service = (Button) findViewById(R.id.button1_bind_service);
          button2_unbind_service = (Button) findViewById(R.id.button2_unbind_service);
          button1_bind_service.setOnClickListener(this);
          button2_unbind_service.setOnClickListener(this);
      }
      @ Override
      public void onClick(View v) {
          switch (v.getId()) {
          case R.id.button1_bind_service:
              Intent bindIntent = new Intent(this, MyService.class);
              bindService(bindIntent, connection, BIND_AUTO_CREATE);
    //这里传入BIND_AUTO_CREATE表示在Activity和Service建立关联后会自动创建Service(即使之前没有创建
    //Service也没有关系),这会使得MyService中的onCreate()方法得到执行,但onStartCommand()方法不会执行。
              break;
          case R.id.button2_unbind_service:
              //如果和Service是绑定的状态,就解除绑定。
              if(mBound){
                  unbindService(connection);
                  mBound=false;
              }
              break;
          default:
              break;
          }
      }
    }
    
    
    • 操作一:bindService 执行方法 onCreate() getProgress() startDownload()

    • 操作二:unbindService 执行方法 onUnbind() onDestroy()

    Three passions, simple but overwhelmingly strong, have governed my life: the longing for love, the search for knowledge, and unbearable pity for the suffering of mankind
  • 相关阅读:
    50
    49
    Windows编程之connect函数研究
    48
    C++创建窗口程序初步
    47
    46
    45
    计算机组成原理实验思路
    44(function pointer 2)
  • 原文地址:https://www.cnblogs.com/s3abiscuit/p/7202600.html
Copyright © 2020-2023  润新知