2011-07-22 62 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

我只是把你的源代碼看,它似乎問題都屬於邏輯, 你重新創建你的改變文字內容後新的遠程視窗對象:

解決方案:註釋掉下面的代碼行:

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

在課堂上爲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"); 
     } 

     /* 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中的行更改爲remoteView.etc而非remoteViews.etc仍然無法正常工作。 – leonsas

0

儘量把你的代碼從onStartCommand變成

public void onStart(Intent intent, int startId) 

因爲您可能會在僅使用onStart的舊版Android平臺上運行您的代碼。