2012-03-06 25 views
1

私はユーザーからのデータを受け取り、データベースに保存しています。データベースには1つの値しかありません。したがって、ユーザーがデータベースに入るたびに、データベースが更新されます。私はこのコードを使用することをAppWidget.ForのTextViewの中でこれらの件のデータを示しています。ボタンのクリックと同様に、ユーザが実行時にデータベースに新しい値を入力した後リモートビュー更新の問題

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); 

    } 

マイアプリのウィジェットは、自分自身を更新する必要がありますアプリウィジェットはスクリーンからアンインストールしてから再インストールするか、プログラムを再実行した後にのみ更新されます。なぜこの現象が起こっていますか?ボタンをクリックした後、自分のアプリウィジェットを更新するには

答えて

1

AppWidgetを手動で更新するには、Broadcastを送信し、AppWidgetProvideronReceive()メソッドで受信する必要があります。 onReceive()から、ブロードキャストIntentから必要な値をすべて抽出して、onUpdate()メソッドを手動で呼び出す必要があります。お役に立てれば。

+0

私は正しいのか分かりませんが、もし私がonReceive .....をオーバーライドしようとすると、AppWidgetProviderを拡張する受信機がエラーを表示するので、あなたはデモやサンプルプログラムのコードを使ってあなたの答えを精巧にできますより簡単に実装することができます – Prativa

+0

@prativa、ここでは、トピックに関する素晴らしいチュートリアルがあります:http://www.vogella.de/articles/AndroidWidgets/article.html#updates。ここで使用されているアプローチは、私が提案したものとは少し異なりますが、それもうまくいくはずです。 – Egor

+0

私はブロードキャストを送信し、それを私のAppWidgetProviderのonReceive()で受け取った後、onUpdate()をonReceive()から呼び出すと、その作業は素晴らしいです...しかし、このプロセスはAndroidAppWidgetクラスの2つのオブジェクトを作成しています。 – Prativa