7***@qq.com
7***@qq.com
  • 发布:2021-03-12 17:08
  • 更新:2021-03-12 17:08
  • 阅读:1887

安卓设置系统休眠时间-安卓原生方法

分类:uni-app
 //获取默认休眠时间  
    @UniJSMethod(uiThread = true)  
    public void GetDormant(final JSCallback callback) {  
        try{  
            Context context = mWXSDKInstance.getContext();  
            int systemGravity = Settings.System.getInt(context.getContentResolver(),android.provider.Settings.System.SCREEN_OFF_TIMEOUT,0);  
            callback.invoke(systemGravity);  
        }catch (Exception ex){  
            callback.invoke(false);  
            Log.e(TAG, "setDormant: "+ex);  
        }  
    }  
    //设置系统休眠时间  
    @UniJSMethod(uiThread = true)  
    public void setDormant(int time,final JSCallback callback) {  
        try{  
            Context context = mWXSDKInstance.getContext();  
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {  
                if (!Settings.System.canWrite(context)) {  
                    Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);  
                    intent.setData(Uri.parse("package:" + context.getPackageName()));  
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
                    context.startActivity(intent);  
                } else {  
                    //有了权限,具体的动作  
                    Settings.System.putInt(context.getContentResolver(),  
                            Settings.System.SCREEN_OFF_TIMEOUT, time);  
                    Uri uri = Settings.System  
                            .getUriFor(Settings.System.SCREEN_OFF_TIMEOUT);  
                    context.getContentResolver().notifyChange(uri, null);  
                    Log.e(TAG, "setDormant: 设置完成" );  
                    callback.invoke(true);  
                }  
            }  
        }catch (Exception ex){  
            callback.invoke(false);  
            Log.e(TAG, "setDormant: "+ex);  
        }  
    }
0 关注 分享

要回复文章请先登录注册