Search code examples
androidandroid-widget

Android Widget : Displaying RAM


I am trying to create a widget for my app that displays the device's CPU usage, Battery charge and remaining RAM; not the storage space.

The CPU level and battery charge code works fine; but the RAM code does not. I have read that in order to use .getSystemService() in an AppWidgetProvider you must pass a context from an activity to here.

The issue is, when the method tries to set up a systemService with an ActivityManager, it throws :

01-11 21:29:21.468: E/AndroidRuntime(30759): java.lang.RuntimeException: Unable to instantiate receiver com.example.myfirstappex.AppWidget: java.lang.NullPointerException

I have narrowed the problem to this block of code :

    import com.example.myfirstappex.MainActivity;

    public class AppWidget extends AppWidgetProvider {
    ...
    public long getRam()
{

    MemoryInfo mi = new MemoryInfo();

    Context mainCon = MainActivity.getAppContext();
    ActivityManager activityManager = (ActivityManager)mainCon.getSystemService(Context.ACTIVITY_SERVICE);

    activityManager.getMemoryInfo(mi);


    long availableMegs = mi.availMem / 1048576L;

    //Returning the value
    return(availableMegs);

}
    ...
    }

Any Ideas on how to fix this issue? Or an alternative to getting and displaying the RAM in a widget?

EDIT : The entire error log is :

    01-11 22:21:32.426: E/AndroidRuntime(32059): FATAL EXCEPTION: main
    01-11 22:21:32.426: E/AndroidRuntime(32059): java.lang.RuntimeException: Unable to start receiver com.example.myfirstappex.AppWidget: java.lang.NullPointerException

    01-11 22:21:32.426: E/AndroidRuntime(32059):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2383)

    01-11 22:21:32.426: E/AndroidRuntime(32059):    at android.app.ActivityThread.access$1500(ActivityThread.java:141)

    01-11 22:21:32.426: E/AndroidRuntime(32059):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)

    01-11 22:21:32.426: E/AndroidRuntime(32059):    at android.os.Handler.dispatchMessage(Handler.java:99)

     01-11 22:21:32.426: E/AndroidRuntime(32059):   at android.os.Looper.loop(Looper.java:137)

     01-11 22:21:32.426: E/AndroidRuntime(32059):   at android.app.ActivityThread.main(ActivityThread.java:5039)

     01-11 22:21:32.426: E/AndroidRuntime(32059):   at java.lang.reflect.Method.invokeNative(Native Method)

     01-11 22:21:32.426: E/AndroidRuntime(32059):   at java.lang.reflect.Method.invoke(Method.java:511)

     01-11 22:21:32.426: E/AndroidRuntime(32059):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

     01-11 22:21:32.426: E/AndroidRuntime(32059):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

     01-11 22:21:32.426: E/AndroidRuntime(32059):   at dalvik.system.NativeStart.main(Native Method)

    01-11 22:21:32.426: E/AndroidRuntime(32059): Caused by: java.lang.NullPointerException

    01-11 22:21:32.426: E/AndroidRuntime(32059):    at com.example.myfirstappex.AppWidget.getRam(AppWidget.java:82)

    01-11 22:21:32.426: E/AndroidRuntime(32059):    at com.example.myfirstappex.AppWidget.onUpdate(AppWidget.java:35)

    01-11 22:21:32.426: E/AndroidRuntime(32059):    at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:66)

    01-11 22:21:32.426: E/AndroidRuntime(32059):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2376)

    01-11 22:21:32.426: E/AndroidRuntime(32059):    ... 10 more

This only happens when the main app is not running.


Solution

  • According to the error logs, your issue is on line 82 of AppWidget.java. Based on a hunch, I think that's ActivityManager activityManager = .... However, if that's not line 82, have a look there instead.

    That aside, if I'm right, you'll need to get the context from elsewhere. The following methods are those which are part of your AppWidget class:

    onDeleted(Context context, int[] appWidgetIds)
    onDisabled(Context context)
    onEnabled(Context context)
    onReceive(Context context, Intent intent)
    onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    

    When you call getRam(), you should pass it a Context object from whichever method you're currently using; in this case, that's onUpdate(). Then, you need to modify getRam() to use that:

    public long getRam(Context context) {
        // ...
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        // ...
    }