? ? ? System.out.println(flag);
? ? ? ? return ms;
? ? }
? ? MyServiceAIDL.Stub ms = new MyServiceAIDL.Stub() {
? ? ? ? @Override
? ? ? ? public void DownLoad() throws RemoteException {
? ? ? ? ? ? new Thread(new Runnable() {
? ? ? ? ? ? ? ? int i = 0;
? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? ? ? //未达到线程条件,会一直在后台运行,就算服务已经关闭
? ? ? ? ? ? ? ? ? ? while (flag) {
? ? ? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? ? ? ? ? i++;
? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("i的值是" + i);
? ? ? ? ? ? ? ? ? ? ? ? ? ? Thread.sleep(1000);
? ? ? ? ? ? ? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? System.out.println("退出服务");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }).start();
? ? ? ? }
? ? };
}
AndroidManifest.xml
? ? ? ? android:allowBackup="true"
? ? ? ? android:icon="@drawable/ic_launcher"
? ? ? ? android:label="@string/app_name"
? ? ? ? android:theme="@style/AppTheme">
? ? ? ? ? ? ? ? ? ? android:name=".MainActivity"
? ? ? ? ? ? android:label="@string/app_name">
? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ?
? ? ? ? ? ? ? ? ? ? android:name=".MainService"
? ? ? ? ? ? android:process=":remote">
? ?
MyServiceAIDL.aidl
// myServiceAIDL.aidl
package com.example.wanghao.aidldemo;
// Declare any non-default types here with import statements
interface MyServiceAIDL {
? ? void DownLoad();
}