Widget double click

2020-07-23 04:17发布

I have a widget (AppWidgetProvider) and i want to know if there is a way to support multiple clicks.Example:

1)If is the first click on the widget, then the ImageButton of the widget changes (for example, changes the color).

2)If is the second time, then open an Activity.

-- There is some way to handle click events inside AppWidgetProvider?

My code:

public class MyWidgetProvider extends AppWidgetProvider
{


    @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];    

            Intent intent = new Intent(context, MyActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);


            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.control_widget);

            views.setOnClickPendingIntent(R.id.asdf, pendingIntent);

             appWidgetManager.updateAppWidget(appWidgetId, views);
        }    
    }
}

My widget is working fine. When i click the ImageButton(R.id.asdf), it goes to the activity MyActivity.

Id like to know how can i handle click events on my widget to make a different action (example: change the color of the ImageButton) instead of go to some activity. Is there some other way to some click handle besides setOnClickPendingIntent()?

标签: android
7条回答
趁早两清
2楼-- · 2020-07-23 05:09

The solution by almisoft provides a double-click feel rather than simply successive clicks. And it works. Unfortunately you also get a lint message telling you that the 'handler class should be static or leaks might occur'. The solution is to use a weak reference and static handler - a generic version is here. Converting almisoft's code gives:

public class WidgetProvider extends AppWidgetProvider {

private static final int DOUBLE_CLICK_DELAY = 500;
private static Context mContext;
private static int mClickCount;

private static class MyHandler extends Handler {
  //Using a weak reference means you won't prevent garbage collection
  private final WeakReference<WidgetProvider> myClassWeakReference; 
  public MyHandler(WidgetProvider myClassInstance) {
    myClassWeakReference = new WeakReference<WidgetProvider>(myClassInstance);
  }
  @Override
  public void handleMessage(Message msg) {
    WidgetProvider myWidget = myClassWeakReference.get();
    if (myWidget != null) {
        //...do work here as in almisoft original...
        int clickCount = mContext.getSharedPreferences("widget", Context.MODE_PRIVATE).getInt("clicks", 0);
        if (clickCount > 1) {
          Toast.makeText(mContext, "doubleClick", Toast.LENGTH_SHORT).show();
        } else {
          Toast.makeText(mContext, "singleClick", Toast.LENGTH_SHORT).show();
        }
        mContext.getSharedPreferences("widget", Context.MODE_PRIVATE).edit().putInt("clicks", 0).commit();
    }
  }
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
    Intent intent = new Intent(context, getClass());
    intent.setAction("Click");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    views.setOnClickPendingIntent(R.id.image, pendingIntent);
    appWidgetManager.updateAppWidget(appWidgetIds, views);
    context.getSharedPreferences("widget", 0).edit().putInt("clicks", 0).commit();
}

@Override
public void onReceive(final Context context, Intent intent) {
    mContext=context;
    mIntent=intent;
    if (intent.getAction().equals("Click")) {
        int clickCount = context.getSharedPreferences("widget", Context.MODE_PRIVATE).getInt("clicks", 0);
        context.getSharedPreferences("widget", Context.MODE_PRIVATE).edit().putInt("clicks", ++clickCount).commit();
        final Handler handler=new MyHandler(this);
        if (clickCount == 1) new Thread() {
            @Override
            public void run(){
                try {
                    synchronized(this) { wait(DOUBLE_CLICK_DELAY); }
                    handler.sendEmptyMessage(0);
                } catch(InterruptedException ex) {}
            }
        }.start();
      }
    super.onReceive(context, intent);
    }

}
查看更多
登录 后发表回答