我尝试使用在线发现的一些在线教程和一些反编译代码,首次制作一个简单的小部件。但是我在下面的方法中遇到了麻烦。我收到错误无法访问的代码。
private void Update(Context paramContext, AppWidgetManager paramAppWidgetManager, int[] paramArrayOfInt)
{
RemoteViews localRemoteViews = new RemoteViews(paramContext.getPackageName(), R.xml.widget);
WifiManager localWifiManager = (WifiManager)paramContext.getSystemService("wifi");
try
{
int i = localWifiManager.getWifiState();
if (i == 3)
{
localRemoteViews.setImageViewResource(R.xml.widget, R.drawable.off);
link(localRemoteViews, paramContext, paramAppWidgetManager);
if ((paramArrayOfInt == null) || (paramArrayOfInt.length <= 0))
break;
paramAppWidgetManager.updateAppWidget(paramArrayOfInt, localRemoteViews);
}
while (true)
{
return;
if (i == 1)
{
localRemoteViews.setImageViewResource(R.xml.widget, R.drawable.on);
break;
}
localRemoteViews.setImageViewResource(R.xml.widget, R.drawable.off);
break;
paramAppWidgetManager.updateAppWidget(this.THIS_WIDGET, localRemoteViews);
}
}
catch (Exception localException)
{
break;
}
}
这是全班:
package com.wifiswitch.bestwifiswitch;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.RemoteViews;
public class WifiWidget extends AppWidgetProvider {
static final String THIS_INTENT = "com.wifiswitch.bestwifiswitch.toggle";
final ComponentName THIS_WIDGET = new ComponentName("com.wifiswitch.bestwifiswitch", "com.wifiswitch.bestwifiswitch.WifiWidget");
private void Toggle(Context paramContext, String paramString)
{
if ("com.wifiswitch.bestwifiswitch.toggle".equals(paramString))
{
WifiManager localWifiManager = (WifiManager)paramContext.getSystemService("wifi");
try
{
int i = localWifiManager.getWifiState();
if (i == 3)
localWifiManager.setWifiEnabled(false);
else if (i == 1)
localWifiManager.setWifiEnabled(true);
}
catch (Exception localException)
{
}
}
}
private void Update(Context paramContext, AppWidgetManager paramAppWidgetManager, int[] paramArrayOfInt)
{
RemoteViews localRemoteViews = new RemoteViews(paramContext.getPackageName(), R.xml.widget);
WifiManager localWifiManager = (WifiManager)paramContext.getSystemService("wifi");
try
{
int i = localWifiManager.getWifiState();
if (i == 3)
{
localRemoteViews.setImageViewResource(R.xml.widget, R.drawable.off);
link(localRemoteViews, paramContext, paramAppWidgetManager);
if ((paramArrayOfInt == null) || (paramArrayOfInt.length <= 0))
break;
paramAppWidgetManager.updateAppWidget(paramArrayOfInt, localRemoteViews);
}
while (true)
{
return;
if (i == 1)
{
localRemoteViews.setImageViewResource(R.xml.widget, R.drawable.on);
break;
}
localRemoteViews.setImageViewResource(R.xml.widget, R.drawable.off);
break;
paramAppWidgetManager.updateAppWidget(this.THIS_WIDGET, localRemoteViews);
}
}
catch (Exception localException)
{
break;
}
}
private void link(RemoteViews paramRemoteViews, Context paramContext, AppWidgetManager paramAppWidgetManager)
{
Intent localIntent = new Intent("com.wifiswitch.bestwifiswitch.toggle");
localIntent.setComponent(this.THIS_WIDGET);
paramRemoteViews.setOnClickPendingIntent(2131099648, PendingIntent.getBroadcast(paramContext, 0, localIntent, 0));
}
public void onReceive(Context paramContext, Intent paramIntent)
{
String str = paramIntent.getAction();
if ("android.appwidget.action.APPWIDGET_UPDATE".equals(str))
{
Bundle localBundle = paramIntent.getExtras();
if (localBundle != null)
{
int[] arrayOfInt = localBundle.getIntArray("appWidgetIds");
if ((arrayOfInt != null) && (arrayOfInt.length > 0))
onUpdate(paramContext, AppWidgetManager.getInstance(paramContext), arrayOfInt);
}
}
while (true)
{
return;
if ("com.wifiswitch.bestwifiswitch.toggle".equals(str))
{
Toggle(paramContext, "com.roundedlabs.widgets.wifi.toggle");
Update(paramContext, AppWidgetManager.getInstance(paramContext), null);
}
else
{
Update(paramContext, AppWidgetManager.getInstance(paramContext), null);
}
}
}
public void onUpdate(Context paramContext, AppWidgetManager paramAppWidgetManager, int[] paramArrayOfInt)
{
Update(paramContext, paramAppWidgetManager, paramArrayOfInt);
}
}
答案 0 :(得分:-1)
问题在于onUpdate
方法中的break语句。 break语句将中断循环或switch语句。在您的代码中,您需要从方法返回。因此,将休息时间更改为返回。
其他问题在于onUpdate
和onReceive
方法。如果while(true)返回,则会中断循环并返回,以便不会达到下一个语句。您需要在此处删除return语句或根据任何条件更改您的逻辑以返回。
while (true)
{
return;
if ("com.wifiswitch.bestwifiswitch.toggle".equals(str))
{
Toggle(paramContext, "com.roundedlabs.widgets.wifi.toggle");
Update(paramContext, AppWidgetManager.getInstance(paramContext), null);
}