2013-07-02 3 views
6

내 활동에서 내 appWidgetProvide 클래스로 데이터 (문자열)를 보내는 데 문제가 있습니다.내 (홈 화면) 위젯에 데이터 보내기

저는 updateWidget이라는 메서드가 있습니다. 이것은 위젯이 처음 홈 화면에 놓일 때 위젯 구성 활동에 의해 호출됩니다.이 메소드는 사용자가 해당 활동에 데이터를 입력 할 때 내 활동 중 하나에서 데이터를 수신 할 때 onReceive 메소드에 의해 호출됩니다.

내 문제는 다음과 같습니다. 위젯은 모든 데이터가 올바른 위치에있는 홈 화면에 배치됩니다. 그러나 내 활동이 데이터를 보내면 onReceive (의도 및 추가 기능 사용) 데이터가 전달되지 않습니다. 단지 extras.getString에서 빈 문자열을 얻습니다.

이전에 활동간에 데이터를 보내기 위해 의도를 사용했는데, 데이터를 위젯 제공 클래스로 보낼 때 다른 작업을해야합니까? 아니면 바보가되어 뭔가 분명하지 않은가?

나는 (내가 생각하기에) 관련 코드 비트를 첨부했다. 설명이나 코드가 필요하면 알려주십시오. 이를 읽어 주셔서 당신이 줄 수있는 모든 도움을

감사합니다,

건배 Rakshak

위젯 구성 클래스의 onListItemClick.

@Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 

     Cursor note = mDbHelper.fetchNote(id); 
     startManagingCursor(note); 
     String title = note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)); 
     String text = note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)); 
     loadData(title); 


     Intent resultValue = new Intent(); 
     resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);    
     setResult(RESULT_OK,resultValue); 
     finish(); 
} 



void loadData(String title) { 

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this); 

    SingleNote.updateWidget(this, appWidgetManager, mAppWidgetId, title); 

} 

private void updateWidget() { 
    Intent i = new Intent(this, SingleNote.class); 
    i.setAction(SingleNote.UPDATE_ACTION); 
    Toast.makeText(getApplicationContext(),mTitleText.getText()+"from the activity", 
      Toast.LENGTH_SHORT).show();//This works just fine 
    i.putExtra("title", mTitleText.getText()); 
    sendBroadcast(i); 

}

내 위젯은 내가 제안 클래스를

public class SingleNote extends AppWidgetProvider { 

public static String UPDATE_ACTION = "ActionUpdateSinglenoteWidget"; 
private static NotesDbAdapter mDbHelper; 



public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
    final int N = appWidgetIds.length; 

    // Perform this loop procedure for each App Widget that belongs to this provider 
    for (int i=0; i<N; i++) { 
     int appWidgetId = appWidgetIds[i]; 



    // Tell the AppWidgetManager to perform an update on the current app widget 
     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.singlenote_widget); 
     appWidgetManager.updateAppWidget(appWidgetId, views); 




    } 
} 

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

     String action = intent.getAction(); 
     Bundle extras = intent.getExtras(); 
     String title1 = extras.getString("title");//this value does not come through 
     Toast.makeText(context, title1,Toast.LENGTH_LONG).show();//this gives an empty space 

     if (action != null && action.equals(UPDATE_ACTION)) { 
       final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 
       ComponentName name = new ComponentName(context, SingleNote.class); 
       int[] appWidgetId = AppWidgetManager.getInstance(context).getAppWidgetIds(name); 
       final int N = appWidgetId.length; 
       if (N < 1) 
       { 
        return ; 
       } 
       else { 
        int id = appWidgetId[N-1]; 
        updateWidget(context, appWidgetManager, id ,title1); 
       } 
     } 

     else { 
       super.onReceive(context, intent); 
     } 
} 



static void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, String title){ 

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.singlenote_widget); 
    views.setTextViewText(R.id.single_note_title, title); 
    appWidgetManager.updateAppWidget(appWidgetId, views); 



} 

답변

0

을 제공 onReceive (이것은 내 활동 클래스 중 하나 인)에 데이터를 전송 목적 i.putExtra("title", mTitleText.getText());updateWidget()으로 바꾸려면으로 시도하십시오. 0

String title1 = extras.getString("title"); 

문자열 및 mTitleText.getText() 반환을 Editable 기대 -이 가능성이 잘 작동 불일치

+0

덕분에 남자입니다. – DrkStr