设置特定的应用程序以忽略Android M中的代码优化

时间:2015-07-01 06:37:10

标签: android android-6.0-marshmallow

在新的VOIP版本中,电池优化功能会在某些条件下停止应用以节省电量,但​​对于需要一直保持活动的var app = angular.module('myApp', ['ngGrid']); app.controller('MyCtrl', function($scope, $http) { $scope.filterOptions = { filterText: "", useExternalFilter: true }; $scope.pagingOptions = { pageSizes: [25, 50, 100], pageSize: 25, totalServerItems: 0, currentPage: 1 }; $scope.setPagingData = function(data, page, pageSize){ var pagedData = data.slice((page - 1) * pageSize, page * pageSize); $scope.myData = pagedData; $scope.pagingOptions.totalServerItems = data.length; if (!$scope.$$phase) { $scope.$apply(); } }; $scope.getPagedDataAsync = function (pageSize, page, searchText) { setTimeout(function () { var data; if (searchText) { var ft = searchText.toLowerCase(); $http.get('largeLoad.json').success(function (largeLoad) { data = largeLoad.filter(function(item) { return JSON.stringify(item).toLowerCase().indexOf(ft) != -1; }); $scope.setPagingData(data,page,pageSize); }); } else { $http.get('largeLoad.json').success(function (largeLoad) { $scope.setPagingData(largeLoad,page,pageSize); }); } }, 100); }; $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage); $scope.$watch('pagingOptions', function () { console.log( "watch changed pagingOptions" ); $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText); }, true); $scope.$watch('filterOptions', function () { $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText); }, true); $scope.gridOptions = { data: 'myData', enablePaging: true, pagingOptions: $scope.pagingOptions, showFooter: true, filterOptions: $scope.filterOptions, columnDefs: [{ field: 'nm', displayName: 'Name', cellTemplate: '<div class="ngCellText"><div ng-show="!row.entity.edit">{{row.getProperty(col.field)}}</div>' + '<div ng-show="row.entity.edit" class="ngCellText"><input type="text" ng-model="row.entity.nm"/></div></div>' }, { field: 'cty', displayName: 'city', cellTemplate: '<div class="ngCellText"><div ng-show="!row.entity.edit">{{row.getProperty(col.field)}}</div>' + '<div ng-show="row.entity.edit" class="ngCellText"><input type="text" ng-model="row.entity.cty"/></div></div>' }, { field: 'hse', displayName: 'Address', cellTemplate: '<div class="ngCellText"><div ng-show="!row.entity.edit">{{row.getProperty(col.field)}}</div>' + '<div ng-show="row.entity.edit" class="ngCellText"><input type="text" ng-model="row.entity.hse"/></div></div>' }, { field: 'yrs', displayName: 'PinCode', cellTemplate: '<div class="ngCellText"><div ng-show="!row.entity.edit">{{row.getProperty(col.field)}}</div>' + '<div ng-show="row.entity.edit" class="ngCellText"><input type="text" ng-model="row.entity.yrs"/></div></div>' }, { displayName: 'Edit', cellTemplate: '<button id="editBtn" type="button" class="btn btn-primary" ng-click="edit(row)" >Modify</button> ' }, { displayName: 'Remove', cellTemplate: '<button id="removebtn" type="button" class="btn btn-primary" ng-click="removeRow($index)" >Remove</button> ' } ] }; $scope.selectedrowsInfo = []; // Selection setting $scope.gridOptions.primaryKey = "yrs"; $scope.gridOptions.enableRowSelection = true; $scope.gridOptions.showSelectionCheckbox = false; $scope.gridOptions.multiSelect = true; $scope.gridOptions.selectedItems = $scope.selectedrowsInfo; $scope.gridOptions.showColumnMenu = true; $scope.edit = function (row) { row.entity.edit = !row.entity.edit; }; $scope.removeRow = function() { var index = this.row.rowIndex; $scope.gridOptions.selectItem(index, false); $scope.myData.splice(index, 1); }; $scope.go = function($location) { $location.path('/form.html'); //$window.location.href = '/form.html'; }; }); 个应用,此功能会导致问题对开发者来说。

目前有一种方法可以绕过优化,即将app设置为忽略优化,但需要执行太多步骤。

我的问题是,有没有办法跳转到忽略特定应用的优化页面,如何知道我的应用是否被排除在优化之外?

3 个答案:

答案 0 :(得分:7)

警告:Google似乎不允许Play商店中的应用执行此操作。请参阅以下评论。

基于从MPA44I版本中的Settings.apk中提取的AndroidManifest.xml,您似乎可以通过下面的意图执行您想要的操作。

来自XML:

<intent
    android:action="android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"
    android:targetPackage="com.android.settings"
    android:data="package:PUT_YOUR_PACKAGE_NAME_HERE" />

来自代码:

try {
    Intent intent = new Intent(android.provider.Settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
    intent.setData(Uri.parse("package:PUT_YOUR_PACKAGE_NAME_HERE"));
    startActivity(intent);

} catch (ActivityNotFoundException e) {
    e.printStackTrace();
}

请注意,我自己没有时间在实践中对此进行验证,但该方法与我在其中一个应用中用于启动应用专用设置菜单的方法相同。不过,我希望它有所帮助。

答案 1 :(得分:3)

Google根据一些条件允许此操作 查看“支持其他用例”部分

here

同时检查White List Conditions

答案 2 :(得分:0)

目前无法有效选择退出优化,因为这不会影响打盹模式 - 即使您的应用位于白名单中。

见这里: https://stackoverflow.com/a/31721398/4301846

在这里: https://code.google.com/p/android-developer-preview/issues/detail?id=2225