0

我的小部件只是一个电池监视器。但我想打开一个包含更多信息的全屏活动。

这是 AndroidBatteryWidgetProvider.java:

package cogiloo.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class AndroidBatteryWidgetProvider extends AppWidgetProvider {


    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // TODO Auto-generated method stub
        updateWidget(context);
        context.startService(new Intent(context, MyBatteryReceiver.class));     

    }

    public void updateWidget(Context context){
        RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.androidbatterywidget_layout);
        updateViews.setTextViewText(R.id.level, "waiting!");

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

}

////////////////////////////////////////////////////////////////////////////

这里 Battery.java (我想通过单击我的小部件打开的活动):

package cogiloo.widget;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.widget.TextView;

public class Battery extends Activity {

    private TextView batteryLevel, batteryVoltage, batteryTemperature, 
                batteryTechnology, batteryStatus, batteryHealth;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        batteryLevel = (TextView)findViewById(R.id.batterylevel);
        batteryVoltage = (TextView)findViewById(R.id.batteryvoltage);
        batteryTemperature = (TextView)findViewById(R.id.batterytemperature);
        batteryTechnology = (TextView)findViewById(R.id.batterytechology);
        batteryStatus = (TextView)findViewById(R.id.batterystatus);
        batteryHealth = (TextView)findViewById(R.id.batteryhealth);

        this.registerReceiver(this.myBatteryReceiver, 
                new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }

    private BroadcastReceiver myBatteryReceiver 
    = new BroadcastReceiver(){

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub

            if (arg1.getAction().equals(Intent.ACTION_BATTERY_CHANGED)){
                batteryLevel.setText("Level: "
                        + String.valueOf(arg1.getIntExtra("level", 0)) + "%");
                batteryVoltage.setText("Voltage: "
                        + String.valueOf((float)arg1.getIntExtra("voltage", 0)/1000) + "V");
                batteryTemperature.setText("Temperature: "
                        + String.valueOf((float)arg1.getIntExtra("temperature", 0)/10) + "c");
                batteryTechnology.setText("Technology: " + arg1.getStringExtra("technology"));

                int status = arg1.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
                String strStatus;
                if (status == BatteryManager.BATTERY_STATUS_CHARGING){
                    strStatus = "Charging";
                } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING){
                    strStatus = "Dis-charging";
                } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING){
                    strStatus = "Not charging";
                } else if (status == BatteryManager.BATTERY_STATUS_FULL){
                    strStatus = "Full";
                } else {
                    strStatus = "Unknown";
                }
                batteryStatus.setText("Status: " + strStatus);

                int health = arg1.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN);
                String strHealth;
                if (health == BatteryManager.BATTERY_HEALTH_GOOD){
                    strHealth = "Good";
                } else if (health == BatteryManager.BATTERY_HEALTH_OVERHEAT){
                    strHealth = "Over Heat";
                } else if (health == BatteryManager.BATTERY_HEALTH_DEAD){
                    strHealth = "Dead";
                } else if (health == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE){
                    strHealth = "Over Voltage";
                } else if (health == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE){
                    strHealth = "Unspecified Failure";
                } else{
                    strHealth = "Unknown";
                }
                batteryHealth.setText("Health: " + strHealth);

            }
        }

    };
}


/////////////////////////////////////////////////////////////

这是我的电池接收器:

package cogiloo.widget;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.IBinder;
import android.widget.RemoteViews;

public class MyBatteryReceiver extends Service {

    private int batterylevel = 0;
    private String batteryStatus ="";

    private BroadcastReceiver myReceiver = 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);

                int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
                String strStatus;
                if (status == BatteryManager.BATTERY_STATUS_CHARGING){
                    batteryStatus = "Charging"; 
                } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING){
                    batteryStatus = "Dis-charging";
                } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING){
                    batteryStatus = "Not charging";
                } else if (status == BatteryManager.BATTERY_STATUS_FULL){
                    batteryStatus = "Full";
                } else {
                    batteryStatus = "";
                }

                updateAppWidget(context);
            }
        }

        public void updateAppWidget(Context context){
            RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.androidbatterywidget_layout);
            updateViews.setTextViewText(R.id.level, 
                    "Cogiloo Battery\n\n" +
                    "Bat. Status:\n" +
                    "Level: " + batterylevel + "%\n" +
                    "Status: " + batteryStatus);

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

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

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(myReceiver, intentFilter);



    }

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



    //borrar esto y el activity Battery y en el maniffest


    public void onClick() {
        // TODO Auto-generated method stub
        Intent i = new Intent(this, Battery.class);
        startActivity(i);
    }

//hasta aqui



}

/////////////////////////////////////////////////////////

该小部件运行良好,但我找不到任何可点击的方法!

4

3 回答 3

5

修改updateWidget()以创建并分配一个新的PendingIntent.

public void updateWidget(Context context){
    RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.androidbatterywidget_layout);
    Intent intent = new Intent(context, Battery.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    updateViews.setOnClickPendingIntent(R.id.widget, pendingIntent);
    ...
}
于 2011-02-01T17:59:51.483 回答
3

好的 之前

 manager.updateAppWidget(myComponentName, updateViews);

您必须定义一个在您单击小部件(R.id.imagetoClick)中的图像时触发的意图

Intent defineIntent = new Intent(); 
    defineIntent.setClassName("cogiloo.widget","cogiloo.widget.ActivitytoShow");
PendingIntent pendingIntent = PendingIntent.getActivity(context,
0 /* no requestCode */, defineIntent, 0 /* no flags */);
    updateViews.setOnClickPendingIntent(R.id.imagetoClick, pendingIntent);
于 2011-02-01T17:54:37.257 回答
1

要使小部件可点击,您需要调用setOnClickPendingIntent小部件的远程视图,传入要使其可点击的视图的资源标识符,以及要触发的待定意图。

在您的示例中,它看起来像这样:

Intent intent = new Intent(context, Battery.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

updateViews.setOnClickPendingIntent(R.id.level, pi);
于 2011-02-01T17:57:42.763 回答