2011-11-04 25 views
0

我正在調用一個名爲「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); 
} 
} 

回答

1

如果AllDoneNowCloseUp是靜態的,您可以從YourPreferenceActivityClassName.AllDoneNowCloseUp訪問它......但我猜你不想讓這樣一個方法是靜態的 - 什麼它做什麼?

+0

它只是關閉PreferenceActivity屏幕,以防用戶想要查看對設置的更改。 –

0

我想你不會直接調用一個方法從一個小部件到一個Activity。您必須使用Intents和廣播來在它們之間進行通信。 PreferenceActivity應該能夠在onNewIntent()中獲得該意圖

+0

感謝您的回覆。我對此很陌生。你能展示實現它所需的編碼嗎? –