我正在使用重置按钮制作计数小部件。我遇到的问题是,在我在模拟器上安装后,弹出配置活动,点击确定后只有一个按钮正常工作(+1按钮有效,但重置按钮不起作用)。如果我在小部件仍在模拟器上运行时从Eclipse运行程序,那么它可以工作。如果我将它拖到垃圾箱然后重新打开它,它会在配置活动后停止工作。我试图删除配置活动,但它只是说没有安装应用程序。
我对此很陌生,我很感激任何帮助。
我的代码如下:
对于配置活动:
package com.diego.widgetprueba;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class WidgetConfig extends Activity {
private int widgetId = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.widget_config);
Intent intentOrigen = getIntent();
Bundle params = intentOrigen.getExtras();
widgetId = params.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
final Button btnAceptar = (Button)findViewById(R.id.BtnAceptar);
final Button btnCancelar = (Button)findViewById(R.id.BtnCancelar);
setResult(RESULT_CANCELED);
btnCancelar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//Devolvemos como resultado: CANCELAR (RESULT_CANCELED)
finish();
}
});
btnAceptar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(WidgetConfig.this);
MiWidget.actualizarWidget(WidgetConfig.this, appWidgetManager, widgetId);
MiWidget.resetearWidget(WidgetConfig.this, appWidgetManager, widgetId);
Intent resultado = new Intent();
resultado.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
setResult(RESULT_OK, resultado);
finish();
}
});
}
}
对于AppWidgetProvider
package com.diego.widgetprueba;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.RemoteViews;
public class MiWidget extends AppWidgetProvider {
static int mCount=0;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++)
{
int widgetId = appWidgetIds[i];
actualizarWidget(context, appWidgetManager, widgetId);
resetearWidget(context, appWidgetManager, widgetId);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds)
{
SharedPreferences prefs = context.getSharedPreferences("WidgetPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
for(int i=0; i<appWidgetIds.length; i++)
{
int widgetId = appWidgetIds[i];
editor.remove("msg_" + widgetId);
}
editor.commit();
super.onDeleted(context, appWidgetIds);
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.diego.widgetprueba.ACTUALIZAR_WIDGET")) {
int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
if (widgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
actualizarWidget(context, widgetManager, widgetId);
}
}
if (intent.getAction().equals("com.diego.widgetprueba.RESETEAR_WIDGET")) {
int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
if (widgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
resetearWidget(context, widgetManager, widgetId);
}
}
super.onReceive(context, intent);
}
public static void actualizarWidget(Context context, AppWidgetManager appWidgetManager, int widgetId)
{
RemoteViews controles = new RemoteViews(context.getPackageName(), R.layout.main);
Intent intent = new Intent("com.diego.widgetprueba.ACTUALIZAR_WIDGET");
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, widgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
controles.setOnClickPendingIntent(R.id.BtnActualizar, pendingIntent);
mCount++;
String mCountString = Integer.toString(mCount);
controles.setTextViewText(R.id.LblHora, mCountString);
appWidgetManager.updateAppWidget(widgetId, controles);
}
public static void resetearWidget(Context context, AppWidgetManager appWidgetManager, int widgetId)
{
RemoteViews controles = new RemoteViews(context.getPackageName(), R.layout.main);
Intent intent2 = new Intent("com.diego.widgetprueba.RESETEAR_WIDGET");
intent2.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, widgetId, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
controles.setOnClickPendingIntent(R.id.BtnResetear, pendingIntent2);
mCount=0;
String mCountString = Integer.toString(mCount);
controles.setTextViewText(R.id.LblHora, mCountString);
appWidgetManager.updateAppWidget(widgetId, controles);
}
}
清单文件是
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.diego.widgetprueba"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity android:name=".WidgetConfig">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
<receiver android:name=".MiWidget" android:label="@string/miprimerwidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="com.diego.widgetprueba.ACTUALIZAR_WIDGET"/>
</intent-filter>
<intent-filter>
<action android:name="com.diego.widgetprueba.RESETEAR_WIDGET"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@layout/miwidget_provider" />
</receiver>
</application>
</manifest>
干杯
答案 0 :(得分:0)
我通过在AppWidgetProvider中创建以下函数来解决它
public static void configurarWidget(Context context, AppWidgetManager appWidgetManager, int widgetId)
{
//Obtenemos la lista de controles del widget actual
RemoteViews controles = new RemoteViews(context.getPackageName(), R.layout.main);
//Asociamos los 'eventos' al widget
Intent intent = new Intent("com.diego.widgetprueba.ACTUALIZAR_WIDGET");
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, widgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent2 = new Intent("com.diego.widgetprueba.RESETEAR_WIDGET");
intent2.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, widgetId, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
controles.setOnClickPendingIntent(R.id.BtnActualizar, pendingIntent);
controles.setOnClickPendingIntent(R.id.BtnResetear, pendingIntent2);
//Actualizamos el mensaje en el contador
mCount=0;
String mCountString = Integer.toString(mCount);
controles.setTextViewText(R.id.LblHora, mCountString);
//Notificamos al manager de la actualizaciÛn del widget actual
appWidgetManager.updateAppWidget(widgetId, controles);
}
然后在配置活动中更改这两行
MiWidget.actualizarWidget(WidgetConfig.this, appWidgetManager, widgetId);
MiWidget.resetearWidget(WidgetConfig.this, appWidgetManager, widgetId);
代表
MiWidget.configurarWidget(WidgetConfig.this, appWidgetManager, widgetId);