[英]how to update widget only if wifi state changes?
Following this tutorial I quickly created a simple widget which displays and updates the current time every second on the home screen: 在学习完本教程之后,我迅速创建了一个简单的小部件,该小部件每秒在主屏幕上显示和更新当前时间:
Question: I was wondering how this code has to be changed, so that the time is only updated when a certain event occurs. 问题:我想知道如何更改此代码,以便仅在发生特定事件时才更新时间。 Eg: update the time only if wifi state changes . 例如: 仅在wifi状态更改时才更新时间 。
Here are the (hopefully) relevant code sections: 这是(希望)相关的代码部分:
<!-- 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>
Here's the widget class code: 这是小部件类代码:
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);
}
}
}
To trigger when a certain event occurs, you create a broastcast receiver
in manifest file (note that not all event will be trigger, for example : SMS received can, but SMS sending cannot). 要在发生某个事件时触发,请在清单文件中创建一个broastcast receiver
(请注意,并非所有事件都会触发,例如:可以接收SMS,但不能发送SMS)。
Here is the example for Network State Change. 这是网络状态更改的示例。
1) Create a broastcast receiver: 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) This broastcast receiver will run code from NetworkStateChangeService
. 2)此广播广播接收器将运行NetworkStateChangeService
代码。 Here is the main code of 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
}
}
Note : WakefulIntentService
is a custom class invented by commonsguy
注意: WakefulIntentService
是commonsguy
发明的自定义类
3) You register your receiver and service in manifest file: 3)您在清单文件中注册接收器和服务:
<receiver android:name="NetworkStateChangeReceiver ">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<service
android:name="NetworkStateChangeService">
</service>
Above is the framework often if you want to do with Broastcast Receiver
. 如果要使用Broastcast Receiver
通常是上面的框架。 As you see some other easier tutorial, they just put main code in onreceive
of broadcast receiver
, but if your work takes long time, you should put in service, and should use WakefulIntentService
because it helps devices awake while doing your work. 如您所见,还有一些更简单的教程,它们只是将主要代码放在broadcast receiver
onreceive
上,但是如果您的工作花费很长时间,则应该投入使用,并应使用WakefulIntentService
因为它可以帮助设备在工作时唤醒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.