【问题标题】:Battery Widget did not work, won't refresh the status of the batteryBattery Widget 不工作,不会刷新电池状态
【发布时间】:2014-09-11 23:16:30
【问题描述】:

我不知道,我做错了什么。 启动小部件后,我可以看到电池状态,但是当我手动给 telnet 下“电源容量 xx”命令时,它根本不刷新。

MainActivity.java

public class MainActivity extends AppWidgetProvider
{
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);

        Intent received = context.getApplicationContext()
                .registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        String receivedAction = received.getAction();

        if (receivedAction.equals(Intent.ACTION_BATTERY_CHANGED))
        {
            int level = received.getIntExtra("level", 0);
            views.setTextViewText(R.id.TextView1, level + "%!");

            ComponentName appComponent = new ComponentName(context, MainActivity.class);
            appWidgetManager.getInstance(context).updateAppWidget(appComponent, views);
        }



    }
}

widgetxml.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="72dp"
    android:minHeight="72dp"
    android:updatePeriodMillis="1800000"
    android:initialLayout="@layout/activity_main"
    >

</appwidget-provider>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/TextView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.widgetownia"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:icon="@drawable/ic_launcher" >

        <receiver android:name=".MainActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"> </action>
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widgetxml" > </meta-data>
        </receiver>

    </application>
</manifest>

【问题讨论】:

    标签: java android eclipse


    【解决方案1】:

    当您使用 AppWidgetProvider 时,您可以从任何活动中更新您的小部件,或者在您的情况下,您可以从服务中更新它。检查以下链接以了解如何刷新小部件。

    Programmatically update widget from activity/service/receiver

    Force Android widget to update

    还可以查看 vogella 提供的精彩教程,解释有关小部件的所有内容。它还显示了如何通过单击它或从任何服务更新小部件。

    http://www.vogella.com/tutorials/AndroidWidgets/article.html

    【讨论】:

      猜你喜欢
      • 2018-12-26
      • 2017-04-14
      • 2012-07-21
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2021-03-20
      相关资源
      最近更新 更多