如何在Android应用小部件中找到被点击的按钮?

3

我想设计一个简单的应用程序小部件,其中包含两个文本视图和两个按钮,用于上一个/下一个。 我在处理应用程序小部件中的按钮单击时遇到了困难。 实际上,我的愿望是,如果用户点击上一个按钮,我希望显示前一个值,如果用户点击下一个按钮,我希望从数据库中显示下一个值。 如何知道哪个按钮被点击了?

在这里,我像这样注册按钮单击侦听器:

  public static class UpdateWidgetService extends IntentService {

    public UpdateWidgetService() {
        super("UpdateWidgetService");

    }

    @Override
    protected void onHandleIntent(Intent intent) {
        AppWidgetManager appWidgetManager = AppWidgetManager
                .getInstance(this);

        int incomingAppWidgetId = intent.getIntExtra(EXTRA_APPWIDGET_ID,
                INVALID_APPWIDGET_ID);
        if (incomingAppWidgetId != INVALID_APPWIDGET_ID) {
            updateOneAppWidget(appWidgetManager, incomingAppWidgetId);

        }

    }


    private void updateOneAppWidget(AppWidgetManager appWidgetManager,
            int appWidgetId) {
        DatabaseManager dbManager = new DatabaseManager(this);

        dbManager.open();
        String contactNumber, date, status, message;
        ArrayList<QueuedMessage> listOfQuedMessage =        (ArrayList<QueuedMessage>) dbManager
                .fetchContactNumber();
        if (listOfQuedMessage.size() == 0)
            Log.i("Db", "null");
        else{
        date = dbManager.fetchDate();
        message = dbManager.fetchMessage();
        status = dbManager.fetchStatus();

        dbManager.closeDatabase();

        RemoteViews views = new RemoteViews(this.getPackageName(),
                R.layout.schdulesms_appwidget_layout);
        views.setTextViewText(R.id.to_appwidget_saved_data,
                listOfQuedMessage.get(count).contacNumber);
        views.setTextViewText(R.id.date_appwidget_saved_data, date);
        views.setTextViewText(R.id.status_appwidget_saved, status);
        views.setTextViewText(R.id.message_appwidgset_saved_data, message);

                    **//here i want do**
                   if(button1){
               btnNxtClick(views, appWidgetId,listOfQuedMessage.size());
                    }else{
                 btPrevClick(views, appWidgetId, listOfQuedMessage.size());
                    }

        appWidgetManager.updateAppWidget(appWidgetId, views);
        }

    }

    private void btnNxtClick(RemoteViews views, int appWidgetId,int sizeOfList) {
        Intent btnNextIntent = new Intent(this, this.getClass());
        btnNextIntent.putExtra(EXTRA_APPWIDGET_ID, appWidgetId);
        PendingIntent btnNextPendingIntent = PendingIntent.getService(this,
                0, btnNextIntent,    PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.btnNext, btnNextPendingIntent);

    }
    private void btPrevClick(RemoteViews views, int appWidgetId,int sizeOfList) {
        Intent btnNextIntent = new Intent(this, this.getClass());
        btnNextIntent.putExtra(EXTRA_APPWIDGET_ID, appWidgetId);
        PendingIntent btnNextPendingIntent = PendingIntent.getService(this,
                0, btnNextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.btnPrev, btnNextPendingIntent);

    }

}

可以有人帮我解决这个问题吗?谢谢。
1个回答

9

如果想要知道哪个按钮被点击了,按照以下步骤进行:

步骤1: 为btnNext和btnPrev注册两个接收器

 <intent-filter >
 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
 <action android:name="com.app.example.MyWidget.ACTION_WIDGET_CLICK_NEXT"/>
 <action android:name="com.app.example.MyWidget.ACTION_WIDGET_CLICK_PREV"/>
 </intent-filter>

步骤 2: 对于 btnNext 和 btnPrev 动作字符串:

public class MyWidget extends AppWidgetProvider {
public static String ACTION_WIDGET_CLICK_NEXT = "Action_nextbtn";
public static String ACTION_WIDGET_CLICK_PREV = "Action_prevbtn";
}

步骤三:在onReceive中

@Override
        public void onReceive(Context paramContext, Intent paramIntent)
        {
          String str = paramIntent.getAction();
            if (paramIntent.getAction().equals(ACTION_WIDGET_CLICK_NEXT)) {
                updateWidgetState(paramContext, str);   
            }
            if (paramIntent.getAction().equals(ACTION_WIDGET_CLICK_PREV)) {
                updateWidgetState(paramContext, str);   
            }
        }

步骤 4:创建一个更新小部件状态的方法:

static void updateWidgetState(Context paramContext, String paramString)
          {
            RemoteViews localRemoteViews = buildUpdate(paramContext, paramString);
            ComponentName localComponentName = new ComponentName(paramContext, MyWidget.class);
            AppWidgetManager.getInstance(paramContext).updateAppWidget(localComponentName, localRemoteViews);
          }

第五步:

private static RemoteViews buildUpdate(Context paramContext, String paramString)
          {

            rview = new RemoteViews(paramContext.getPackageName(), R.layout.widget_layout);
            Intent activebtnnext = new Intent(paramContext, MyWidget.class);
            active.setAction(ACTION_WIDGET_CLICK_NEXT);
            PendingIntent configPendingIntentnext = PendingIntent.getBroadcast(paramContext, 0, activebtnnext , 0);
            rmViews.setOnClickPendingIntent(R.id.btnNext, configPendingIntentnext);

            Intent activeprevbtn = new Intent(paramContext, MyWidget.class);
            active.setAction(ACTION_WIDGET_CLICK_PREV);
            PendingIntent configPendingIntentprev = PendingIntent.getBroadcast(paramContext, 0, activeprevbtn , 0);
            rmViews.setOnClickPendingIntent(R.id.btnprev, configPendingIntentprev);
            if(parmString.equals(ACTION_WIDGET_CLICK_NEXT))
            {

             //

            }  
            if(parmString.equals(ACTION_WIDGET_CLICK_PREV))
            {

             //

            }           
             return rview; 
          }

哇,它起作用了。我按照自己的方式实现了它,但逻辑是你的,功劳归于你,为你加一分。再次非常感谢@imran :) - Krishna Shrestha
IntentService没有暴露onReceive()方法。 - IgorGanapolsky
我正在使用AppWidgetProvider,所以我只需在onReceive函数中添加super.onReceive(context,intent);,它就可以工作了。谢谢。 - Mohammad Tabbara

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接