Android服務Services

今天有時間來分享一個android中的Services(服務)寫一個類似可以用QQ號登錄微信的功能

什麼是服務:
Service 是一個可以在後臺執行長時間運行操作而不提供用戶界面的應用組件。服務可由其他應用組件啓動,而且即使用戶切換到其他應用,服務仍將在後臺繼續運行。 此外,組件可以綁定到服務,以與之進行交互,甚至是執行進程間通信 (IPC)。 例如,服務可以處理網絡事務、播放音樂,執行文件 I/O 或與內容提供程序交互,而所有這一切均可在後臺進行。
服務基本上分爲兩種形式:啓動、綁定
官網API:https://developer.android.google.cn/guide/components/services.html

本次牽涉到進程 進程通訊
AIDL(Android 接口定義語言)與您可能使用過的其他 IDL 類似。 您可以利用它定義客戶端與服務使用進程間通信 (IPC) 進行相互通信時都認可的編程接口。 在 Android 上,一個進程通常無法訪問另一個進程的內存。 儘管如此,進程需要將其對象分解成操作系統能夠識別的原語,並將對象編組成跨越邊界的對象。 編寫執行這一編組操作的代碼是一項繁瑣的工作,因此 Android 會使用 AIDL 來處理。
官網API:https://developer.android.google.cn/guide/components/aidl.html

不多bibi上代碼吧
首先我們要興建兩個項目:
anjie_android_services2_1(QQ)
anjie_android_services2_2(微信 使用QQ的登錄)

需要在anjie_android_services2_1(QQ)mainfests中配置

  <!--配置服務-->
 <service android:name=".QQLoginService"
            android:exported="true"
            ></service>

客戶端 anjie_android_services2_2(微信 使用QQ的登錄)
需要將服務器中AIDL自動生成的接口類以及包copy導客戶端項目的Java包中(需要重新編譯),開始在Activity中onREsume()方法綁定服務,
方法bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);
裏面有三個參數,第一個參數:Intent(new一個Intent),intent = new Intent(this,QQLoginServices.class);
多進程間啓動Services時,Android 5.0 之後,啓動其他應用程序的服務,不允許使用隱式。
ComponentName 有兩個參數,第一個是放置包名(你所需要的服務在哪個項目),第二個是放置服務類名(你所需要的服務在哪個項目的服務類名)

ComponentName componentName=new ComponentName
(“cn.veryedu.g0305_android18_services”,”cn.veryedu.g0305_android18_services.MyServices”);
intent.setComponent(componentName);
第二個參數:ServiceConnection,new一個ServiceConnection,實現接口類中的兩個方法。onServiceConnected() onServiceDisconnected()。
第三個參數:Service.BIND_AUTO_CREATE(一般都是用這個)。
然後在onServiceConnected()方法裏面得到接口的方法:QQLoginInface.Stub.asInterface(iBinder);調用它

傳遞數據(IBinder),通過調用方法取得服務器的數據(傳遞對象是需要序列化實現遠程服務)。

服務器 anjie_android_services2_1(QQ)
Services

public class QQLoginServices extends Service {
    //新建一個IBinder類繼承aidl

    class MyIBinder extends QQLoginInface.Stub{

        @Override
        public boolean Login(String num, String pwd) throws RemoteException {
            if("10010".equals(num)&&"123456".equals(pwd)){
                return true;
            }
            return false;
        }
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i("tt","onBind");
        return new MyIBinder();
    }
}

Activity(簡易的登錄)

public class MainActivity extends AppCompatActivity {
    private EditText main_num;
    private EditText main_pwd;
    private Intent intent;
    private QQLoginInface qqLoginInface;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        main_num = (EditText) findViewById(R.id.main_num);
        main_pwd = (EditText) findViewById(R.id.main_pwd);
        intent = new Intent();
        ComponentName componentName=new ComponentName("com.example.yang_servicesqq","com.example.yang_servicesqq.QQLoginServices");
        intent.setComponent(componentName);
    }
        ServiceConnection serviceConnection=new ServiceConnection() {




      @Override
      public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
          //連接成功
          Log.i("tt","服務綁定成功了!");
          //得到QQLoginInface.aidl
          qqLoginInface = QQLoginInface.Stub.asInterface(iBinder);
           }

      @Override
      public void onServiceDisconnected(ComponentName componentName) {

      }

  };
    @Override
    protected void onResume() {
        super.onResume();
        //綁定服務
        bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);
    }

    //登錄
    public void login(View view) throws Exception {
        String num=main_num.getText().toString();
        String pwd=main_pwd.getText().toString();
        boolean f=qqLoginInface.Login(num,pwd);
        Toast.makeText(this,""+f,Toast.LENGTH_SHORT).show();
    }
}

AIDL

package com.example.yang_servicesqq;
interface QQLoginInface {
   boolean Login(String num,String pwd);
}

配置服務

<service android:name=".QQLoginServices"
            android:exported="true"
            android:enabled="true">

        </service>

客戶端Activity

public class MainActivity extends AppCompatActivity {
    private EditText main_num;
    private EditText main_pwd;
    private Intent intent;
    private QQLoginInface qqLoginInface;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        main_num = (EditText) findViewById(R.id.main_num);
        main_pwd = (EditText) findViewById(R.id.main_pwd);
        intent = new Intent();
        ComponentName componentName=new ComponentName("com.example.yang_servicesqq","com.example.yang_servicesqq.QQLoginServices");
        intent.setComponent(componentName);
    }
        ServiceConnection serviceConnection=new ServiceConnection() {     @Override
      public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
          //連接成功
          Log.i("tt","服務綁定成功了!");
          //得到QQLoginInface.aidl
          qqLoginInface = QQLoginInface.Stub.asInterface(iBinder);
           }

      @Override
      public void onServiceDisconnected(ComponentName componentName) {

      }

  };
    @Override
    protected void onResume() {
        super.onResume();
        //綁定服務
        bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);
    }
登錄
 public void login(View view) throws Exception {
        String num=main_num.getText().toString();
        String pwd=main_pwd.getText().toString();
        boolean f=qqLoginInface.Login(num,pwd);
        Toast.makeText(this,""+f,Toast.LENGTH_SHORT).show();
    }

差不多就這樣,有疑問請留言。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章