本文介绍了窗口小部件的onReceive犯规增量INT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前了解的Andr​​oid部件。我想增加计数器的值,但是当我点击小窗口从0增加它1并停止增加它。我的 onRecieve 方法做工精细。
这里是我的code:

公共静态了checkdate了checkdate;
公共静态EVENTLIST EVENTLIST;公共静态字符串EVENT_CLICK =EventClick;
公共弦乐味精;公共RemoteViews rviews;
公共AppWidgetManager经理;
公共语境cntxt;
公众意向intnt;
公众诠释[] IDS;公众诠释计数= 0;公共无效的onUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){
    super.onUpdate(背景下,appWidgetManager,appWidgetIds);    了checkdate =新了checkdate();
    EVENTLIST =新EVENTLIST();    经理= appWidgetManager;
    RemoteViews意见=新的RemoteViews(context.getPackageName(),R.layout.widget_layout);
    IDS = appWidgetIds;    意向意图=新意图(背景下,DayWidget.class);
    intent.setAction(EVENT_CLICK);
    intent.putExtra(味精,变);    的PendingIntent actionPendingIntent = PendingIntent.getBroadcast(上下文,0,意图,0);    views.setOnClickPendingIntent(R.id.Event,actionPendingIntent);    CheckDate.Check();    views.setTextViewText(R.id.Today,CheckDate.Today);
    views.setTextViewText(R.id.Event,将String.valueOf(柜));    manager.updateAppWidget(IDS,意见);
}公共无效UpdateWidget(){
    捆绑额外= intnt.getExtras();
    如果(临时演员!= NULL){
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(cntxt);
        组件名AppWidget =新单元名(cntxt.getPackageName(),DayWidget.class.getName());
        INT [] = appWidgetIds appWidgetManager.getAppWidgetIds(AppWidget);        rviews =新RemoteViews(AppWidget.getPackageName(),R.layout.widget_layout);
        反++;
        rviews.setTextViewText(R.id.Event,将String.valueOf(柜));        appWidgetManager.updateAppWidget(appWidgetIds,rviews);
    }
}公共无效的onReceive(上下文的背景下,意图意图){
    intnt =意图;
    cntxt =背景;    如果(EVENT_CLICK.equals(intent.getAction())){
        尝试{
            味精= intent.getStringExtra(味精);
        }赶上(NullPointerException异常五){
            e.printStackTrace();
        }
        UpdateWidget();
        吐司面包= Toast.makeText(cntxt,onclick事件,Toast.LENGTH_LONG);
        toast.show();
    }
    super.onReceive(背景下,意图);
}


解决方案

让你的公众诠释计数= 0; 静态公共静态INT计数= 0;
或将其保存在共享preference

I am currently learning about widgets in Android. I want to increment counter value, but when I click on widget it increment it from 0 to 1 and stops to increase it. My onRecieve method work fine. Here is my code:

public static CheckDate CheckDate;
public static EventList EventList;

public static String EVENT_CLICK = "EventClick";
public String msg;

public RemoteViews rviews;
public AppWidgetManager manager;
public Context cntxt;
public Intent intnt;
public int[] ids;

public int counter = 0;

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
    super.onUpdate(context, appWidgetManager, appWidgetIds);

    CheckDate = new CheckDate();
    EventList = new EventList();

    manager = appWidgetManager;
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
    ids = appWidgetIds;

    Intent intent = new Intent(context, DayWidget.class);
    intent.setAction(EVENT_CLICK);
    intent.putExtra("msg", "change");

    PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    views.setOnClickPendingIntent(R.id.Event, actionPendingIntent);

    CheckDate.Check();

    views.setTextViewText(R.id.Today, CheckDate.Today);
    views.setTextViewText(R.id.Event, String.valueOf(counter));

    manager.updateAppWidget(ids, views);
}

public void UpdateWidget(){
    Bundle extras = intnt.getExtras();
    if(extras!=null) {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(cntxt);
        ComponentName AppWidget = new ComponentName(cntxt.getPackageName(), DayWidget.class.getName());
        int[] appWidgetIds = appWidgetManager.getAppWidgetIds(AppWidget);

        rviews = new RemoteViews(AppWidget.getPackageName(), R.layout.widget_layout);
        counter ++;
        rviews.setTextViewText(R.id.Event, String.valueOf(counter));

        appWidgetManager.updateAppWidget(appWidgetIds, rviews);
    }
}

public void onReceive(Context context, Intent intent){
    intnt = intent;
    cntxt = context;

    if(EVENT_CLICK.equals(intent.getAction())){
        try{
            msg = intent.getStringExtra("msg");
        }catch(NullPointerException e){
            e.printStackTrace();
        }
        UpdateWidget();
        Toast toast = Toast.makeText(cntxt, "OnClick", Toast.LENGTH_LONG);
        toast.show();
    }
    super.onReceive(context, intent);
}
解决方案

make your public int counter = 0; static to be public static int counter = 0;Or save it on shared preference

这篇关于窗口小部件的onReceive犯规增量INT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 16:39