//app類 package com.qudoulicai.www.aidltest; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.Button; import android.widget.TextView; import com.qudoulicai.www.mylibrary.*; import com.qudoulicai.www.mylibrary.Aidlone; public class MainActivity extends AppCompatActivity { private TextView titles; public Button onclick; Aidlone aidlone; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); titles = (TextView) findViewById(R.id.titles); onclick = (Button) findViewById(R.id.onclick); onclick.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); //5.0版本則設置爲顯示意圖 //不然設置爲Intent intent = new Intent("you action("參考mylibrary清單文件")") intent.setClass(MainActivity.this, MAIDLService.class); bindService(intent, connection, BIND_AUTO_CREATE); } }); } ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { try { aidlone = Aidlone.Stub.asInterface(service); Log.e("張三",aidlone.getName().length()+""); titles.setText(aidlone.getName()); } catch (Exception e) { e.printStackTrace(); } } // @Override public void onServiceDisconnected(ComponentName name) { Log.e("張三","失敗"); } }; } //app中的aidl // Aidlone.aidl package com.qudoulicai.www.aidltest; // Declare any non-default types here with import statements interface Aidlone { String getName(); } //mylibrary依賴包 //服務類 package com.qudoulicai.www.mylibrary; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import android.support.annotation.Nullable; /** * Created by Lenovo on 2015/12/3. */ public class MAIDLService extends Service { public MAIDLService(){ } Aidlone.Stub sumber = new Aidlone.Stub(){ @Override public String getName() throws RemoteException { return "張三"; } }; @Nullable @Override public IBinder onBind(Intent intent) { return sumber; } } //aidl文件 // Aidlone.aidl package com.qudoulicai.www.mylibrary; // Declare any non-default types here with import statements interface Aidlone { String getName(); }
//mylibrary清單文件 <service android:name="com.qudoulicai.www.mylibrary.MAIDLService" android:process=":remote" > <intent-filter> <action android:name="com.qudoulicai.www.mylibrary.MAIDLService"></action> </intent-filter> </service>
//app中的佈局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.qudoulicai.www.aidltest.MainActivity" tools:showIn="@layout/activity_main"> <TextView android:id="@+id/titles" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <Button android:id="@+id/onclick" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="單擊"/> </LinearLayout>
//ps:有不足之處請多包涵。。。建立依賴文件記得引用文件「java
compile project(':"you libray name"')
」
android