0

我是 Android 新手,我有一点问题,我已经搜索了多个论坛,但一无所获..

我有一个显示电池的简单小部件,单击它会打开一个带有弹出主题的活动。

所以我也有这个文件,可能是某些方法错误?

小部件提供者:

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);
    }
}

在我的服务中:

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();
    }
}

有人可以帮帮我吗?除此之外,该服务有 30MB... 在运行时

4

1 回答 1

2

让我们为您简化:

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);
        }
    }
};
于 2013-04-30T19:13:10.523 回答