2012-03-06 2 views
1

사용자로부터 데이터를 가져 와서 데이터베이스에 저장하고 있습니다. 데이터베이스에는 단 하나의 값만 있습니다. 따라서 사용자가 데이터베이스에 들어갈 때마다 데이터베이스가 업데이트됩니다.원격보기 업데이트 문제

public void onUpdate(Context context, android.appwidget.AppWidgetManager appWidgetManager, int[] appWidgetIds) { 

    //Get all ids 
    ComponentName thisWidget = new ComponentName(context.getPackageName(),SmsSchedulerWidget.class.getName()); 

    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget); 


    //the datas to be shown are fetched from the database 
    DatabaseManager dbManager = new DatabaseManager(context); 
    dbManager.open(); 
    contactNumber = dbManager.fetchContactNumber(); 
    Log.i("contactNumber",contactNumber); 
    date = dbManager.fetchDate(); 
    Log.i("date",date); 
    message = dbManager.fetchMessage(); 
    Log.i("message",message); 
    status = dbManager.fetchStatus(); 
    Log.i("status", status); 
    dbManager.closeDatabase(); 
    //Build the intent to call the service 

    //it creates the UI for the given app widget 
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.schdulesms_appwidget_layout); 


    remoteViews.setTextViewText(R.id.to_appwidget_saved_data, 
      contactNumber); 
    remoteViews.setTextViewText(R.id.date_appwidget_saved_data, date); 
    remoteViews.setTextViewText(R.id.status_appwidget_saved, status); 
    remoteViews.setTextViewText(R.id.message_appwidgset_saved_data, 
      message); 

    //to start the activity with the click on the layout 
    Intent clickIntent = new Intent(context.getApplicationContext(),MainActivity.class); 
    clickIntent.setFlags(clickIntent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent pendingIntent= PendingIntent.getActivity(context, 0, clickIntent, 0); 
    remoteViews.setOnClickPendingIntent(R.id.full_widget, pendingIntent); 

    //to update the BroadCast Receiver so that it holds the current values in the layout 
    Intent updateIntent =new Intent(context,SmsSchedulerWidget.class); 

    updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); 
    updateIntent.setComponent(thisWidget); 

    PendingIntent pendingIntentForUpdate = PendingIntent.getBroadcast(context, 0, updateIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    remoteViews.setOnClickPendingIntent(R.id.logoBtn, pendingIntentForUpdate); 

    appWidgetManager.updateAppWidget(thisWidget, remoteViews); 

    } 

내 애플 위젯이 실행에 데이터베이스에뿐만 아니라 사용자가 새 값을 입력 한 것처럼 버튼을 클릭 한 후 자체를 업데이트해야합니다 :이 코드를 사용하는 것이 나는 AppWidget.For의 텍스트 뷰에서 이러한 datas를 보여 app widget이 화면에서 제거되고 다시 설치되거나 프로그램이 다시 실행 된 후에 만 ​​앱 위젯이 업데이트됩니다. 왜 이런 일이 발생합니까? 버튼을 클릭 한 후 내 앱 위젯을 업데이트하는 방법 ?????

답변

1

AppWidget을 직접 업데이트하려면 Broadcast으로 보내야하며 AppWidgetProvideronReceive() 방법으로 수신해야합니다. onReceive()에서 브로드 캐스트 Intent에서 필요한 모든 값을 추출한 다음 onUpdate() 메서드를 수동으로 호출해야합니다. 희망이 도움이됩니다.

+0

글쎄, 만약 내가 맞다면 잘 모르겠지만, 수신자가 AppWidgetProvider를 확장하면 overReceive .....를 오버라이드하려고 할 때 오류가 발생한다. 그러면 데모 나 샘플 프로그램 코드를 통해 대답을 자세히 설명 할 수있다. 좀 더 쉽게 구현할 수 있습니다. – Prativa

+0

@prativa, 다음은 주제에 대한 훌륭한 자습서입니다 : http://www.vogella.de/articles/AndroidWidgets/article.html#updates. 여기에 사용 된 접근법은 내가 제안한 것과 조금 다르지만 작동해야합니다. – Egor

+0

잘 브로드 캐스트를 보내고 내 AppWidgetProvider의 onReceive()에서받은 다음 onUpdate()를 onReceive()에서 호출하고 그 작업을 환상적으로 ...하지만이 프로세스는 AndroidAppWidget 클래스의 두 객체를 만듭니다. – Prativa