我希望通过名称“AllDoneNowCloseUp”调用特定方法,该名称位于小部件内的PreferenceActivity中。
你能告诉我所需的编码吗?
我认为需要将编码添加到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);
}
}
答案 0 :(得分:1)
如果AllDoneNowCloseUp是静态的,你可以从YourPreferenceActivityClassName.AllDoneNowCloseUp访问它...但我猜你不想让这样的方法静态 - 它做了什么?
答案 1 :(得分:0)
我认为你不会直接调用从小部件到Activity的方法。您必须使用Intents和Broadcasts在它们之间进行通信。 PreferenceActivity应该能够在onNewIntent()
中获取Intent