1

アンドロイドでウィジェットを作成しました。いつでもWidget TextViewをクリックすると1つのサービスが開始されますが、同時に私は右側に表示されているアイコンを変更します。ウィジェットでテキストビューをクリックするとアイコンが変わります

私は

remoteViews.setOnClickPendingIntent(R.id.widgetTxt,pendingIntent); 

を使用してWidget TextView上のonclickを設定することができますが、TextViewにはクリック

画像 enter image description here

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

         // Here i am trying to set a onclick over TextView 
    Intent intent = new Intent(context,MyService.class); 
      PendingIntent pendingIntent = PendingIntent.getService(context,0 , 
        intent, 0); 

      remoteViews.setOnClickPendingIntent(R.id.MyWidgetTxt,pendingIntent); 

    } 

を得るとき、私はアイコンを変更することができ、私は知りませんアイコン自体がクリックされると、それも変化し、onReceiveでそのコードを設定しました

+0

:AppWidgetProvider.java

のmanifest.xmlで

<receiver android:name="TestwidgetProvider" android:label="@string/widget_title" android:exported="false" android:icon="@drawable/test"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> <action android:name="com.imranandroid.demo.ACTION_ON_ImG_CLICK"/> <action android:name="android.appwidget.action.APPWIDGET_DELETED"/> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info" /> </receiver> 

を.android.com/guide/topics/appwidgets/index.html)、[onReceive()](http://developer.android.com/reference/android/appwidget/AppWidgetProvider.html#onReceive(android.conten) t.Context、%20andandroid.content.Intent))メソッドを呼び出します。 –

答えて

3

この試してください://開発者:[アプリケーションのウィジェット](HTTPのためのガイドがあります

public class TestwidgetProvider extends AppWidgetProvider { 
     public static final String TAG_IMAGE_CLICK = "com.imranandroid.demo.ACTION_ON_ImG_CLICK"; 
     public static RemoteViews rview; 
public static int ii=0; 
     @Override 
     public void onUpdate(Context paramContext, AppWidgetManager appWidgetManager, 
       int[] appWidgetIds){ 
      //REMOVE YOUR CODE FROM HERE 
      updateWidgetState(paramContext, "");  
     } 
     @Override 
     public void onReceive(Context paramContext, Intent paramIntent) 
      { 
      String str = paramIntent.getAction(); 
      if (paramIntent.getAction().equals(TAG_IMAGE_CLICK)) { 
       updateWidgetState(paramContext, str); 
      } 
      else 
      { 
       super.onReceive(paramContext, paramIntent); 
      } 
      } 
     static void updateWidgetState(Context paramContext, String paramString) 
      { 
      RemoteViews localRemoteViews = buildUpdate(paramContext, paramString); //CALL HERE 
      ComponentName localComponentName = new ComponentName(paramContext, TestwidgetProvider.class); 
      AppWidgetManager.getInstance(paramContext).updateAppWidget(localComponentName, localRemoteViews); 
      } 
    private static RemoteViews buildUpdate(Context paramContext, String paramString) 
     { 
     rview = new RemoteViews(paramContext.getPackageName(), R.layout.widget_layoutmain); 
     Intent active = new Intent(paramContext, TestwidgetProvider.class); //YOUR WIDGET NAME 
     active.setAction(TAG_IMAGE_CLICK); 
     PendingIntent actionPendingIntent = PendingIntent.getBroadcast(paramContext, 0, active, 0); 
     rview.setOnClickPendingIntent(R.id.MyWidgetTxt, actionPendingIntent); 
     if (paramString.equals(TAG_IMAGE_CLICK)) { 
     if(ii==0) 
      { 
      rview.setImageViewResource(R.id.ImageView01a, R.drawable.off); 
        ii=1; 
      } 
      else 
      { 
      rview.setImageViewResource(R.id.ImageView01a, R.drawable.on); 
        ii=0; 
      } 
      } 
     return rview; 
     } 
    } 
+0

こんにちはハント、私の答えbezとの任意のprobこれは私の最後には、動作例です.. –

+0

どのようにbuildUpdateが呼び出されるのですか? – Hunt

+0

これを参照してください: - [ウィジェットがクリックされました](http://stackoverflow.com/questions/9671596/how-do-i-start-an-activity-when-my-widget-is-clicked/9671968#9671968) –

関連する問題