启动活动时使android小部件不可见

时间:2011-06-29 07:22:43

标签: android widget hide invisible

我有小部件,当我点击它我正在开始透明背景的新活动,我想隐藏该小部件,所以只有活动出现在主屏幕上或找到一种方法将活动窗口放在顶部小部件,但我不能使用intent.getSourceBounds方法因为我没有使用API​​级别7或以上

2 个答案:

答案 0 :(得分:0)

尝试使用以下代码示例

如果您使用以下代码创建了远程视图

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);

使用以下代码设置整个小部件的可见性

views.setViewVisibility(R.id.widget_counter, View.INVISIBLE);

答案 1 :(得分:0)

我找到了解决方案

     public class homeWidget extends AppWidgetProvider {
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
 @Override
    public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds )
    {
        RemoteViews remoteViews;
        ComponentName watchWidget;

        Intent active = new Intent(context, homeWidget.class);
        active.setAction(ACTION_WIDGET_RECEIVER);
        PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);


        remoteViews = new RemoteViews( context.getPackageName(), R.layout.widget );
        watchWidget = new ComponentName( context, homeWidget.class );

        remoteViews.setOnClickPendingIntent(R.id.buttonWidget1, actionPendingIntent);



        appWidgetManager.updateAppWidget( watchWidget, remoteViews );
    }
 @Override
 public void onEnabled(Context context){
     AppWidgetManager mgr = AppWidgetManager.getInstance(context); 
     RemoteViews remoteViews;
        ComponentName watchWidget;

        Intent active = new Intent(context, homeWidget.class);
        active.setAction(ACTION_WIDGET_RECEIVER);
        PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);


        remoteViews = new RemoteViews( context.getPackageName(), R.layout.widget );
        watchWidget = new ComponentName( context, homeWidget.class );

        remoteViews.setOnClickPendingIntent(R.id.buttonWidget1, actionPendingIntent);

        mgr.updateAppWidget(watchWidget,remoteViews);

 }
 @Override
 public void onReceive(Context context, Intent intent) {
     AppWidgetManager mgr = AppWidgetManager.getInstance(context); 
     if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
         Log.i("proverka", "uleva");
         Intent intent1 = new Intent(context,widget_activity.class);
         PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent1, 0);
         RemoteViews remoteViews;
         remoteViews = new RemoteViews( context.getPackageName(), R.layout.widget );

         ComponentName watchWidget;
         watchWidget = new ComponentName( context, homeWidget.class );

         remoteViews.setViewVisibility(R.id.LinearLayout01, View.GONE);
         try {
            pendingIntent.send();
        } catch (CanceledException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



         mgr.updateAppWidget(watchWidget,remoteViews);
         }
         super.onReceive(context, intent);
 }

 }