遠程服務實現android系統中的線程之間的通訊

來源:互聯網
上載者:User

1、遠程服務的類的建立

(1)服務類

public class MyService extends Service {

 // 遠端服務
 @Override
 public IBinder onBind(Intent intent) {
  // TODO Auto-generated method stub
 return new MyBinder();
 }
 

 private class MyBinder extends IService.Stub{

  @Override
  public void CallMethodInService(String name) throws RemoteException {
   sayHelloInservice(name);
   
  }
  
 }
 public  void sayHelloInservice(String name){
  Looper.prepare();
  Toast.makeText(this, "你好"+name, 1).show();
  Looper.loop();
 }

 @Override
 public void onCreate() {
  System.out.println("reomte oncreate");
  super.onCreate();
 }

 @Override
 public void onStart(Intent intent, int startId) {
  // TODO Auto-generated method stub
  System.out.println("onstart");
  super.onStart(intent, startId);
 }

 @Override
 public void onDestroy() {
  // TODO Auto-generated method stub
  System.out.println("ondestroy");
  super.onDestroy();
 }

}

配置

 <service  android:name=".MyService"            >
            <intent-filter>
                <action android:name="cn.itcast.remoteservice"/>    

            </intent-filter>
        </service>
    </application>

(2)介面IService.aidl

package cn.itcast.remoteservice;
interface IService {
 void CallMethodInService(String name);
}

(3)自動產生的檔案IService.java

public interface IService extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder
implements remoteservice.IService
{

........................
}
工程結構圖

2、遠端存取端

DemoActivity.java類

public class DemoActivity extends Activity {
 Intent intent;
 MyRemoteConn conn;
 cn.itcast.remoteservice.IService iRemoteService;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         conn = new MyRemoteConn();
    }
    private class MyRemoteConn implements ServiceConnection{

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
     iRemoteService  = cn.itcast.remoteservice.IService.Stub.asInterface(service);
     
    }
    @Override
    public void onServiceDisconnected(ComponentName name) {
     
    }
       
      }
 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  unbindService(conn);
  super.onDestroy();
 }
   
    //綁定按鈕
    public void bindRemoteService(View view){
     Intent service = new Intent();
     service.setAction("cn.itcast.remoteservice");
     bindService(service,new MyRemoteConn() , BIND_AUTO_CREATE);
    }
    //執行服務方法
    public void callMethodInRemoteService(View view){
     try {
   iRemoteService.CallMethodInService("遠程張三 ");
  } catch (RemoteException e) {
   e.printStackTrace();
  }
    }
   
}

(2)介面IService.aidl

package cn.itcast.remoteservice;
interface IService {
 void CallMethodInService(String name);
}

(3)自動產生的檔案IService.java

public interface IService extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder
implements remoteservice.IService
{

........................
}

工程檔案結構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.