将活动中的数据更新到窗口小部件是不行的

时间:2018-01-24 13:51:55

标签: android android-activity

我处于这种情况,我有一个活动和一个小部件连接到它,我把所有必要的代码用于确认,发送和更新我的活动中的数据到小部件,或者我认为因为数据不是&#39 ; t更新。我想念一些东西,但我不知道是什么。

除了更新部分之外,我的所有代码都正常工作。当按下活动中的确认button时,小部件没有任何反应。

这是我的活动代码:

public class MainActivity extends AppCompatActivity {

NumberPicker dayPicker, monthPicker, yearPicker;
Button confirmButton;
int day,month,year;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dayPicker = findViewById(R.id.dayPicker);
    monthPicker = findViewById(R.id.monthPicker);
    yearPicker = findViewById(R.id.yearPicker);
    dayPicker.setMinValue(1);
    dayPicker.setMaxValue(31);
    monthPicker.setMinValue(1);
    monthPicker.setMaxValue(12);
    yearPicker.setMinValue(1950);
    yearPicker.setMaxValue(Calendar.getInstance().get(Calendar.YEAR));
    confirmButton = findViewById(R.id.confirmationButton);
    confirmButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            day = dayPicker.getValue();
            month = monthPicker.getValue();
            year = yearPicker.getValue();
            Intent intent = new Intent(MainActivity.this, 
BirthdayWidget.class);
            intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
            intent.putExtra("day",day);
            intent.putExtra("month",month);
            intent.putExtra("year",year);
            sendBroadcast(intent);
        }
    });
  }
}

这是我的小部件代码:

 public class BirthdayWidget extends AppWidgetProvider {

int day,month,year;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        Date d1 = calendar.getTime();
        Date d2 = new Date( Calendar.getInstance().get(Calendar.YEAR) - year + 90,
                month-1,day);
        long diff = d2.getTime() - d1.getTime();
        long dayLong = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
        String dayString = Long.toString(dayLong);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.birthday_widget);
        remoteViews.setTextViewText(R.id.appwidget_text,dayString+" days until birthday!");

        int id = appWidgetIds[0];
        appWidgetManager.updateAppWidget(id, remoteViews);
}

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    day = intent.getIntExtra("day",1);
    month = intent.getIntExtra("month",1);
    year = intent.getIntExtra("year",2000);
}

1 个答案:

答案 0 :(得分:0)

您是否将接收器标签放入舱单?像这样。

Hook.AppEvents().OnCombination(new Dictionary<Combination, Action>
{
    {Combination.FromString("Left"), GoWest},
    {Combination.FromString("Left+Up"), GoNorthWest},
    {Combination.FromString("Up+Left"), GoNorthWest},
    {Combination.FromString("Up"), GoNorth},
    {Combination.FromString("Up+Right"), GoNorthEast},
    {Combination.FromString("Right+Up"), GoNorthEast},
});

将其放在应用程序标记