0

I'm new on Android and I have a bit issue, I've already searched multiple forums and nothing..

I have a simple widget that shows the battery and on click it opens a activity with Popup Theme.

So I have this too files, may be some method wrong?

WidgetProvider:

public class Widget extends AppWidgetProvider {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        super.onReceive(context, intent);
        context.startService(new Intent(context, Service.class));
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // TODO Auto-generated method stub
        ComponentName thisWidget = new ComponentName(context,
                Widget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(context);

        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
        for (int widgetId : allWidgetIds) {

            RemoteViews updateViews = new RemoteViews(context.getPackageName(),
                    R.layout.widget_layout);

            Intent intent = new Intent(context, Popup.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                    intent, 0);
            updateViews.setOnClickPendingIntent(R.id.widget_layout,
                    pendingIntent);

            manager.updateAppWidget(widgetId, updateViews);

        }
    }

    @Override
    public void onDisabled(Context context) {
        // TODO Auto-generated method stub
        context.stopService(new Intent(context, Service.class));
        super.onDisabled(context);
    }
}

And on my service:

public class Service extends Service {

    private int batteryLevel = 0;
    RemoteViews updateViews;

    private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
                batteryLevel = intent.getIntExtra("level", 0);
                updateViews = new RemoteViews(context.getPackageName(),
                        R.layout.widget_layout);

                updateViews.setImageViewBitmap(R.id.imgBattery,
                buildBitmap(batteryLevel + "%", context));

                ComponentName thisWidget = new ComponentName(context,
                        Widget.class);
                AppWidgetManager manager = AppWidgetManager
                        .getInstance(context);
                manager.updateAppWidget(thisWidget, updateViews);
            }
        }
    };

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(receiver, intentFilter);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        unregisterReceiver(Receiver);
        super.onDestroy();
    }
}

Can someone please help me? and besides that, the service has 30MB... while running

Emanuel Moecklin
  • 28,488
  • 11
  • 69
  • 85
FilipeOS
  • 801
  • 1
  • 11
  • 42

1 Answers1

2

Let's make this easy for you:

private BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
            batteryLevel = intent.getIntExtra("level", 0);
            updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

            updateViews.setImageViewBitmap(R.id.imgBattery, buildBitmap(batteryLevel + "%", context));

            Intent intent = new Intent(context, Popup.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            updateViews.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);

            ComponentName thisWidget = new ComponentName(context, Widget.class);
            AppWidgetManager manager = AppWidgetManager.getInstance(context);
            manager.updateAppWidget(thisWidget, updateViews);
        }
    }
};
Emanuel Moecklin
  • 28,488
  • 11
  • 69
  • 85
  • Thanks! 1. And what I remove on onUpdate (Widget class)? 2. onEnabled I've passed the start service, It's fine? Because when I restart my phone It only updates on battery changed.. – FilipeOS Apr 30 '13 at 21:42
  • 1. Sorry I don't understand your first question 2. onEnabled() is only called when an instance the App Widget is created for the first time. For example, if the user adds two instances of your App Widget, this is only called the first time. That's why the service isn't started after a reboot. Start the service from onUpdate() or onReceive() but make sure it happens only once during the lifecycle of the app. Use e.g. a static boolean variable to check whether it's already started or not. – Emanuel Moecklin May 01 '13 at 01:45
  • 1. I must delete something on my widget class since I've made the intent on service too? to remove duplicated code? 2. I've used this example http://stackoverflow.com/questions/600207/android-check-if-a-service-is-running where is not running then start it, good idea? something better to improve? THANKS! – FilipeOS May 02 '13 at 08:57