Android应用源码之仿墨迹天气插件

移动开发
仿照墨迹天气的桌面小插件例子源码。

源码简介

仿照墨迹天气的桌面小插件例子源码。
源码运行截图

源码片段

  1. public class UpdateService extends Service { 
  2.     private static final int UPDATE = 0x123
  3.     private RemoteViews remoteViews; 
  4.     // 数字时间图片资源数组 
  5.     private int[] imgs = { R.drawable.n0, R.drawable.n1, R.drawable.n2, 
  6.             R.drawable.n3, R.drawable.n4, R.drawable.n5, R.drawable.n6, 
  7.             R.drawable.n7, R.drawable.n8, R.drawable.n9, }; 
  8.     // 将显示小时、分钟的ImageView定义成数组 
  9.     private int[] dateViews = { R.id.h1, R.id.h2, R.id.m1, R.id.m2 }; 
  10.     // 按照中国天气网的天气图片顺序排列好本地资源图片,我这里是随意的~嘿嘿 
  11.     private int[] weatherImg = { R.drawable.sunny, R.drawable.cloudy, 
  12.             R.drawable.chance_of_rain, R.drawable.chance_of_sleet, 
  13.             R.drawable.chance_of_snow, R.drawable.chance_of_storm, 
  14.             R.drawable.clock1, R.drawable.fog, R.drawable.haze, 
  15.             R.drawable.mist, R.drawable.mostly_sunny, R.drawable.mostly_cloudy, 
  16.             R.drawable.lower, R.drawable.middle }; 
  17.     private Handler handler = new Handler() { 
  18.   
  19.         @Override 
  20.         public void handleMessage(Message msg) { 
  21.             switch (msg.what) { 
  22.             case UPDATE: 
  23.                 // 更新天气 
  24.                 updateTime(); 
  25.                 updateWeather(); 
  26.                 break
  27.             } 
  28.         } 
  29.     }; 
  30.     // 广播接收者去接收系统每分钟的提示广播,来更新时间 
  31.     private BroadcastReceiver mTimePickerBroadcast = new BroadcastReceiver() { 
  32.   
  33.         @Override 
  34.         public void onReceive(Context context, Intent intent) { 
  35.             updateTime(); 
  36.         } 
  37.     }; 
  38.   
  39.     private void updateWeather() { 
  40.         // Weather w = new GetWeather().googleWeather(); 
  41.         // if (w != null) { 
  42.         // System.out.println("当前天气:" + w.getWeather() + ":" + w.getTemp_c() 
  43.         // + ":" + w.getIcon()); 
  44.         remoteViews.setTextViewText(R.id.condition, MyWeather.weather1); 
  45.         remoteViews.setTextViewText(R.id.tem, (MyWeather.temp1)); 
  46.         // 根据图片名,获取天气图片资源 
  47.         // remoteViews.setImageViewResource( 
  48.         // R.id.weather, 
  49.         // getApplicationContext().getResources().getIdentifier( 
  50.         // w.getIcon(), "drawable", "com.way.apptest")); 
  51.         if (MyWeather.img1 != null || !"".equals(MyWeather.img1)) 
  52.             remoteViews.setImageViewResource(R.id.weather, 
  53.                     weatherImg[Integer.parseInt(MyWeather.img1)]); 
  54.         // 执行更新 
  55.         ComponentName componentName = new ComponentName( 
  56.                 getApplicationContext(), App.class); 
  57.         AppWidgetManager.getInstance(getApplicationContext()).updateAppWidget( 
  58.                 componentName, remoteViews); 
  59.     } 
  60.   
  61.     @Override 
  62.     public IBinder onBind(Intent intent) { 
  63.         return null
  64.     } 
  65.   
  66.     @Override 
  67.     public void onCreate() { 
  68.         super.onCreate(); 
  69.         remoteViews = new RemoteViews(getApplication().getPackageName(), 
  70.                 R.layout.main);// 实例化RemoteViews 
  71.         if (isNetworkAvailable()) { 
  72.             MyWeather.getWeather();// json解析中国天气网天气 
  73.         } else { 
  74.             toast(); 
  75.         } 
  76.         updateTime();// ***次运行时先更新一下时间和天气 
  77.         updateWeather(); 
  78.         // 点击天气图片,进入MainActivity 
  79.         Intent intent = new Intent(getApplicationContext(), MainActivity.class); 
  80.         PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 
  81.                 0, intent, 0); 
  82.         remoteViews.setOnClickPendingIntent(R.id.weather, pi); 
  83.   
  84.         // 定义一个定时器去更新天气。实际开发中更新时间间隔可以由用户设置, 
  85.         new Timer().scheduleAtFixedRate(new TimerTask() { 
  86.             @Override 
  87.             public void run() { 
  88.                 Message msg = handler.obtainMessage(); 
  89.                 msg.what = UPDATE; 
  90.                 handler.sendMessage(msg); 
  91.             } 
  92.         }, 13600 * 1000);// 每小时更新一次天气 
  93.     } 
  94.   
  95.     private void updateTime() { 
  96.         Date date = new Date(); 
  97.         // 定义SimpleDateFormat对象 
  98.         SimpleDateFormat df = new SimpleDateFormat("HHmm"); 
  99.         // 将当前时间格式化成HHmm的形式 
  100.         String timeStr = df.format(date); 
  101.   
  102.         for (int i = 0; i < timeStr.length(); i++) { 
  103.             // 将第i个数字字符转换为对应的数字 
  104.             int num2 = Integer.parseInt(timeStr.substring(i, i + 1)); 
  105.             // 将第i个图片的设为对应的数字图片 
  106.             remoteViews.setImageViewResource(dateViews[i], imgs[num2]); 
  107.         } 
  108.         remoteViews.setTextViewText(R.id.city, MyWeather.city); 
  109.         remoteViews.setTextViewText(R.id.date, "0" + (date.getMonth() + 1
  110.                 + "-" + date.getDate() + " 周" + date.getDay()); 
  111.         ComponentName componentName = new ComponentName(getApplication(), 
  112.                 App.class); 
  113.         AppWidgetManager.getInstance(getApplication()).updateAppWidget( 
  114.                 componentName, remoteViews); 
  115.     } 
  116.   
  117.     @Override 
  118.     public void onStart(Intent intent, int startId) { 
  119.         // 注册系统每分钟提醒广播(注意:这个广播只能在代码中注册) 
  120.         IntentFilter updateIntent = new IntentFilter(); 
  121.         updateIntent.addAction("android.intent.action.TIME_TICK"); 
  122.         registerReceiver(mTimePickerBroadcast, updateIntent); 
  123.         super.onStart(intent, startId); 
  124.     } 
  125.   
  126.     @Override 
  127.     public void onDestroy() { 
  128.         // 注销系统的这个广播 
  129.         unregisterReceiver(mTimePickerBroadcast); 
  130.         //被系统干掉后,服务重启,做一次流氓软件,哈哈 
  131.         Intent intent = new Intent(getApplicationContext(), UpdateService.class); 
  132.         getApplication().startService(intent); 
  133.         super.onDestroy(); 
  134.     } 
  135.   
  136.     /** 
  137.      * 判断手机网络是否可用 
  138.      * 
  139.      * @param context 
  140.      * @return 
  141.      */ 
  142.     private boolean isNetworkAvailable() { 
  143.         ConnectivityManager mgr = (ConnectivityManager) getApplicationContext() 
  144.                 .getSystemService(Context.CONNECTIVITY_SERVICE); 
  145.         NetworkInfo[] info = mgr.getAllNetworkInfo(); 
  146.         if (info != null) { 
  147.             for (int i = 0; i < info.length; i++) { 
  148.                 if (info[i].getState() == NetworkInfo.State.CONNECTED) { 
  149.                     return true
  150.                 } 
  151.             } 
  152.         } 
  153.         return false
  154.     } 
  155.   
  156.     private void toast() { 
  157.         new AlertDialog.Builder(getApplicationContext()) 
  158.                 .setTitle("提示"
  159.                 .setMessage("网络连接未打开"
  160.                 .setPositiveButton("前往打开"
  161.                         new DialogInterface.OnClickListener() { 
  162.   
  163.                             public void onClick(DialogInterface dialog, 
  164.                                     int which) { 
  165.                                 Intent intent = new Intent( 
  166.                                         android.provider.Settings.ACTION_WIRELESS_SETTINGS); 
  167.                                 startActivity(intent); 
  168.                             } 
  169.                         }).setNegativeButton("取消"null).create().show(); 
  170.     } 

源码链接:http://down.51cto.com/data/1985018

责任编辑:chenqingxiang 来源: 网络整理
相关推荐

2015-10-12 11:19:35

墨迹天气引导界面android

2013-08-23 17:06:09

墨迹天气Android 2.4

2015-03-30 14:24:06

网易布局

2013-09-27 16:26:25

墨迹天气

2017-05-12 14:55:39

Android绘制刻度盘

2015-10-23 13:36:22

2014-05-12 09:38:38

墨迹天气智能硬件空气检测

2015-02-27 16:35:13

智能农业Android界面

2014-11-21 12:57:55

墨迹天气

2017-08-07 14:24:16

互联网

2017-07-06 15:26:48

互联网

2015-02-03 14:45:55

android全局异常

2013-08-23 17:23:01

墨迹天气可口可乐

2015-02-11 17:49:35

Android源码自定义控件

2016-03-14 10:29:38

天气预报各类工具源码

2015-02-06 18:29:09

仿滴滴打车android

2015-01-22 15:44:55

Android源码音乐播放器

2015-10-20 16:01:47

华为商城源码android
点赞
收藏

51CTO技术栈公众号