android-system:允许应用程序绑定到系统服务

android-system: allowing an app to bind to a system service

这个答案是关于如何在android中添加系统服务的重点:

但是,每次 android makesystem 构建我的东西时我懒得等待几个小时,所以我通过 android-studio 将我的服务作为常规 apk 部署到 /system/app 然后在应该使用我做的服务的应用程序中:

private ServiceConnection myAidlConnection = new ServiceConnection(){
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        // do stuff
    }
}

public void bindService(Context context){
    Intent intent = new Intent();
    intent.setClassName("my.service","my.service.Service");
    intent.setAction("my.service.TOAST");
    intent.putExtra("text", "stulle");
    context.bindService(intent, myAidlConnection, Context.BIND_AUTO_CREATE);
}

这很好用,只要该应用也是系统 apk。如果它不是系统 apk,则永远不会调用 myAidlConnection.onServiceConnected。我想这是某种安全问题,因此您不能从非特权应用程序绑定任意系统服务。

有什么方法可以让我的系统服务被任何人绑定,还是真的必须通过getSystemService()传递绑定?

第 3 方应用程序可以绑定到任何 "exported" 服务,无论它是否是系统服务。
你的 "my.service.Service" 出口了吗?

有关"exported"的更多信息:http://developer.android.com/guide/topics/manifest/service-element.html#exported