2011-11-04 3 views
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); 
} 
} 

답변

1

AllDoneNowCloseUp이 정적 인 경우 YourPreferenceActivityClassName.AllDoneNowCloseUp에서 액세스 할 수 있지만 정적 메서드를 사용하지 않으려 고합니다. 그렇지?

+0

사용자는 설정 변경 사항을보고 싶을 때 PreferenceActivity 화면을 닫습니다. –

0

위젯에서 Activity로 직접 메서드를 호출하는 것이 좋습니다. 인 텐트와 브로드 캐스트를 사용하여 이들 사이에서 통신해야합니다. PreferenceActivity는 onNewIntent()에서 해당 인 텐트를 가져올 수 있어야합니다.

+0

답장을 보내 주셔서 감사합니다. 나는 이것에 처음이에요. 코드를 구현하는 데 필요한 코딩을 보여줄 수 있습니까? –