2013-07-18 74 views
1

我正在嘗試How do I run an Activity from a button on a widget in Android?以便點擊圖像按鈕啓動我的應用程序。當我取消註釋代碼時,它會切換圖像,但是當我嘗試使用此代碼時,它什麼都不做。我想推出我的應用程序。通過點擊小工具啓動我的應用程序

public class MyWidgetIntentReceiver extends BroadcastReceiver { 

    private static int clickCount = 0; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if(intent.getAction().equals("pl.looksok.intent.action.CHANGE_PICTURE")){ 
      updateWidgetPictureAndButtonListener(context); 
     } 
    } 

    private void updateWidgetPictureAndButtonListener(Context context) { 
     Log.i("PROJECTCARUSO", "updateWidgetPictureAndButtonListener"); 
     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget); 
//  remoteViews.setImageViewResource(R.id.widget_button, getImageToSet()); 
// 
//  //REMEMBER TO ALWAYS REFRESH YOUR BUTTON CLICK LISTENERS!!! 
//  remoteViews.setOnClickPendingIntent(R.id.widget_button, MyWidgetProvider.buildButtonPendingIntent(context)); 
// 
//  MyWidgetProvider.pushWidgetUpdate(context.getApplicationContext(), remoteViews); 

     Intent intentClick = new Intent(context, FragmentChange.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity (context, 0, 
     intentClick, 0); 
     remoteViews.setOnClickPendingIntent (R.id.widget_button, pendingIntent); 
    } 

    private int getImageToSet() { 
     clickCount++; 
     return clickCount % 2 == 0 ? R.drawable.app_icon : R.drawable.energy; 
    } 
} 

回答

0

這裏就是我終於開始工作:

public class WidgetProvider extends AppWidgetProvider { 

    public static String SWITCH_WIDGET_UPDATE = "MainActivity.Switch"; 


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

     Log.i("PROJECTCARUSO","onUpdate"); 

     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget); 
     remoteViews.setOnClickPendingIntent(R.id.widget_button, buildButtonPendingIntent(context)); 

     Intent intentClick = new Intent(context, FragmentChange.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity (context, 0, intentClick, 0); 
     remoteViews.setOnClickPendingIntent (R.id.widget_button, pendingIntent); 

     pushWidgetUpdate(context, remoteViews); 

    } 


    public static PendingIntent buildButtonPendingIntent(Context context) { 
     Intent intent = new Intent(); 
     intent.setAction("CHANGE_PICTURE"); 
     return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    } 


    public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) { 
     ComponentName myWidget = new ComponentName(context, WidgetProvider.class); 
     AppWidgetManager manager = AppWidgetManager.getInstance(context); 
     manager.updateAppWidget(myWidget, remoteViews);  

    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     super.onReceive(context, intent); 
    } 
} 
相關問題