gpt4 book ai didi

java - 如何在 fragment Android中获取PackageManager

转载 作者:IT老高 更新时间:2023-10-28 21:11:47 24 4
gpt4 key购买 nike

我有一个正在清除缓存的应用程序,但我不知道如何让 getPackageManager 在 fragment 中工作。这是我的代码。任何帮助将不胜感激。我对android和java很陌生。非常感谢。

public class Cache  extends SherlockFragment  {

Button cache;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.cache, null);
cache = (Button) view.findViewById(R.id.button3);

cache.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Cache is Cleared", Toast.LENGTH_SHORT).show();
PackageManager pm = getPackageManager();

// Get all methods on the PackageManager
java.lang.reflect.Method[] methods = pm.getClass().getDeclaredMethods();

for (java.lang.reflect.Method m : methods) {
if (m.getName().equals("freeStorageAndNotify")) {
// Found the method I want to use
try {
long desiredFreeStorage = Long.MAX_VALUE; // Request for free space
m.invoke(pm, desiredFreeStorage , null);
} catch (Exception e) {
// Method invocation failed. Could be a permission problem
}
break;
}
}
}

private PackageManager getPackageManager() {
// TODO Auto-generated method stub
return null;
}});
return view;
}
}

最佳答案

您应该能够通过 getActivity() 访问 getPackageManager()

例如 getActivity().getPackageManager()

关于java - 如何在 fragment Android中获取PackageManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18946471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com