我一直在关注this教程,试图为android创建一个非常简单的时钟小部件。我能够创建小部件,但它似乎没有更新。我发现这很奇怪,因为我的代码似乎与android dev博客中的tutorial基本相同。
当我将小部件添加到主屏幕(Android 2.2模拟器)时,我在logCat中收到以下消息:
I/ActivityManager( 68): Starting activity: Intent { act=android.appwidget.action.APPWIDGET_PICK cmp=com.android.settings/.AppWidgetPickActivity (has extras) }
W/InputManagerService( 68): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44fbaf40
I/AppWidgetPickActivity( 135): EXTRA_CUSTOM_INFO not present.
I/ActivityManager( 68): Displayed activity com.android.settings/.AppWidgetPickActivity: 1181 ms (total 1181 ms)
W/ActivityManager( 68): Unable to launch app com.levold.customClockWidget/10036 for broadcast Intent { act=android.appwidget.action.APPWIDGET_ENABLED cmp=com.levold.customClockWidget/.ClockWidget }: process is bad
W/ActivityManager( 68): finishReceiver called but none active
W/ActivityManager( 68): Unable to launch app com.levold.customClockWidget/10036 for broadcast Intent { act=android.appwidget.action.APPWIDGET_UPDATE cmp=com.levold.customClockWidget/.ClockWidget (has extras) }: process is bad
W/ActivityManager( 68): finishReceiver called but none active
W/InputManagerService( 68): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44fe1c50
线Unable to launch app com.levold.customClockWidget/10036 for broadcast Intent { act=android.appwidget.action.APPWIDGET_ENABLED cmp=com.levold.customClockWidget/.ClockWidget }: process is bad
似乎是最相关的,但我找不到有关它的更多信息。
大部分代码如下。如果有人能提供帮助,那将是非常好的,我现在几乎陷入困境......
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.levold.customClockWidget"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<!-- Broadcast Receiver that will process AppWidget updates -->
<receiver android:name=".ClockWidget" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/custom_clock_widget" />
</receiver>
</application>
</manifest>
XML / custom_clock_widget.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="72dip"
android:updatePeriodMillis="1000"
android:initialLayout="@layout/main"
/>
的src / COM / levold / customClockWidget / ClockWidget.java
package com.levold.customClockWidget;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
public class ClockWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Toast.makeText(context,"Updating widget...", Toast.LENGTH_SHORT).show();
Log.i("clockWidget","Updating widget...");
}
答案 0 :(得分:5)
你的袖口看起来很精致。我不确定您是否可以从Toast
那样提出BroadcastReceiver
。更重要的是,请记住,它只会每半小时更新一次,因为android:updatePeriodMillis
不会接受低于半小时的值。
应用小部件并不是每秒都要更新。更新应用小部件涉及进程间通信调用,这并不便宜。
主屏幕时钟最好由主屏幕应用程序实现,而不是作为应用程序小部件实现。
答案 1 :(得分:0)
为了比每半小时更频繁地更新一个小部件,人们仍然可以使用android报警管理器。
像
这样的东西Intent intent = new Intent(<action Name>);
PendingIntent pendingIntent = PendingIntent.getBroadcast( <you widget class>.this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
然后您可以使用appwidget提供程序的Onreceive方法来处理事件。