AppWidgetHost上的AppWidgets在重启后消失

时间:2017-08-26 00:58:09

标签: java android android-source android-appwidget

现在这是使用AppWidgets的非常规方法。我在AOSP的系统级工作。基本上AppWidgets有一个自定义的小部件类别,我调查以获得Providiers和我需要的一切。现在问题是双重的:1。)AppWidgets不会像他们应该的那样更新2.)它们在重新启动后消失,不再显示在小部件列表中。当我将类别设置为启动器窗口小部件时也会发生这种情况(当服务未运行时,启动器窗口小部件按预期工作)。我可以尽可能提供日志和堆栈跟踪,并做任何事情来帮助解决这个问题。我用这个撕掉了我的头发。 AppWidgetHost的代码如下所示。

注意: - 我无法使用设置意图来选择AppWidgets,因为我需要一个自定义选择器。

public class SecondScreenService extends Service {

private ArrayList<View> ssas = new ArrayList<>();
private ArrayList<Integer> appWidgetIds = new ArrayList<>();
private ArrayList<AppWidgetProviderInfo> secondScreenWidgets = new ArrayList<>();
private FrameLayout fakeRootView;
private SecondScreenView rootView;
private ViewsPagerAdapter viewsPagerAdapter;
private ViewPager viewPager;
private WindowManager wm;
private DisplayManager dm;
private LayoutInflater inflater;
private AppWidgetManager mAppWidgetManager;
private AppWidgetHost mAppWidgetHost;
private BroadcastReceiver mReceiver = new PackageReceiver();
private ArrayList<Integer> pos = new ArrayList<>();
private int scrollState;
public static final int APPWIDGET_HOST_ID = 3254;
public static final String TAG = "BLUG";

private final IBinder mBinder = (IBinder) new LocalBinder();                                                                                              

public class LocalBinder extends Binder {                                                                                                                
    public SecondScreenService getService() {                                                                                                            
        return SecondScreenService.this;                                                                                                                  
    }                                                                                                                                                    
}                                                                                                                                                        

@Override                                                                                                                                                
public IBinder onBind(Intent intent) {                                                                                                                    
    return mBinder;                                                                                                                                      
}                                                                                                                                                        

@Override                                                                                                                                                
public void onCreate() {                                                                                                                                  
    inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);

    fakeRootView = (FrameLayout) inflater.inflate(R.layout.second_screen_root, null);

    rootView = (SecondScreenView) fakeRootView.findViewById(R.id.secondscreenview);

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
            PixelFormat.OPAQUE);

    params.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SECOND_SCREEN;
    params.gravity = Gravity.LEFT | Gravity.TOP;
    params.x = 400;
    params.y = 0;
    params.width = 1040;
    params.height = 160;

    wm.addView(fakeRootView, params);

    viewPager = (ViewPager) fakeRootView.findViewById(R.id.second_screen_viewpager);
    viewsPagerAdapter = new ViewsPagerAdapter(this);
    viewPager.setAdapter(viewsPagerAdapter);

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
    intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
    intentFilter.addDataScheme("package");

    registerReceiver(mReceiver, intentFilter);
    mAppWidgetManager = AppWidgetManager.getInstance(this);
    mAppWidgetHost = new AppWidgetHost(this, SecondScreenService.APPWIDGET_HOST_ID);
    mAppWidgetHost.startListening();
    pollWidgets();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mAppWidgetHost.stopListening();
    if (fakeRootView != null) {
        wm.removeView(fakeRootView);
        fakeRootView = null;
    }
}

private void pollWidgets() {
    for(View view : ssas) {
        mAppWidgetHost.deleteAppWidgetId(((AppWidgetHostView) view).getAppWidgetId());
    }
    ssas.clear();
    int appWidgetId = this.mAppWidgetHost.allocateAppWidgetId();
    List<AppWidgetProviderInfo> widgets = new ArrayList<>();
    mAppWidgetManager.getInstalledProviders(AppWidgetProviderInfo.WIDGET_CATEGORY_SECOND_SCREEN);
    widgets.addAll(mAppWidgetManager.getInstalledProviders(36864));
    for(AppWidgetProviderInfo awpi : widgets) {
        System.out.println("BLUG: SysUI: got widget " + awpi.provider.toString());
        mAppWidgetManager.bindAppWidgetId(appWidgetId, awpi.provider);
        AppWidgetHostView hostView = mAppWidgetHost.createView(this, appWidgetId, awpi);
        hostView.setAppWidget(appWidgetId, awpi);
        ssas.add((View) hostView);
    }
    if(this.viewPager != null && this.viewsPagerAdapter != null) {
        viewsPagerAdapter = new ViewsPagerAdapter(this);
        viewPager.setAdapter(viewsPagerAdapter);
    }
}

private class PackageReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                SecondScreenService.this.pollWidgets();
            }
        }, 1000);
    }
}

private class ViewsPagerAdapter extends PagerAdapter {
    private Context mContext;

    public ViewsPagerAdapter(Context context) {
        mContext = context;
    }

    @Override
    public Object instantiateItem(ViewGroup collection, int position) {
        View view = (View) SecondScreenService.this.ssas.get(position);
        collection.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup collection, int position, Object view) {
        collection.removeView((View) view);
    }

    @Override
    public int getCount() {
        return SecondScreenService.this.ssas.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "";
    }
}
}

0 个答案:

没有答案