单击小部件启动我的应用程序

时间:2013-07-18 21:55:23

标签: android broadcastreceiver

我正在尝试How do I run an Activity from a button on a widget in Android?,以便点击图片按钮启动我的应用程序。当我取消注释代码时,它会切换图像,但是当我尝试使用此代码时,它什么都不做。我想开始申请。

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;
    }
}

1 个答案:

答案 0 :(得分: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);
    }
}