Andoid OS 内置非system UID service 持续启动和API权限配置

1. 非system UID service 持续启动

从android O 开始,android 不允许非system uid service在后台默默运行了。大概启动运行了一分多种后,就会系统kill。

普通service要持续在系统中运行,需要service和notification结合使用,告诉用户有个service在持续运行中。

示例代码如下:

   @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        //区别于system UID service, 普通service在前台启动service
        startForeground(1, getNotificationfromChannel(mContext)); 
    }
    
    private Notification getNotificationfromChannel(Context mContext) {

        String CHANNEL_ID_STRING = "channel";

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationChannel channel = new NotificationChannel(CHANNEL_ID_STRING, "running", NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(channel);
        channel.enableVibration(false);
        channel.enableLights(false);

        notification = new Notification.Builder(getApplicationContext(), CHANNEL_ID_STRING)
                .setSmallIcon(android.R.drawable.sym_def_app_icon)  //这个icon设置必须加上,否则会service无法启动
                .setOngoing(true)
                .setAutoCancel(false)
                .setPriority(Notification.PRIORITY_MAX)
                .setOnlyAlertOnce(true)
                .setContentIntent(PendingIntent.getService(mContext, 0,
                        new Intent(), PendingIntent.FLAG_IMMUTABLE))
                .build();

        return notification;
    }

2. android OS 内置非system uid app如何调用SystemProperties.set接口

2.1 调用接口

Google使用UnsupportedAppUsage来限制framework中的某些定义无法被外部应用访问,对于SystemProperties.set就用了UnsupportedAppUsage做限制。非system uid app调用SystemProperties.set接口,则编译会失败。

好在Google暂时还未限制Java的反射机制,所以内置普通app可以通过反射的方法来调用这个接口。

public static void setSystemProperty(String key, String value) {

    try {

        Method set = Class.forName("android.os.SystemProperties")

        .getDeclaredMethod("set", String.class, String.class);

        set.invoke(null, key, value);

    } catch (Exception e) {

        e.printStackTrace();

    }

}

2.2权限申请

Google虽然让内置非system uid appapp用反射跳过API权限限制,但是在sepolicy这边做了限制。既然是内置app,那么就可以修改sepolicy基于权限。

首先,内置app必须是platform签名,作为一个platform app。非platform app,下面的方法加上也是玩不转的。

LOCAL_CERTIFICATE := platform

其次,为这个app设置一个sepolicy文件,我给它取名my1379servic_app.te,在这里添加app所需要的所有权限,比如这里,我需要setproperty的权限,放心加上。

type my1379servic_app, domain;
app_domain(my1379servic_app);

allow my1379servic_app{ app_api_service activity_service }:service_manager find;

allow my1379servic_app init:unix_stream_socket connectto;
allow my1379servic_appproperty_socket:sock_file write;
allow my1379servic_app debug_prop:property_service set;
allow my1379servic_appdebug_prop:file { getattr map open read };

最后,修改seapp_contexts 把你的app package name和my1379servic_app domain绑定

user=_app seinfo=platform name=com.my.my1379service domain=my1379servic_app type=app_data_file levelFrom=all

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值