如何更改Android小部件背景和textcolor?

时间:2013-01-02 13:15:55

标签: android android-widget

我需要以这种方式更改小部件背景。  1-点击小部件后,我们会自定义widgetview,然后单击确定按钮小部件。 屏幕。

喜欢这个小部件: -                      http://youtu.be/k9SFD8yuttw

我在谷歌上搜索了很多但没有得到任何线索。我有很多尝试: - 但它不起作用: -

private static final String ACTION_CLICK = "ACTION_CLICK";

private static Intent getLaunchIntent(final Context context,
        final int appWidgetId) {
    final PackageManager manager = context.getPackageManager();
    final Intent intent = manager
            .getLaunchIntentForPackage("com.appstudioz.hellowidget");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.setData(new Uri.Builder().path(String.valueOf(appWidgetId))
            .build());
    return intent;

}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    /*Timer timer = new Timer();
    timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1,
            1000);

    ComponentName objcomponentname = new ComponentName(context,
            HelloWidget.class);
    int[] allappids = appWidgetManager.getAppWidgetIds(objcomponentname);*/



    final RemoteViews remoteViews = new RemoteViews(
            context.getPackageName(), R.layout.widgetlayout);

    final PreferenceAdaptor preferences = new PreferenceAdaptor(context);

    for (final int appWidgetId : appWidgetIds) {
        updateAppWidget(context, appWidgetManager, appWidgetId,
                preferences, remoteViews);
    }
    if (preferences.isUpdateNeeded()) {
        // Build the intent to call the service
        Intent intent = new Intent(context, UpdateService.class);
        context.startService(intent);
    }

}

private class MyTime extends TimerTask {
    RemoteViews remoteViews;
    AppWidgetManager appWidgetManager;
    ComponentName thisWidget;
    DateFormat format = SimpleDateFormat.getTimeInstance(
            SimpleDateFormat.MEDIUM, Locale.getDefault());

    public MyTime(Context context, AppWidgetManager appWidgetManager) {
        this.appWidgetManager = appWidgetManager;
        remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.activity_main);
        thisWidget = new ComponentName(context, HelloWidget.class);
    }

    @Override
    public void run() {
        remoteViews.setTextViewText(R.id.widget_textview, "Time = "
                + format.format(new Date()));
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);
    }
}

@Override
public void onReceive(Context context, Intent intent) {
    // v1.5 fix that doesn't call onDelete Action
    final String action = intent.getAction();
    if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
        final int appWidgetId = intent.getExtras().getInt(
                AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
            this.onDeleted(context, new int[] { appWidgetId });
        }
    } else {
        super.onReceive(context, intent);
    }
}

public static void updateAppWidget(final Context context,
        final AppWidgetManager appWidgetManager, final int appWidgetId,
        final PreferenceAdaptor preferences, final RemoteViews remoteViews) {

    if (preferences.isUpdateNeeded()) {
        return;
    }


    final PendingIntent pendingIntent = PendingIntent.getActivity(context,
            appWidgetId, getLaunchIntent(context, appWidgetId), 0);

    remoteViews.setOnClickPendingIntent(R.id.thelayout, pendingIntent);
    appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}

任何人建议我或给我链接如何自定义我的小部件。

0 个答案:

没有答案