我需要有关小部件的帮助。我用小部件创建了一个简单的应用程序。唯一的功能是用户通过单击窗口小部件打开应用程序(窗口小部件显示当前日期)。问题是用户在重新启动手机或语言更改后无法再通过单击打开小组件。 我上传了整个项目here。
请帮助我,这个问题阻止我在市场上发布两个应用程序......
由于
Hellowidget.java:
public class HelloWidget extends AppWidgetProvider {
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
private static final int REQUEST_CODE_ONE = 10;
String elso;
public static String MY_WIDGET_UPDATE = "MY_OWN_WIDGET_UPDATE";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
updateAppWidget(context, appWidgetManager, appWidgetId);
Toast.makeText(context, "onUpdate(): " + String.valueOf(i) + " : " + String.valueOf(appWidgetId), Toast.LENGTH_SHORT).show();
}
Intent intent = new Intent(context, UpdateService.class);
context.startService(intent);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
Intent configIntent = new Intent(context, MainActivity.class);
configIntent.setAction(ACTION_WIDGET_CONFIGURE);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, REQUEST_CODE_ONE, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.ImageView01, configPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if(MY_WIDGET_UPDATE.equals(intent.getAction())){
Bundle extras = intent.getExtras();
if(extras!=null) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), HelloWidget.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
if (appWidgetIds.length > 0) {
new HelloWidget().onUpdate(context, appWidgetManager, appWidgetIds);
}
}
}
}
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId){
}
}
UpdateService.java:
public final class UpdateService extends Service {
String datetoday;
@Override
public void onStart(Intent intent, int startId) {
final Calendar today = Calendar.getInstance();
int year_tod = today.get(Calendar.YEAR);
int month_tod = today.get(Calendar.MONTH)+1;
int day_tod = today.get(Calendar.DAY_OF_MONTH);
if (month_tod<10)
{
datetoday = String.valueOf(year_tod) + ".0" + String.valueOf(month_tod) + "." + String.valueOf(day_tod);
}
if (day_tod<10)
{
datetoday = String.valueOf(year_tod) + "." + String.valueOf(month_tod) + ".0" + String.valueOf(day_tod);
}
if ((month_tod<10) && (day_tod<10))
{
datetoday = String.valueOf(year_tod) + ".0" + String.valueOf(month_tod) + ".0" + String.valueOf(day_tod);
}
RemoteViews updateViews = new RemoteViews(this.getPackageName(), R.layout.main);
Date date = new Date();
java.text.DateFormat format = SimpleDateFormat.getDateInstance(
SimpleDateFormat.MEDIUM, Locale.getDefault());
updateViews.setTextViewText(R.id.widget_textview, datetoday);
ComponentName thisWidget = new ComponentName(this, HelloWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
}
public void onCreate(Intent intent, int startId) {
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
还有MainActivity.java用于活动,但这并不重要。
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bfarago.hellowidget"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".HelloWidget" 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/hello_widget_provider" />
</receiver>
<service android:enabled="true" android:name=".UpdateService" />
<activity android:name=".MainActivity"
android:label="@string/hello">
<intent_filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent_filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
小部件提供商:
<?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"
/>