1

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

在此处输入图像描述

问题: 我想知道如何更改此代码,以便仅在发生特定事件时更新时间。例如:仅当 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>    

这是小部件类代码:

public class HelloWidget extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);
    }

    private class MyTime extends TimerTask {
        RemoteViews remoteViews;
        AppWidgetManager appWidgetManager;
        ComponentName thisWidget;
        DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());

    public MyTime(Context context, AppWidgetManager appWidgetManager) {
        this.appWidgetManager = appWidgetManager;
        remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
        thisWidget = new ComponentName(context, HelloWidget.class);
    }

    @Override
    public void run() {
        remoteViews.setTextViewText(R.id.widget_textview, "TIME = " +format.format(new Date()));
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);
    }

    } 
}
4

1 回答 1

1

要在某个事件发生时触发,您可以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) 这个广播接收器将从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是由commonsguy

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

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

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

如果您想使用上述框架,则经常使用Broastcast Receiver. 正如您看到的其他一些更简单的教程,他们只是将主要代码放入onreceive其中broadcast receiver,但如果您的工作需要很长时间,您应该投入使用,并且应该使用,WakefulIntentService因为它可以帮助设备在工作时保持清醒。

于 2013-05-20T14:41:34.163 回答