Android:缺少Intent extra(使用widget)

时间:2014-04-18 20:08:21

标签: android android-intent widget android-pendingintent

我想制作一个带有2个按钮的小部件(一个是带有数字的Textview)。一个按钮应该增加数字,另一个减少它。

这是我的变量(2x动作名称和数字的int)

    private static final String PLUS_ONE = "Plus1";
    private static final String MINUS_ONE = "minus1";
    static int counter = 0;

onUpdate():

 @Override
 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
 final int N = appWidgetIds.length;
 for (int i=0; i<N; i++) {
     int appWidgetId = appWidgetIds[i];
     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.plus1counter_widget);
     remoteViews.setOnClickPendingIntent(R.id.counter_widget, getPendingSelfIntent(context, PLUS_ONE, counter));
     remoteViews.setOnClickPendingIntent(R.id.img_downWid, getPendingSelfIntent(context, MINUS_ONE, counter));

     appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
 }}

getPendingSelfIntent是我创建intent的方法,在其中放入一个int值并返回一个PendingIntent 在此详细说明:

    protected PendingIntent getPendingSelfIntent(Context context, String action, int i) {
    Intent intent = new Intent(context, Plus1CounterAppWidgetProvider.class);
    intent.setAction(action);
    intent.putExtra("safeInt", i);
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);}

并最终我的onReturn(),我想在其中将计数器设置为我给Intent的值并执行操作:

 @Override
 public void onReceive(Context context, Intent intent) {
 super.onReceive(context, intent);
 counter = intent.getIntExtra("safeInt", 0); 

 if (PLUS_ONE.equals(intent.getAction())) {
     counter++;
     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.plus1counter_widget);
     remoteViews.setTextViewText(R.id.counter_widget, Integer.toString(counter));
     AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context, Plus1CounterAppWidgetProvider.class), remoteViews);
 }

 if (MINUS_ONE.equals(intent.getAction())) {
     counter--;
     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.plus1counter_widget);
     remoteViews.setTextViewText(R.id.counter_widget, Integer.toString(counter));
     AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context, Plus1CounterAppWidgetProvider.class), remoteViews);
 }}

致电

counter = intent.getIntExtra("safeInt", 0); 

计数器始终设置为0,这意味着没有额外的值。 我只是不明白为什么!任何方案? :)

1 个答案:

答案 0 :(得分:0)

我找到了另一个解决方案。 我只使用了SharedPreferences而不是Intent,它起作用了! :) 这是代码:

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.RemoteViews;

public class Plus1CounterAppWidgetProvider extends AppWidgetProvider{

private static final String PLUS_ONE = "Plus1";
private static final String MINUS_ONE = "Minus1";
private static final String REFRESH_COUNTER = "Refresh";
static int counter = 0;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[]  appWidgetIds) {
 final int N = appWidgetIds.length;
 for (int i=0; i<N; i++) {
     int appWidgetId = appWidgetIds[i];
     RemoteViews remoteViews = new RemoteViews(context.getPackageName(),  R.layout.plus1counter_widget);
     remoteViews.setOnClickPendingIntent(R.id.counter_widget, getPendingSelfIntent(context, PLUS_ONE));
     remoteViews.setOnClickPendingIntent(R.id.img_downWid, getPendingSelfIntent(context, MINUS_ONE));
     remoteViews.setOnClickPendingIntent(R.id.img_refreshWid, getPendingSelfIntent(context, REFRESH_COUNTER));
     appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
  }
 }

 @Override
 public void onReceive(Context context, Intent intent) {
 super.onReceive(context, intent);

 SharedPreferences sharedPref = context.getSharedPreferences("com.plus1counter.safeFile", Context.MODE_PRIVATE);
 counter = sharedPref.getInt("safeFile", 0);
 if(counter == 100){
     counter = 0;
 }

 if (PLUS_ONE.equals(intent.getAction())) {
     counter++;
     writeSharedPrefs(sharedPref);         
 }

 if (MINUS_ONE.equals(intent.getAction())) {
     counter--;
     writeSharedPrefs(sharedPref);
 }

 if(REFRESH_COUNTER.equals(intent.getAction())){
     counter = 0;
     writeSharedPrefs(sharedPref);
 }
 RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.plus1counter_widget);
 remoteViews.setTextViewText(R.id.counter_widget, Integer.toString(counter));
 AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context, Plus1CounterAppWidgetProvider.class), remoteViews);
 }

 public void writeSharedPrefs(SharedPreferences sharedPref){
 SharedPreferences.Editor editor = sharedPref.edit();
 editor.putInt("safeFile", counter);
 editor.commit();
 }

 protected PendingIntent getPendingSelfIntent(Context context, String action) {
 Intent intent = new Intent(context, Plus1CounterAppWidgetProvider.class);
 intent.setAction(action);
 return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
 }

 }