Android以编程方式将小部件添加到应用程序警告消息

Android Adding Widgets To App Programmatically Warning Message(Android以编程方式将小部件添加到应用程序警告消息)

本文介绍了Android以编程方式将小部件添加到应用程序警告消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个测试 Android 启动器应用程序,并将其作为系统应用程序安装在有根设备上.目标是让应用程序将所有已安装的小部件放置在 viewpager 的片段上.这已成功完成;但是,每次显示新页面时都会向用户显示一条消息.

I have created a test Android launcher application and installed it on a rooted device as a system app. The goal is for the app to place all installed widgets on fragments of a viewpager. This has been successfully accomplished; however, the user is shown a message every time a new page is shown.

即使用户选中该框,每次添加小部件时仍会显示该消息.

Even if the user checks the box, the message is still shown every time a widget is to be added.

我通常遵循 在 android 启动器中托管小部件中的步骤和 在没有 bindAppWidgetId() 的情况下将小部件添加到启动器页面将小部件添加到应用程序.

I have generally followed the steps in Hosting widgets in an android launcher and Adding widgets to a launcher page without bindAppWidgetId() to add widgets to the app.

首先我得到一个扩展应用程序的类中的小部件列表并将其存储在一个公共静态变量中

First I get a list of widgets in the class that extends application and store it in a public static variable

AppWidgetManager manager = AppWidgetManager.getInstance(this); 
widgetList = manager.getInstalledProviders();

接下来我使用绑定意图来获取小部件

Next I use a bind intent to get the widget

appWidgetManager = AppWidgetManager.getInstance(this.getActivity());
appWidgetHost = new AppWidgetHost(this.getActivity(), APPWIDGET_HOST_ID);
int id = appWidgetHost.allocateAppWidgetId();
Intent bindIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
bindIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
bindIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER,LauncherExperiment.widgetList.get(mParam1).provider);
startActivityForResult(bindIntent, REQUEST_BIND_APPWIDGET);

然后根据活动结果创建小部件

Then on activity result I create the widget

Bundle extras = data.getExtras();
int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
AppWidgetProviderInfo appWidgetInfo = appWidgetManager.getAppWidgetInfo(appWidgetId);

AppWidgetHostView hostView = appWidgetHost.createView(this.getActivity(), appWidgetId, appWidgetInfo);
hostView.setAppWidget(appWidgetId, appWidgetInfo);
// Add  it on the layout you want
myLayout.addView(hostView);

再次,我已通过更新清单将设备植根并安装为系统应用

Again, I have rooted the device and installed the app as a system app by updating the manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.launcherexperiment"
android:sharedUserId="android.uid.system" >

并按照此处的步骤操作:如何使用系统签名签署我的应用程序钥匙?

And by following the steps here: How can I sign my application with the system signature key?

所以综上所述,我已经能够成功的绑定应用并展示出来了;但问题是警告信息应该最多显示一次.

So in summary, I have been able to successfully bind apps and display them; but the issue is that the warning message should be displayed at most one time.

推荐答案

开始吧!使用此代码,用户只会看到一次对话框.它还展示了以编程方式添加小部件的完整解决方案.

Here we go! With this code the user will only be shown the dialog once. It also shows a complete solution for adding widgets programmatically.

由于我将在我的 github 上更新它,请在此处查找最新版本,https://github.com/DagW/FragmentHomescreen/blob/master/app/src/main/java/se/dw/fragmenthomescreen/widget/WidgetFragment.java.

As I will be updating this on my github, look here for the latest version, https://github.com/DagW/FragmentHomescreen/blob/master/app/src/main/java/se/dw/fragmenthomescreen/widget/WidgetFragment.java.

但出于存档目的,这是我现在的做法.

But for archiving purposes here is the way I do it now.

/**
 * checkToAddWidget
 * <p/>
 * At the creation of WidgetFragment,
 * add a default widget so that the homescreen is not empty
 * shows how to add widgets programmatically
 */
private void checkToAddWidget () {
        MainActivity m = (MainActivity) getActivity();
        AppWidgetManager manager = m.getAppWidgetManager();
        AppWidgetHost host = m.getAppWidgetHost();

        List<AppWidgetProviderInfo> widgetList = manager.getInstalledProviders();

        AppWidgetProviderInfo searchProvider = null;
        AppWidgetProviderInfo search2Provider = null;
        AppWidgetProviderInfo clockProvider = null;

        for ( AppWidgetProviderInfo info : widgetList ) {
            Log.d(TAG, info.provider.getPackageName() + " " + info.provider.getClassName());
            if ( info.provider.getClassName().equals("com.google.android.googlequicksearchbox.SearchWidgetProvider") ) {
                searchProvider = info;
                break;
            }
            if ( info.provider.getClassName().equals("com.android.alarmclock.AnalogAppWidgetProvider") ) {
                clockProvider = info;
            }
            if ( info.provider.getClassName().equals("com.android.alarmclock.DigitalAppWidgetProvider") ) {
                clockProvider = info;
            }
            if ( info.provider.getClassName().equals("com.android.quicksearchbox.SearchWidgetProvider") ) {
                search2Provider = info;
            }
        }
        if ( searchProvider != null || search2Provider != null || clockProvider != null ) {
            AppWidgetProviderInfo provider = null;
            if ( searchProvider != null ) {
                provider = searchProvider;
            } else if ( clockProvider != null ) {
                provider = clockProvider;
            } else {
                provider = search2Provider;
            }

            addProvider(m, host, manager, provider);
        }
    }



public void addProvider (MainActivity m, AppWidgetHost host, AppWidgetManager manager, AppWidgetProviderInfo provider) {
    int id = host.allocateAppWidgetId();
    boolean success = false;
    success = manager.bindAppWidgetIdIfAllowed(id, provider.provider);

    if ( success ) {
        AppWidgetHostView hostView = host.createView(getActivity(), id, provider);
        AppWidgetProviderInfo appWidgetInfo = manager.getAppWidgetInfo(id);

        LauncherAppWidgetInfo info = new LauncherAppWidgetInfo(id);
        info.setHostView(hostView);
        info.getHostView().setAppWidget(id, appWidgetInfo);

        ItemInfo launcherInfo = attachWidget(info);
        if ( launcherInfo != null ) {
            WidgetPersistance.addDesktopAppWidget(screen, launcherInfo);
        }

    } else {
        Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, provider.provider);
        m.startActivityForResult(intent, MainActivity.REQUEST_BIND_APPWIDGET);
    }

}

你可能还需要

<uses-permission android:name="android.permission.BIND_APPWIDGET"
                 tools:ignore="ProtectedPermissions"/>

这篇关于Android以编程方式将小部件添加到应用程序警告消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android以编程方式将小部件添加到应用程序警告消息

基础教程推荐