如何使我的小部件可点击?

发布于 2024-10-15 19:10:43 字数 8461 浏览 1 评论 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(我想通过单击我的小部件打开的 Activity):

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

            }
        }

    };
}


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

这里是我的 BatteryReceiver:

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



}

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

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

My widget is just a battery monitor.
But i want to open a full screen activity with more info.

here is the 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);
    }

}

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

Here Battery.java (The Activity i want to open by clicking on my widget):

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

            }
        }

    };
}


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

Here my BatteryReceiver:

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



}

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

The widget works well, but i couldn´t find any way to make is clickable!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烈酒灼喉 2024-10-22 19:10:43

修改 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);
    ...
}

Modify updateWidget() to create and assign a new 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);
    ...
}
蓝眸 2024-10-22 19:10:43

确定之前,

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

ok Before

 manager.updateAppWidget(myComponentName, updateViews);

you have to define an intent that is triggered when you click for example an imagen in your Widget (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);
苦妄 2024-10-22 19:10:43

要使 Widget 可点击,您需要在 Widget 的远程视图上调用 setOnClickPendingIntent,传入要使其可点击的视图的资源标识符,以及您希望其触发的 Pending Intent。

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

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

updateViews.setOnClickPendingIntent(R.id.level, pi);

To make a Widget clickable you need to call setOnClickPendingIntent on the widget's Remote View, passing in the resource identifier of the View you want to make clickable, and the Pending Intent you want it to fire.

In your example it would look something like this:

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

updateViews.setOnClickPendingIntent(R.id.level, pi);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文