亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

綁定/取消綁定服務示例(android)

綁定/取消綁定服務示例(android)

慕婉清6462132 2019-11-28 14:09:20
您能給我一個帶有后臺服務的應用程序的簡單示例,該應用程序使用bind / unbind方法啟動和停止它嗎?我一直在搜索它半小時,但是這些示例使用startService / stopService方法,或者對我來說很難。謝謝。
查看完整描述

3 回答

?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

您可以嘗試使用以下代碼:


protected ServiceConnection mServerConn = new ServiceConnection() {

    @Override

    public void onServiceConnected(ComponentName name, IBinder binder) {

        Log.d(LOG_TAG, "onServiceConnected");

    }


    @Override

    public void onServiceDisconnected(ComponentName name) {

        Log.d(LOG_TAG, "onServiceDisconnected");

    }

}


public void start() {

    // mContext is defined upper in code, I think it is not necessary to explain what is it 

    mContext.bindService(intent, mServerConn, Context.BIND_AUTO_CREATE);

    mContext.startService(intent);

}


public void stop() {

    mContext.stopService(new Intent(mContext, ServiceRemote.class));

    mContext.unbindService(mServerConn);

}


查看完整回答
反對 回復 2019-11-28
?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

將以下方法添加到您的活動中:


private MyService myServiceBinder;

public ServiceConnection myConnection = new ServiceConnection() {


    public void onServiceConnected(ComponentName className, IBinder binder) {

        myServiceBinder = ((MyService.MyBinder) binder).getService();

        Log.d("ServiceConnection","connected");

        showServiceData();

    }


    public void onServiceDisconnected(ComponentName className) {

        Log.d("ServiceConnection","disconnected");

        myService = null;

    }

};


public Handler myHandler = new Handler() {

    public void handleMessage(Message message) {

        Bundle data = message.getData();

    }

};


public void doBindService() {

    Intent intent = null;

    intent = new Intent(this, BTService.class);

    // Create a new Messenger for the communication back

    // From the Service to the Activity

    Messenger messenger = new Messenger(myHandler);

    intent.putExtra("MESSENGER", messenger);


    bindService(intent, myConnection, Context.BIND_AUTO_CREATE);

}

您可以通過在Activity類上ovverriding onResume()和onPause()綁定到服務。


@Override

protected void onResume() {


    Log.d("activity", "onResume");

    if (myService == null) {

        doBindService();

    }

    super.onResume();

}


@Override

protected void onPause() {

    //FIXME put back


    Log.d("activity", "onPause");

    if (myService != null) {

        unbindService(myConnection);

        myService = null;

    }

    super.onPause();

}

請注意,綁定到服務時,僅onCreate()在服務類中調用該方法。在您的Service類中,您需要定義myBinder方法:


private final IBinder mBinder = new MyBinder();

private Messenger outMessenger;


@Override

public IBinder onBind(Intent arg0) {

    Bundle extras = arg0.getExtras();

    Log.d("service","onBind");

    // Get messager from the Activity

    if (extras != null) {

        Log.d("service","onBind with extra");

        outMessenger = (Messenger) extras.get("MESSENGER");

    }

    return mBinder;

}


public class MyBinder extends Binder {

    MyService getService() {

        return MyService.this;

    }

}

定義了這些方法之后,您可以在“活動”中找到服務的方法:


private void showServiceData() {  

    myServiceBinder.myMethod();

}

最后,當某些事件發生時,例如_BOOT_COMPLETED_,您可以啟動服務


public class MyReciever  extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();

        if (action.equals("android.intent.action.BOOT_COMPLETED")) {

            Intent service = new Intent(context, myService.class);

            context.startService(service);

        }

    }

}

需要注意的是啟動服務的時候onCreate()和onStartCommand()被調用服務類,當另一個事件發生時由您可以停止你的服務stopService() 請注意,事件偵聽器應該是在你的Android清單文件registerd:


<receiver android:name="MyReciever" android:enabled="true" android:exported="true">

        <intent-filter>

            <action android:name="android.intent.action.BOOT_COMPLETED" />

        </intent-filter>

</receiver>


查看完整回答
反對 回復 2019-11-28
?
四季花海

TA貢獻1811條經驗 獲得超5個贊

首先,我們需要了解兩件事,


客戶

它向特定服務器發出請求

bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"),

            mServiceConn, Context.BIND_AUTO_CREATE);


這mServiceConn是ServiceConnection類(內置)的實例,它實際上是我們需要用兩種方法(用于連接網絡的第一個和未連接網絡的第二個)實現的接口,以監視網絡連接狀態。


服務器

它處理客戶端的請求,并制作自己的副本,該副本僅對發送請求的客戶端是私有的,并且此服務器在不同線程上運行。

現在在客戶端,如何訪問服務器的所有方法?

服務器發送帶有IBinder對象的響應。因此,IBinderobject是我們的處理程序,該處理程序Service使用(。)運算符訪問所有方法。

。


MyService myService;

public ServiceConnection myConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName className, IBinder binder) {

        Log.d("ServiceConnection","connected");

        myService = binder;

    }

    //binder comes from server to communicate with method's of 


    public void onServiceDisconnected(ComponentName className) {

        Log.d("ServiceConnection","disconnected");

        myService = null;

    }

}

現在如何調用服務中的方法

myservice.serviceMethod();

這myService是對象,serviceMethod是服務中的方法。并以此方式在客戶端和服務器之間建立通信。


查看完整回答
反對 回復 2019-11-28
  • 3 回答
  • 0 關注
  • 619 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號