IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Andoid OS 内置非system UID service 持续启动和API权限配置 -> 正文阅读

[移动开发]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

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2022-10-22 21:26:57  更:2022-10-22 21:28:01 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 1:53:19-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码