【问题标题】:Need help calling a method from a widget需要帮助从小部件调用方法
【发布时间】:2011-12-21 23:29:56
【问题描述】:

我希望通过小部件内的 PreferenceActivity 中的名称“AllDoneNowCloseUp”调用特定方法。

你能告诉我实现这一点所需的编码吗?

我认为编码需要添加到我的 AppWidgetProvider 中的 onReceive 部分,并且与远程视图有关?如果可能,我还需要检查 PreferenceActivity 是否在后台运行。

public class ButtonWidget extends AppWidgetProvider {

public static String ON_OFF_BUTTON_CHOSEN = "Chime On/Off button was chosen";

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

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

    Intent active = new Intent(context, ButtonWidget.class);
    active.setAction(ON_OFF_BUTTON_CHOSEN);

    PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context,
            0, active, 0);

    /*
     * Activate click event handler for the button.
     */
    remoteViews.setOnClickPendingIntent(R.id.button_on_off,
            actionPendingIntent);

    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}

@Override
public void onReceive(Context context, Intent intent) {

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

    // check, if our Action was called
    if (intent.getAction().equals(ON_OFF_BUTTON_CHOSEN)) {

        String strNotifyMessage = null;

        /*
         * Get all the settings from the settings xml file.
         */
        SharedPreferences clockSettings = PreferenceManager
                .getDefaultSharedPreferences(context);

        /*
         * Find out what the current state of the on/off mode is.
         */
        boolean booleanMasterChimeToggle = clockSettings
                .getBoolean("MasterChimeToggle", false);

        /*
         * Save the new state in the preferences.
         */
        SharedPreferences.Editor prefEditor = clockSettings
                .edit(); // Allow the settings to be changed.

        if (booleanMasterChimeToggle == true) {

            strNotifyMessage = "Chiming has now been DISABLED.";

            prefEditor.putBoolean("MasterChimeToggle", false);
            prefEditor.putBoolean("ChimeOnTheHour", false);
            prefEditor.putBoolean("ChimeOn15Past", false);
            prefEditor.putBoolean("ChimeOn30Past", false);
            prefEditor.putBoolean("ChimeOn45Past", false);

            remoteViews.setTextViewText(R.id.button_on_off, "Turn On"); 

        } else {

            strNotifyMessage = "Chiming has now been ENABLED.";

            prefEditor.putBoolean("MasterChimeToggle", true);
            prefEditor.putBoolean("ChimeOnTheHour", true);
            prefEditor.putBoolean("ChimeOn15Past", true);
            prefEditor.putBoolean("ChimeOn30Past", true);
            prefEditor.putBoolean("ChimeOn45Past", true);

            remoteViews.setTextViewText(R.id.button_on_off, "Turn Off"); 
        }

//          Toast.makeText(context, strNotifyMessage, Toast.LENGTH_LONG).show();

        prefEditor.commit(); // Save changes.

        /*
         * Display a message in the status bar showing the new chime on/off
         * state.
         */
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                intent, 0);
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification noty = new Notification(R.drawable.icon,
                strNotifyMessage, System.currentTimeMillis());

        /*
         * This will show up when the user pull down the notice bar.
         */
        noty.setLatestEventInfo(context, "Notice:", strNotifyMessage,
                contentIntent);
        notificationManager.notify(1, noty);

    } else {
        // do nothing
    }

    super.onReceive(context, intent);
}
}

【问题讨论】:

    标签: android methods android-widget call remoteview


    【解决方案1】:

    如果 AllDoneNowCloseUp 是静态的,您可以从 YourPreferenceActivityClassName.AllDoneNowCloseUp... 访问它...但我猜您不想将这样的方法设为静态 - 它有什么作用?

    【讨论】:

    • 它只是关闭 PreferenceActivity 屏幕以防用户想要查看设置的更改。
    【解决方案2】:

    我认为您不会成功地直接从小部件调用方法到 Activity。您必须使用 Intents 和 Broadcasts 在它们之间进行通信。 PreferenceActivity 应该能够在 onNewIntent() 中获取该 Intent

    【讨论】:

    • 感谢您的回复。我是新手。你能展示实现它所需的编码吗?
    猜你喜欢
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多