Android 的应用service


service client /server 端示例:

server端,

step 1 添加aidl 文件,这个文件只是定义用要的接口,接口名字要与文件名相同。

如:ItrainningService.aidl

package com.company.server.aidl;

import com.company.server.aidl.FinishCallBack;

interface ItrainningService{
    void function_one(String abc,String op);
    void function_two(String abc);
    void registerCallback(FinishCallBack cb);
    void unregisterCallback(FinishCallBack cb);
}

FinishCallBack.aidl

package com.company.server.aidl;

interface FinishCallBack {
    void onFinish(String s,int a);
}

它们放的位置也是有要求的,需要和包名相同,另外,如果是在android studio 中编译的话,要放在main下面,最外层还要有一个aidl,

比如上的的文件要放在../main/aidl/com/company/server/aidl/下面。

step 2, 建立service类ServerService, 这个java文件要import 我们上面定义的接口

另外,还定义了一个

class ServerBinder


...
import com.company.server.aidl.ItrainningService;
import com.company.server.aidl.FinishCallBack;


public class ServerService extends Service {
    final String TAG = "ServerService";
    FinishCallBack finishCallBack;
    public ServerService() {
    }

    private IBinder binder = new ServerBinder();

    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "onBind service");
        // TODO Auto-generated method stub
        return binder;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
        Log.i(TAG, "start service");
    }

    @Override
    public boolean bindService(Intent service, ServiceConnection conn, int flags) {
        // TODO Auto-generated method stub
        Log.i(TAG, "bind service");
        return super.bindService(service, conn, flags);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        Log.i(TAG, "start service");
        return super.onStartCommand(intent, flags, startId);
    }

    //继承stub,实现ItrainningService的接口
	private final class ServerBinder extends ItrainningService.Stub {
		
		@Override
		public void function_one(String abc, String op) throws RemoteException {
			Context context = CApplication.getInstance();

			if(MyfuncClass.function_one(context,pkgName,pkgPath)==0){
				//success
				finishCallBack.onFinish(pkgPath,0);
			}else{
				//fail
				finishCallBack.onFinish(pkgPath,1);
			}
		}

		@Override
		public void function_two(String pkgName) throws RemoteException {

			Context context = CApplication.getInstance();
			MyfuncClass.function_two(context, pkgName);
		}
		
		        @Override
        public void registerCallback(FinishCallBack cb) throws RemoteException {
            if (cb != null) {
                finishCallBack = cb;
            }
        }

        @Override
        public void unregisterCallback(FinishCallBack cb) throws RemoteException {
            if(cb != null) {
                finishCallBack = cb;
            }
        }
		
	}
}

注册下Service:

<service android:name=".silent.InstallService">
            <intent-filter >
                <action android:name="com.company.server.ServerService"/>
            </intent-filter>
        </service>

客户端:

Step 1:把服务端的AIDL文件拷贝下,拷贝后目录如之前server端:

放在../main/aidl/com/company/server/aidl/下面

Step 2:编写简单的布局,再接着就是核心MainActvitiy的实现了
定义一个ServciceConnection对象,重写对应方法,和前面的普通数据的类似
再接着在bindService,然后再Button的点击事件中获取Salary对象并显示出来!


package com.client.demo;

import com.company.server.aidl.ItrainningService;
import com.company.server.aidl.FinishCallBack;

public class MainActivity extends AppCompatActivity {

    private Button silentinstalbtn;
   
    private Context mcontext;
    ItrainningService trainserviceInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mcontext = getApplication();
        
        //绑定service 
        Intent service = new Intent("com.company.server.ServerService")
                .setPackage("com.company.server");
        bindService(service, conn, BIND_AUTO_CREATE);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        silentbtn = (Button) findViewById(R.id.demobtn);

        silentbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                String packageName = "open";
                try {
                    if(true) {
                        new testThread("packageName","/sdcard/109.apk").start();
                    }
                } catch (RemoteException e) {
                    Log.e("demo"," error");
                    e.printStackTrace();
                }
            }
        });
    }

    private class testThread extends Thread {
        private String mPackageName;
        private String mPkgpath;
        testThread(String packageName, String pkgpath){
            super("UninstallTread");
            mPackageName = packageName;
            mPkgpath = pkgpath;
        }

        @Override
        public void run() {
            try {
            //调用service接口:
                trainserviceInterface.installSilent(mPackageName,mPkgpath);
            } catch (RemoteException e) {
                Log.e("demo","installTread  error");
                e.printStackTrace();
            }
        }
    }

    private FinishCallBack mCallback = new FinishCallBack.Stub() {
        @Override
        public void onFinish(String s,int a) throws RemoteException {//返回文件路径和错误代码
            if(a == 0) {
                Log.w("demo", "FinishCallBack success path == " + s + " int===" + a);
            }else{
                Log.e("demo", "FinishCallBack fail path == " + s + " int===" + a);
            }

        }
    };

...
    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {

        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            trainserviceInterface = ItrainningService.Stub.asInterface(service);
            try {
                trainserviceInterface.registerCallback(mCallback);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    };
}














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值