Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to getPackageManager in fragment Android

Tags:

java

android

I have this app that I am clearing cache with, but I can't figure out how to get getPackageManager to work in the fragment. Here is my code. Any help would be very appreciated. I am very new to android and java. Thank you very much.

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;
    }
}
like image 289
dfuse06 Avatar asked Sep 22 '13 17:09

dfuse06


People also ask

Can fragment to activity in Android?

Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

What is fragmentation in Android Studio?

A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment.


2 Answers

you should be able to access getPackageManager(), through getActivity().

For instance getActivity().getPackageManager()

like image 125
Blackbelt Avatar answered Sep 28 '22 08:09

Blackbelt


Kotlin Extension Solution

Add this somewhere in your code:

val Fragment.packageManager get() = activity?.packageManager

And then simply use packageManager directly in your Fragment code

packageManager?.resolveActivity(intent, 0)

// or

fragment.packageManager?.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)
like image 39
Gibolt Avatar answered Sep 28 '22 07:09

Gibolt