问题描述
我想要更新的窗口小部件的文本和它的颜色,只要网络分组数据连接的变化。即使是这个小工具可以启用/禁用网络数据包的数据。广播接收器android.net.conn.CONNECTIVITY_CHANGE已经登记在Android清单文件的onReceive()已在AppWidgetProvider类中重写。
I want to update the widget text and its color whenever Network-packet-data connection changes. Even this widget can enable/disable Network-packet-data. Broadcast receiver for android.net.conn.CONNECTIVITY_CHANGE has been register in Android manifest file and onReceive() is already overridden in AppWidgetProvider class.
我注意到一个很奇怪的现象,每当我打开/关闭连接的onReceive()是越来越触发两次。在第一种情况,我收到的NetworkInfo
的对象,但二审我没有收到信息的实例。 但是,当我能够通过系统设置的onReceive()是越来越触发一次/禁止连接和的NetworkInfo
对象是空的。
I am noticing a very strange behavior whenever I enable/disable the connection onReceive() is getting triggered two times. In first instance I receive NetworkInfo
object but in second instance I don't receive the info instance. But when I enable/disable connection by the system setting onReceive() is getting triggered once and NetworkInfo
object is null.
这是我的清单文件。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rakesh.simplewidget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<!-- Permissions -->
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<!-- Widget Broadcast receiver -->
<receiver
android:name=".ExampleAppWidgetProvider"
android:label="Widget ErrorBuster" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget1_info" />
</receiver>
<service android:name=".UpdateWidgetService"></service>
</application>
</manifest>
我的AppWidgetProvider类是如下。
My AppWidgetProvider class is as following.
公共类ExampleAppWidgetProvider扩展AppWidgetProvider {
public class ExampleAppWidgetProvider extends AppWidgetProvider {
int[] mAppWidgetIds;
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
this.mAppWidgetIds = appWidgetIds;
// Get all ids
ComponentName thisWidget = new ComponentName(context,
ExampleAppWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
// Build the intent to call the service
Intent intent = new Intent(context.getApplicationContext(),
UpdateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
// Update the widgets via the service
context.startService(intent);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
NetworkInfo info = (NetworkInfo)intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
StringBuilder str= new StringBuilder();
if(info !=null){
str.append(" info is NULL");
}
if(checkConnectivityState(context)){
str.append("; data is enable");
}else{
str.append("; data is disable");
}
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
public static void updateAppWidget(Context context,
int[] appWidgetIds, boolean enable){
if(appWidgetIds == null){
return;
}
if(appWidgetIds.length > 0){
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget1);
if(enable){
updateViews.setTextColor(R.id.BtEnableDisable, Color.GREEN);
updateViews.setTextViewText(R.id.BtEnableDisable, "Enabled");
}else{
updateViews.setTextColor(R.id.BtEnableDisable, Color.GRAY);
updateViews.setTextViewText(R.id.BtEnableDisable, "Disabled");
}
appWidgetManager.updateAppWidget(appWidgetIds, updateViews);
Toast.makeText(context, "updateAppWidget() ", Toast.LENGTH_SHORT).show();
}
}
private boolean checkConnectivityState(Context context){
final TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
return telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED;
}
}
服务类,启用/禁用网络分组数据连接。
Service class which enables/disables the Network-packet-data connection.
public class UpdateWidgetService extends Service {
private static final String LOG = "de.vogella.android.widget.example";
@Override
public void onStart(Intent intent, int startId) {
Log.i(LOG, "Called");
// Create some random data
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
.getApplicationContext());
int[] allWidgetIds = intent
.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
ComponentName thisWidget = new ComponentName(getApplicationContext(),
ExampleAppWidgetProvider.class);
int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
Log.w(LOG, "From Intent" + String.valueOf(allWidgetIds.length));
Log.w(LOG, "Direct" + String.valueOf(allWidgetIds2.length));
for (int widgetId : allWidgetIds) {
// Create some random data
int number = (new Random().nextInt(100));
RemoteViews remoteViews = new RemoteViews(this
.getApplicationContext().getPackageName(),
R.layout.widget1);
Log.w("WidgetExample", String.valueOf(number));
EnableDisableConnectivity edConn = new EnableDisableConnectivity(this.getApplicationContext());
edConn.enableDisableDataPacketConnection(!checkConnectivityState(this.getApplicationContext()));
// Register an onClickListener
Intent clickIntent = new Intent(this.getApplicationContext(),
ExampleAppWidgetProvider.class);
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
allWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.BtEnableDisable, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
stopSelf();
super.onStart(intent, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private boolean checkConnectivityState(Context context){
final TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
return telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED;
}
}
我也试过用广播接收器类有我收到的NetworkInfo每当有变化的连接。但我不知道如何更新从广播接收器的小部件文字/色彩。
I also tried with a Broadcast Receiver class there I receive the NetworkInfo whenever there is change in connection. But I don't know how to update the widget text/color from broadcast receiver.
有一次我收到CONNECTIVITY_CHANGE意图我可以更新窗口小部件的文字/色彩任何其他方式?
Is there any other way I can update the widget text/color once I receive CONNECTIVITY_CHANGE intent?
我无法想通了,为什么我获得两个CONNECTIVITY_CHANGE意图 AppWidgetProvider
,但我必须学会如何更新从广播接收器窗口小部件,以下为$ C $℃。
I couldn't figured out why I am getting two CONNECTIVITY_CHANGE intent in AppWidgetProvider
, but I got to learn How to update the widget from Broadcast receiver and below is the code.
public class ConnectivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NetworkInfo info = (NetworkInfo)intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
if(info.getType() == ConnectivityManager.TYPE_MOBILE){
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget1);
if(info.isConnectedOrConnecting()){
Toast.makeText(context, "Data packet enabled", Toast.LENGTH_SHORT).show();
Log.d("RK","Mobile data is enabled");
remoteViews.setTextColor(R.id.BtEnableDisable, Color.GREEN);
remoteViews.setTextViewText(R.id.BtEnableDisable, "Enabled");
}else{
Toast.makeText(context, "Data packet disabled", Toast.LENGTH_SHORT).show();
Log.e("RK","Mobile data is disconnected");
remoteViews.setTextColor(R.id.BtEnableDisable, Color.BLACK);
remoteViews.setTextViewText(R.id.BtEnableDisable,"Disabled");
}
ComponentName thiswidget = new ComponentName(context, ExampleAppWidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thiswidget, remoteViews);
}
}
}
如果有人知道为什么我获得两个CONNECTIVITY_CHANGE意向,请点击这里分享您的想法。你帮会在这里pciated真的AP $ P $。
If someone knows why I am getting two CONNECTIVITY_CHANGE intent, please share your thoughts here. you help would be really appreciated here.
推荐答案
现在我已经发现为什么的onReceive()
被调用时,点击按钮启用两次/禁用网络。1.第一次被调用时, APPWIDGET_UPDATE
意图广播。2,第二次它被调用时, CONNECTIVITY_CHANGE
意图广播。
Now I have discovered why onReceive()
gets called twice when button is clicked to enable/disable the network. 1. First time it gets called when APPWIDGET_UPDATE
intent broadcasts.2. Second time it gets called when CONNECTIVITY_CHANGE
intent broadcasts.
反正我可以更新从广播接收器的小部件,源$ C $ C已被张贴在上面的帖子。
Anyway I am able to update the widget from Broadcast receiver, the source code has been posted in the above post.
这篇关于如何更新插件收到一经播出意向CONNECTIVITY_CHANGE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!