2011-07-22 7 views
1

こんにちは私は自分のAppwidgetにボタンを追加しようとしていますが、多くのコードを読んだ後でも、私のボタンは機能しません。AppWidgetのボタン

これは私のAppWidgetProviderです: パブリッククラスはMyWidgetはAppWidgetProvider {

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

      Intent intent = new Intent(context, MyService.class); 
      intent.setAction("0"); 
      intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); 
      PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0); 


      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widgetlayout); 
      views.setOnClickPendingIntent(R.id.button_widget, pendingIntent); 


      appWidgetManager.updateAppWidget(appWidgetIds, views); 
     } 
    } 

そして、ここでは、MyServiceである拡張:

public class MyService extends Service { 


    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     String command = intent.getAction(); 
     int[] appWidgetIds = intent 
     .getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS); 
     RemoteViews remoteView = new RemoteViews(getApplicationContext() 
       .getPackageName(), R.layout.widgetlayout); 
     AppWidgetManager appWidgetManager = AppWidgetManager 
       .getInstance(getApplicationContext()); 


     if (command.equals("0")) { 
      remoteViews.setTextViewText(R.id.button_widget, "TextChanged"); 
     } 

     RemoteViews remoteViews = new RemoteViews(getPackageName(), 
       R.layout.widgetlayout); 

     appWidgetManager.updateAppWidget(appWidgetIds, remoteView); 


     super.onStart(intent, startId); 
     return 0; 
    } 




    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 

} 

これは時にボタンのテキストを変更することになっているだけのダミーコードですあなたはそれをクリックします。それはしません。

私が間違ってやっているかについての任意のアイデア?おかげさまで 私はちょうどあなたのソースコードで見てください

答えて

0

、それは問題のように見えるあなたの変更のテキストコンテンツの後にロジック、 あなたの再作成し、新しいRemoteViewオブジェクトを所属:

ソリューション:アウトコメント次のコード行:

クラスMyServiceでは
/* 22/07/2011 DELETE START */ 
    //RemoteViews remoteViews = new RemoteViews(getPackageName(), 
    //  R.layout.widgetlayout); 
    /* 22/07/2011 DELETE END */ 

public class MyService extends Service { 


    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     String command = intent.getAction(); 
     int[] appWidgetIds = intent 
     .getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS); 
     RemoteViews remoteView = new RemoteViews(getApplicationContext() 
       .getPackageName(), R.layout.widgetlayout); 
     AppWidgetManager appWidgetManager = AppWidgetManager 
       .getInstance(getApplicationContext()); 


     if (command.equals("0")) { 
      remoteViews.setTextViewText(R.id.button_widget, "TextChanged"); 
     } 

     /* 22/07/2011 DELETE START */ 
     //RemoteViews remoteViews = new RemoteViews(getPackageName(), 
     //  R.layout.widgetlayout); 
     /* 22/07/2011 DELETE END */  
     appWidgetManager.updateAppWidget(appWidgetIds, remoteView); 


     super.onStart(intent, startId); 
     return 0; 
    } 




    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 

} 
+0

これを削除し、ifの行をremoteViews.etcではなくremoteView.etcに変更しましたが、まだ動作していません。 – leonsas

0

はあなたのコードを入れて試してみてくださいonStartCommandから

public void onStart(Intent intent, int startId) 

にあなただけのONSTART使用する古いのAndroidプラットフォーム上でコードを実行する可能性があるため。

関連する問題