需要帮助从小部件调用方法

发布于 2024-12-14 02:45:04 字数 3844 浏览 2 评论 0原文

我希望从小部件内的 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);
}
}

I'm looking to call a particular method by the name "AllDoneNowCloseUp" that's in a PreferenceActivity from within a widget.

Can you show me the needed coding to get this to happen?

I think the coding needs to be added to the onReceive section in my AppWidgetProvider and something to do with remote views? I also need to check if the PreferenceActivity is running in the background if possible.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野稚 2024-12-21 02:45:04

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

If AllDoneNowCloseUp is static, you can access it from YourPreferenceActivityClassName.AllDoneNowCloseUp... but I am guessing you don't want to make such a method static - what does it do?

绝情姑娘 2024-12-21 02:45:04

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

I think you will not suceed directly calling a method from a widget to an Activity. You have to use Intents and Broadcasts to communicate between them. The PreferenceActivity should be able to get that Intent in onNewIntent()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文