0
我正在使用重置按鈕製作計數窗口小部件。我遇到的問題是,我將它安裝在模擬器上後,彈出配置活動,點擊確定後,只有一個按鈕可以工作(+1按鈕有效,但重置按鈕不起作用)。如果我在Eclipse中運行程序,而小部件仍在仿真器上運行,那麼它就可以工作。如果我將它拖到垃圾箱然後重新打開它,它會在配置活動後停止工作。我試圖刪除配置活動,但它只是說應用程序沒有安裝。配置活動:窗口小部件只在從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>
個乾杯