当前位置:操作系统 > 安卓/Android >>

Android动画效果Widget(wifi)

实现和飞行模式widget类似,只贴出不同代码:

public class WifiWidget extends AppWidgetProvider {

 

 private final static String SET_WIFI_ACTION = "com.archermind.wifi.widget.set_wifi";

 private static final int START_ANIMATION = 0;

 private static final int END_ANIMATION = 1;

 private static WifiManager mWifiManager;

 private static int wifiState;

 private static boolean mRunAnimation = false;

 private static RemoteViews mRemoteViews;

 

 private static Context mContext;

 private int [] mDrawable = {

   R.drawable.settings_icon_wifi_01,

   R.drawable.settings_icon_wifi_02,

   R.drawable.settings_icon_wifi_03

 };

 

 @Override

 public void onUpdate(Context context, AppWidgetManager appWidgetManager,

   int[] appWidgetIds) {

  if(mContext == null){

   mContext = context;

  }

  super.onUpdate(context, appWidgetManager, appWidgetIds);

  mWifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);

  mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.main);

  wifiState = mWifiManager.getWifiState();

  if(wifiState == WifiManager.WIFI_STATE_DISABLED){

   mRemoteViews.setImageViewResource(R.id.settings_wifi_switch, R.drawable.settings_switch_off);

  }else if(wifiState == WifiManager.WIFI_STATE_ENABLED){

   mRemoteViews.setImageViewResource(R.id.settings_wifi_switch, R.drawable.settings_switch_on);

  }

  Intent intentClick = new Intent(SET_WIFI_ACTION);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,

                intentClick, 0);

        mRemoteViews.setOnClickPendingIntent(R.id.settings_wifi, pendingIntent);

  for(int i=0; i<appWidgetIds.length; i++){

   appWidgetManager.updateAppWidget(appWidgetIds[i], mRemoteViews);

  }

 }

 

 

 @Override

 public void onReceive(Context context, Intent intent) {

  super.onReceive(context, intent);

  Log.e("method", "onReceive");

  if(intent.getAction().equals(SET_WIFI_ACTION)){

   wifiState = mWifiManager.getWifiState();

   Log.e("==", "==");

   Log.e("wifistate", ""+wifiState);

   if(wifiState == WifiManager.WIFI_STATE_DISABLED){

    mWifiManager.setWifiEnabled(true);

    Log.i("open", "open");

   }else if(wifiState == WifiManager.WIFI_STATE_ENABLED){

    mWifiManager.setWifiEnabled(false);

    Log.i("close", "close");

   }

  }else if(intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)){

   Log.e("change", "change");

   handleWifiStateChanged(intent.getIntExtra(

     WifiManager.EXTRA_WIFI_STATE,WifiManager.WIFI_STATE_UNKNOWN));

  }

 }

 //判断wifi状态

 private void handleWifiStateChanged(int state){

  Message msg = mHandler.obtainMessage(START_ANIMATION);

  msg.arg1 = 0;

  switch(state){

  case WifiManager.WIFI_STATE_DISABLING:

   Log.e("state", "WIFI_STATE_DISABLING");

   mRunAnimation = true;

   mHandler.sendMessage(msg);

   break;

  case WifiManager.WIFI_STATE_DISABLED:

   Log.e("state", "WIFI_STATE_DISABLED");

   mRunAnimation = false;

   mHandler.sendEmptyMessage(END_ANIMATION);

   break;

  case WifiManager.WIFI_STATE_ENABLING:

   Log.e("state", "WIFI_STATE_ENABLING");

   mRunAnimation = true;

   mHandler.sendMessage(msg);

   break;

  case WifiManager.WIFI_STATE_ENABLED:

   Log.e("state", "WIFI_STATE_ENABLED");

   mRunAnimation = false;

   mHandler.sendEmptyMessage(END_ANIMATION);

   break;

  default:

   Log.e("state", "WIFI_STATE_UNKNOW");

  }

 }

 

 private Handler mHandler = new Handler(){

 

  @Override

  public void handleMessage(Message msg) {

   super.handleMessage(msg);

   AppWidgetManager appWidgetManger = AppWidgetManager

   .getInstance(mContext);

   int[] appIds = appWidgetManger.getAppWidgetIds(new ComponentName(

           mContext, WifiWidget.class));

   if(msg.what == START_ANIMATION){

    if(!mRunAnimation){

     return ;

    }

    mRemoteViews.setImageViewResource(R.id.settings_wifi_state,mDrawable[msg.arg1]);

    appWidgetManger.updateAppWidget(appIds, mRemoteViews);

    Message message = mHandler.obtainMessage(START_ANIMATION);

    message.arg1 = (msg.arg1 + 1) % 3;

    mHandler.sendMessageDelayed(message, 200);

   }else if(msg.what == END_ANIMATION){

    wifiState = mWifiManager.getWifiState();

    mRemoteViews.setImageViewResource(R.id.settings_wifi_state,mDrawable[2]);

    if(wifiState == WifiManager.WIFI_STATE_ENABLED){

     mRemoteViews.setImageViewResource(R.id.settings_wifi_switch,R.drawable.settings_switch_on);

    }else if(wifiState == WifiManager.WIFI_STATE_DISABLED){

     mRemoteViews.setImageViewResource(R.id.settings_wifi_switch,R.drawable.settings_switch_off);

    }

    appWidgetManger.updateAppWidget(appIds, mRemoteViews);

   }

  }

 };

}

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,