android 使能/禁用四大组件pm.setComponentEnabledSetting无效

pm.setComponentEnabledSetting无效可能是因为获得pm的context不对,获得pm的context要一一对应,context获得时候的包名类名要与实际禁用掉的包名类名一致,如果需要禁掉多个图标时,需要多个context获得的pm去setComponentEnabledSetting

举例:应用场景为切换用户的时候要求禁用掉桌面某个或多个图标

frameworks\base\packages\SystemUI\src\com\android\systemui\GuestResumeSessionReceiver.java 

//xiaozheng add 
import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.app.ActivityManager;

 if (Intent.ACTION_USER_SWITCHED.equals(action)) {
            cancelDialog();
            int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
//xiaozheng add start
Context mActivationContext;
if(userId!=UserHandle.USER_OWNER){
try {

mContext=                                           

                                         context.createPackageContextAsUser("包名", 0,

new UserHandle(ActivityManager.getCurrentUser()));
} catch (NameNotFoundException e) {
Log.i("xiaozheng", "get context error!");
mActivationContext = context;
}
PackageManager pm = mContext.getPackageManager();
ComponentName cn = new ComponentName("包名", 
"类名"); 
pm.setComponentEnabledSetting(cn,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,0);
//xiaozhen add end 
}


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页