Android Widget可在调试中使用,但不能在发行版中使用

时间:2018-08-28 09:55:30

标签: c# android widget

我编写了一个Android小部件,该小部件在调试过程中运行良好,但是在发行后,似乎没有调用AppWidget.cs-Program,因为我只能看到在Layout中创建的内容。 该程序用于激活或禁用灯,该灯在程序调用该站点时会作出反应,例如:“ {http://192.168.0.52/ein”激活灯,“ http://192.168.0.52/aus”禁用灯,“ {{3}” }”可获取灯泡的当前状态(“开”或“关”)。预先谢谢你!

AppWidget.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

using Android.App;
using Android.Appwidget;
using Android.Content;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace OrigWidget
{
    [BroadcastReceiver(Label = "IoT-Lampe")]
    [IntentFilter(new string[] { "android.appwidget.action.APPWIDGET_UPDATE" })]
    [MetaData("android.appwidget.provider", Resource = "@xml/appwidgetprovider")]
    public class AppWidget : AppWidgetProvider
    {
        WebClient client;
        RemoteViews widgetView;
        AppWidgetManager awm;
        Context c;
        int[] aWid;

        public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
        {
            awm = appWidgetManager;
            c = context;
            aWid = appWidgetIds;
            System.Threading.Timer t = new System.Threading.Timer(upd, null, 0, 5000);
        }
        private void upd(Object o)
        {
            var me = new ComponentName(c, Java.Lang.Class.FromType(typeof(AppWidget)).Name);
            awm.UpdateAppWidget(me, BuildRemoteViews(c, aWid));
        }

        private RemoteViews BuildRemoteViews(Context context, int[] appWidgetIds)
        {
            widgetView = new RemoteViews(context.PackageName, Resource.Layout.Widget);

            SetTextViewText(widgetView);
            RegisterClicks(context, appWidgetIds, widgetView);

            return widgetView;
        }

        private void SetTextViewText(RemoteViews widgetView)
        {
            Check();
        }

        public void Check()
        {
            client = new WebClient();
            String htmlText = client.DownloadString("http://192.168.0.52/state");
            client.Dispose();

            if (htmlText == "on")
            {
                widgetView.SetInt(Resource.Id.an, "setBackgroundColor", Color.LimeGreen);
                widgetView.SetInt(Resource.Id.aus, "setBackgroundColor", Color.Rgb(71, 66, 65));
            }
            else if (htmlText == "off")
            {
                widgetView.SetInt(Resource.Id.an, "setBackgroundColor", Color.Rgb(71, 66, 65));
                widgetView.SetInt(Resource.Id.aus, "setBackgroundColor", Color.Red);
            }
        }

        private static string AusClick = "AusClickTag";
        private static string AnClick = "AnClickTag";

        public override void OnReceive(Context context, Intent intent)
        {
            base.OnReceive(context, intent);

            if (AnClick.Equals(intent.Action))
            {
                try
                {
                    client = new WebClient();
                    String unnecessary = client.DownloadString("http://192.168.0.52/ein");
                    String nec = client.DownloadString("http://192.168.0.52/state");
                    client.Dispose();
                }
                catch (Exception) { }
            }

            if (AusClick.Equals(intent.Action))
            {
                try
                {
                    client = new WebClient();
                    String unnecessary = client.DownloadString("http://192.168.0.52/aus");
                    client.Dispose();
                }
                catch (Exception) { }
            }
        }

        private void RegisterClicks(Context context, int[] appWidgetIds, RemoteViews widgetView)
        {
            var intent = new Intent(context, typeof(AppWidget));
            intent.SetAction(AppWidgetManager.ActionAppwidgetUpdate);
            intent.PutExtra(AppWidgetManager.ExtraAppwidgetIds, appWidgetIds);

            widgetView.SetOnClickPendingIntent(Resource.Id.an, GetPendingSelfIntent(context, AnClick));
            widgetView.SetOnClickPendingIntent(Resource.Id.aus, GetPendingSelfIntent(context, AusClick));
        }

        private PendingIntent GetPendingSelfIntent(Context context, string action)
        {
            var intent = new Intent(context, typeof(AppWidget));
            intent.SetAction(action);
            return PendingIntent.GetBroadcast(context, 0, intent, 0);
        }
    }
}

0 个答案:

没有答案