从服务接收广播到appWidgetProvider

时间:2016-05-25 09:16:07

标签: android xamarin broadcastreceiver android-widget android-service

我有一个自定义appWidgetProvider接收不同的系统广播。(蓝牙和wifi状态变化等)

只要没有FlashLight状态更改的操作,我就添加了自定义操作。

所以我正在尝试从serviceappWidgetProvider发送包含自定义操作的广播,但无法真正解决此问题。

这是我到目前为止所做的:

[BroadcastReceiver(Label = "Root Manager")]
[IntentFilter(new string[] 
    {
        RootWidget.FlashWidget, "android.appwidget.action.APPWIDGET_UPDATE", 
        BluetoothAdapter.ActionStateChanged, WifiManager.WifiStateChangedAction
    })]    
[MetaData("android.appwidget.provider", Resource = "@xml/bt_widget")]
public class RootWidget : AppWidgetProvider
{
    public static Camera camera;
    public const string FlashWidget = "com.flashlight.STATE_CHANGED";

    public override void OnReceive(Context context, Intent intent)
    {
        // some code

            UpdateWidget(context);

        // some code           
    }

    private void UpdateWidget(Context context)
    {
        // some code 

        if (camera.GetParameters().FlashMode == Camera.Parameters.FlashModeTorch)
        {
            Intent flashIntent = new Intent(context, typeof(DisableFlashService));
            PendingIntent pendingIntent = PendingIntent.GetService(context, 0, flashIntent, PendingIntentFlags.UpdateCurrent);
            remoteViews.SetOnClickPendingIntent(Resource.Id.flash, pendingIntent);
        }
        else
        {
            Intent flashIntent = new Intent(context, typeof(EnableFlashService));
            PendingIntent pendingIntent = PendingIntent.GetService(context, 0, flashIntent, PendingIntentFlags.UpdateCurrent);
            remoteViews.SetOnClickPendingIntent(Resource.Id.flash, pendingIntent);
        }

        // some code
    }
}   

服务:

[Service]
    class EnableFlashService : IntentService
    {
        protected override void OnHandleIntent(Intent intent)
        {            
            // turn on flash

            Intent widgetIntent = new Intent(RootWidget.FlashWidget);
            SendBroadcast(intent);
        }
    }


    [Service]
    class DisableFlashService : IntentService
    {
        protected override void OnHandleIntent(Intent intent)
        {
            // turn off flash

            Intent widgetIntent = new Intent(RootWidget.FlashWidget);
            SendBroadcast(intent);
        }
    }

总而言之,我希望RootWidget接收来自服务发送的自定义操作的广播。

0 个答案:

没有答案