1

我正在尝试如何从 Android 小部件上的按钮运行 Activity?为了在单击图像按钮时启动我的应用程序。当我取消注释代码时,它会切换图像,但是当我尝试使用此代码时,它什么也不做。我想开始我的申请。

public class MyWidgetIntentReceiver extends BroadcastReceiver {

    private static int clickCount = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("pl.looksok.intent.action.CHANGE_PICTURE")){
            updateWidgetPictureAndButtonListener(context);
        }
    }

    private void updateWidgetPictureAndButtonListener(Context context) {
        Log.i("PROJECTCARUSO", "updateWidgetPictureAndButtonListener");
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
//      remoteViews.setImageViewResource(R.id.widget_button, getImageToSet());
//
//      //REMEMBER TO ALWAYS REFRESH YOUR BUTTON CLICK LISTENERS!!!
//      remoteViews.setOnClickPendingIntent(R.id.widget_button, MyWidgetProvider.buildButtonPendingIntent(context));
//
//      MyWidgetProvider.pushWidgetUpdate(context.getApplicationContext(), remoteViews);

        Intent intentClick = new Intent(context, FragmentChange.class);
        PendingIntent pendingIntent = PendingIntent.getActivity (context, 0,
        intentClick, 0);
        remoteViews.setOnClickPendingIntent (R.id.widget_button, pendingIntent);
    }

    private int getImageToSet() {
        clickCount++;
        return clickCount % 2 == 0 ? R.drawable.app_icon : R.drawable.energy;
    }
}
4

1 回答 1

0

这是我终于开始工作的地方:

public class WidgetProvider extends AppWidgetProvider {

    public static String SWITCH_WIDGET_UPDATE = "MainActivity.Switch";


    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        Log.i("PROJECTCARUSO","onUpdate");

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
        remoteViews.setOnClickPendingIntent(R.id.widget_button, buildButtonPendingIntent(context));

        Intent intentClick = new Intent(context, FragmentChange.class);
        PendingIntent pendingIntent = PendingIntent.getActivity (context, 0, intentClick, 0);
        remoteViews.setOnClickPendingIntent (R.id.widget_button, pendingIntent);

        pushWidgetUpdate(context, remoteViews);

    }


    public static PendingIntent buildButtonPendingIntent(Context context) {
        Intent intent = new Intent();
        intent.setAction("CHANGE_PICTURE");
        return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }


    public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
        ComponentName myWidget = new ComponentName(context, WidgetProvider.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        manager.updateAppWidget(myWidget, remoteViews);     

    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        super.onReceive(context, intent);
    }
}
于 2013-07-24T18:54:29.047 回答