Android 如何仅在wifi状态更改时更新widget?

Android 如何仅在wifi状态更改时更新widget?,android,android-widget,android-activity,Android,Android Widget,Android Activity,根据教程,我快速创建了一个简单的小部件,在主屏幕上每秒显示并更新当前时间: 问题: 我想知道该如何更改代码,以便只在某个事件发生时更新时间。例如:仅当wifi状态更改时更新时间 以下是(希望如此)相关的代码部分: <!-- Broadcast Receiver --> <receiver android:name=".WifiSSIDWidget" android:label="@string/app_name"> <intent-filter>

根据教程,我快速创建了一个简单的小部件,在主屏幕上每秒显示并更新当前时间

问题: 我想知道该如何更改代码,以便只在某个事件发生时更新时间。例如:仅当wifi状态更改时更新时间

以下是(希望如此)相关的代码部分:

<!--  Broadcast Receiver -->
<receiver android:name=".WifiSSIDWidget" android:label="@string/app_name">
    <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> 
    </intent-filter>
    <meta-data android:name="android.appwidget.provider" android:resource="@xml/wifi_ssid_widget_provider" />
</receiver>    

若要在某个事件发生时触发,请在清单文件中创建一个
broastcast receiver
(请注意,并非所有事件都将触发,例如:SMS接收可以,但SMS发送不能)

下面是网络状态更改的示例

1) 创建广播广播接收器:

public class NetworkStateChangeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.i(TAG, "Receive Network State Change");

            // you put your main code here. 
            // or if you don't want, put your code to service. So, run following code
        Intent myIntent = new Intent(context, NetworkStateChangeService .class);
        // put all intent data to this intent
                myIntent.putExtras(intent);
                WakefulIntentService.sendWakefulWork(context, myIntent);

    }
}
2) 此broastcast接收器将运行
NetworkStateChangeService
中的代码。以下是
NetworkStateChangeService
的主要代码:

public class NetworkStateChangeService extends WakefulIntentService {

    public static String TAG = "SMS Service";

    public NetworkStateChangeService() {
        super("Network state change");
    }

    public SMSReceivedService(String name) {
        super(name);
    } 

    @Override
    protected void doWakefulWork(Intent intent) {
            // put your main code here
        }
}
注意:
WakefulIntentService
是由
CommonGuy

3) 在清单文件中注册接收方和服务:

    <receiver android:name="NetworkStateChangeReceiver "> 
                <intent-filter>
                    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                </intent-filter>
      </receiver>

 <service 
            android:name="NetworkStateChangeService">
 </service>


如果您想使用
broast-cast-Receiver
,通常会使用上述框架。正如您看到的其他一些更简单的教程,他们只是将主代码放在
广播接收器的
onreceive
中,但是如果您的工作需要很长时间,您应该投入服务,并且应该使用
WakefulIntentService
,因为它可以帮助设备在工作时唤醒。

当某个事件发生时触发,您可以在清单文件中创建一个
broastcast接收器
(请注意,并非所有事件都将被触发,例如:SMS接收可以,但SMS发送不能)

下面是网络状态更改的示例

1) 创建广播广播接收器:

public class NetworkStateChangeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.i(TAG, "Receive Network State Change");

            // you put your main code here. 
            // or if you don't want, put your code to service. So, run following code
        Intent myIntent = new Intent(context, NetworkStateChangeService .class);
        // put all intent data to this intent
                myIntent.putExtras(intent);
                WakefulIntentService.sendWakefulWork(context, myIntent);

    }
}
2) 此broastcast接收器将运行
NetworkStateChangeService
中的代码。以下是
NetworkStateChangeService
的主要代码:

public class NetworkStateChangeService extends WakefulIntentService {

    public static String TAG = "SMS Service";

    public NetworkStateChangeService() {
        super("Network state change");
    }

    public SMSReceivedService(String name) {
        super(name);
    } 

    @Override
    protected void doWakefulWork(Intent intent) {
            // put your main code here
        }
}
注意:
WakefulIntentService
是由
CommonGuy

3) 在清单文件中注册接收方和服务:

    <receiver android:name="NetworkStateChangeReceiver "> 
                <intent-filter>
                    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                </intent-filter>
      </receiver>

 <service 
            android:name="NetworkStateChangeService">
 </service>


如果您想使用
broast-cast-Receiver
,通常会使用上述框架。正如您看到的其他一些更简单的教程,他们只是将主代码放在
广播接收器的
onreceive
中,但是如果您的工作需要很长时间,您应该投入使用,而且应该使用WakefulIntentService,因为它可以帮助设备在工作时唤醒。

这是我必须定义的额外接收器还是我应该替换原来的接收器?我也不确定如何修改原始的
HelloWidget
代码。你介意详细说明一下吗?这是我必须定义的额外接收器还是我应该替换原来的接收器?我也不确定如何修改原始的
HelloWidget
代码。你介意详细说明一下吗?