Android應用源碼之仿墨跡天氣插件
作者:佚名
仿照墨跡天氣的桌面小插件例子源碼。
源碼簡介
仿照墨跡天氣的桌面小插件例子源碼。
源碼運行截圖
源碼片段
- public class UpdateService extends Service {
- private static final int UPDATE = 0x123;
- private RemoteViews remoteViews;
- // 數(shù)字時間圖片資源數(shù)組
- private int[] imgs = { R.drawable.n0, R.drawable.n1, R.drawable.n2,
- R.drawable.n3, R.drawable.n4, R.drawable.n5, R.drawable.n6,
- R.drawable.n7, R.drawable.n8, R.drawable.n9, };
- // 將顯示小時、分鐘的ImageView定義成數(shù)組
- private int[] dateViews = { R.id.h1, R.id.h2, R.id.m1, R.id.m2 };
- // 按照中國天氣網(wǎng)的天氣圖片順序排列好本地資源圖片,我這里是隨意的~嘿嘿
- private int[] weatherImg = { R.drawable.sunny, R.drawable.cloudy,
- R.drawable.chance_of_rain, R.drawable.chance_of_sleet,
- R.drawable.chance_of_snow, R.drawable.chance_of_storm,
- R.drawable.clock1, R.drawable.fog, R.drawable.haze,
- R.drawable.mist, R.drawable.mostly_sunny, R.drawable.mostly_cloudy,
- R.drawable.lower, R.drawable.middle };
- private Handler handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case UPDATE:
- // 更新天氣
- updateTime();
- updateWeather();
- break;
- }
- }
- };
- // 廣播接收者去接收系統(tǒng)每分鐘的提示廣播,來更新時間
- private BroadcastReceiver mTimePickerBroadcast = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- updateTime();
- }
- };
- private void updateWeather() {
- // Weather w = new GetWeather().googleWeather();
- // if (w != null) {
- // System.out.println("當前天氣:" + w.getWeather() + ":" + w.getTemp_c()
- // + ":" + w.getIcon());
- remoteViews.setTextViewText(R.id.condition, MyWeather.weather1);
- remoteViews.setTextViewText(R.id.tem, (MyWeather.temp1));
- // 根據(jù)圖片名,獲取天氣圖片資源
- // remoteViews.setImageViewResource(
- // R.id.weather,
- // getApplicationContext().getResources().getIdentifier(
- // w.getIcon(), "drawable", "com.way.apptest"));
- if (MyWeather.img1 != null || !"".equals(MyWeather.img1))
- remoteViews.setImageViewResource(R.id.weather,
- weatherImg[Integer.parseInt(MyWeather.img1)]);
- // 執(zhí)行更新
- ComponentName componentName = new ComponentName(
- getApplicationContext(), App.class);
- AppWidgetManager.getInstance(getApplicationContext()).updateAppWidget(
- componentName, remoteViews);
- }
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onCreate() {
- super.onCreate();
- remoteViews = new RemoteViews(getApplication().getPackageName(),
- R.layout.main);// 實例化RemoteViews
- if (isNetworkAvailable()) {
- MyWeather.getWeather();// json解析中國天氣網(wǎng)天氣
- } else {
- toast();
- }
- updateTime();// ***次運行時先更新一下時間和天氣
- updateWeather();
- // 點擊天氣圖片,進入MainActivity
- Intent intent = new Intent(getApplicationContext(), MainActivity.class);
- PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),
- 0, intent, 0);
- remoteViews.setOnClickPendingIntent(R.id.weather, pi);
- // 定義一個定時器去更新天氣。實際開發(fā)中更新時間間隔可以由用戶設置,
- new Timer().scheduleAtFixedRate(new TimerTask() {
- @Override
- public void run() {
- Message msg = handler.obtainMessage();
- msg.what = UPDATE;
- handler.sendMessage(msg);
- }
- }, 1, 3600 * 1000);// 每小時更新一次天氣
- }
- private void updateTime() {
- Date date = new Date();
- // 定義SimpleDateFormat對象
- SimpleDateFormat df = new SimpleDateFormat("HHmm");
- // 將當前時間格式化成HHmm的形式
- String timeStr = df.format(date);
- for (int i = 0; i < timeStr.length(); i++) {
- // 將第i個數(shù)字字符轉換為對應的數(shù)字
- int num2 = Integer.parseInt(timeStr.substring(i, i + 1));
- // 將第i個圖片的設為對應的數(shù)字圖片
- remoteViews.setImageViewResource(dateViews[i], imgs[num2]);
- }
- remoteViews.setTextViewText(R.id.city, MyWeather.city);
- remoteViews.setTextViewText(R.id.date, "0" + (date.getMonth() + 1)
- + "-" + date.getDate() + " 周" + date.getDay());
- ComponentName componentName = new ComponentName(getApplication(),
- App.class);
- AppWidgetManager.getInstance(getApplication()).updateAppWidget(
- componentName, remoteViews);
- }
- @Override
- public void onStart(Intent intent, int startId) {
- // 注冊系統(tǒng)每分鐘提醒廣播(注意:這個廣播只能在代碼中注冊)
- IntentFilter updateIntent = new IntentFilter();
- updateIntent.addAction("android.intent.action.TIME_TICK");
- registerReceiver(mTimePickerBroadcast, updateIntent);
- super.onStart(intent, startId);
- }
- @Override
- public void onDestroy() {
- // 注銷系統(tǒng)的這個廣播
- unregisterReceiver(mTimePickerBroadcast);
- //被系統(tǒng)干掉后,服務重啟,做一次流氓軟件,哈哈
- Intent intent = new Intent(getApplicationContext(), UpdateService.class);
- getApplication().startService(intent);
- super.onDestroy();
- }
- /**
- * 判斷手機網(wǎng)絡是否可用
- *
- * @param context
- * @return
- */
- private boolean isNetworkAvailable() {
- ConnectivityManager mgr = (ConnectivityManager) getApplicationContext()
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo[] info = mgr.getAllNetworkInfo();
- if (info != null) {
- for (int i = 0; i < info.length; i++) {
- if (info[i].getState() == NetworkInfo.State.CONNECTED) {
- return true;
- }
- }
- }
- return false;
- }
- private void toast() {
- new AlertDialog.Builder(getApplicationContext())
- .setTitle("提示")
- .setMessage("網(wǎng)絡連接未打開")
- .setPositiveButton("前往打開",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int which) {
- Intent intent = new Intent(
- android.provider.Settings.ACTION_WIRELESS_SETTINGS);
- startActivity(intent);
- }
- }).setNegativeButton("取消", null).create().show();
- }
源碼鏈接:http://down.51cto.com/data/1985018
責任編輯:chenqingxiang
來源:
網(wǎng)絡整理