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



我有小部件,当我点击它时,我将使用透明的后台开始新的活动,我想隐藏小部件,这样只有活动出现在主屏幕上,或者想办法将活动窗口放在小部件的顶部,但我不能使用intet.getSourceBounds方法,因为我没有使用API 7级或高于的方法。

尝试使用以下代码示例

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

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

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

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

我找到了解决方案

     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);
 }
 }

相关内容

  • 没有找到相关文章

最新更新