Android之高仿墨迹天气桌面组件(AppWidgetProvider)
相信墨迹天气,大家都见过,他在时间显示和天气界面上,很吸引人,今天我就来模仿一下墨迹天气的桌面组件,但是由于谷歌在天朝频频被墙的缘故,所以我在今天测试的时候,解析xml文件的网页打不开,所以天气显示出了点问题,希望大家能理解,谢谢。(今天9月24日修改为解析中国天气网获取天气了,而且修改组件在桌面居中显示)。
老规矩,先分享源代码:http://download.csdn.net/detail/weidi1989/4597809
好了,废话不多说,先上效果图:
再来看一下整个小项目的主体结构:
首先先声明一个桌面布局的xml文件,即app.xml:
<?xml version="1.0" encoding="utf-8"?> <!-- 指定该桌面组件的基本配置信息: initialLayout:初始时显示的布局 minWidth:桌面小控件的最小高度。 minWidth:桌面小控件的最小宽度。 updatePeriodMillis:更新频率 --> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialLayout="@layout/main" android:minHeight="150dp" android:minWidth="200dp" android:updatePeriodMillis="0" > </appwidget-provider>
然后要在manifest文件中声明:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.way.apptest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.way.apptest.MainActivity" > </activity> <receiver android:name="com.way.apptest.App" > <!-- 指定桌面小控件的meta-data --> <meta-data android:name="android.appwidget.provider" android:resource="@xml/app" /> <!-- 将该BroadcastReceiver当成桌面小控件 --> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> </receiver> <service android:name=".UpdateService" > </service> </application> </manifest>
主要代码:
一:定义一个App类继承AppWidgetProvider,然后再onUpdate方法中启动一个服务去更新时间和天气。
/** * @author way */ public class App extends AppWidgetProvider { private Intent intent; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { intent = new Intent(context, UpdateService.class); context.startService(intent); super.onUpdate(context, appWidgetManager, appWidgetIds); } @Override public void onDeleted(Context context, int[] appWidgetIds) { context.stopService(intent); super.onDeleted(context, appWidgetIds); } }
二:本项目中最重要的部分,在这个服务中,我们注册一个广播接收者去接受系统每分钟时间时间变化的广播,从而来更新桌面时间,这样更省电哦,需要注意的是:这个广播接收者必须在代码中注册,在Manifest文件中注册是没有效果的。更新天气,我是通过定义一个定时器,由用户设置时间间隔来更新天气信息。
package com.way.apptest; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import android.app.AlertDialog; import android.app.PendingIntent; import android.app.Service; import android.appwidget.AppWidgetManager; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.widget.RemoteViews; import com.way.getWeather.MyWeather; /** * @author way */ public class UpdateService extends Service { private static final int UPDATE = 0x123; private RemoteViews remoteViews; // 数字时间图片资源数组 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定义成数组 private int[] dateViews = { R.id.h1, R.id.h2, R.id.m1, R.id.m2 }; // 按照中国天气网的天气图片顺序排列好本地资源图片,我这里是随意的~嘿嘿 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; } } }; // 广播接收者去接收系统每分钟的提示广播,来更新时间 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)); // 根据图片名,获取天气图片资源 // 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)]); // 执行更新 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解析中国天气网天气 } 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); // 定义一个定时器去更新天气。实际开发中更新时间间隔可以由用户设置, 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个数字字符转换为对应的数字 int num2 = Integer.parseInt(timeStr.substring(i, i + 1)); // 将第i个图片的设为对应的数字图片 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) { // 注册系统每分钟提醒广播(注意:这个广播只能在代码中注册) IntentFilter updateIntent = new IntentFilter(); updateIntent.addAction("android.intent.action.TIME_TICK"); registerReceiver(mTimePickerBroadcast, updateIntent); super.onStart(intent, startId); } @Override public void onDestroy() { // 注销系统的这个广播 unregisterReceiver(mTimePickerBroadcast); //被系统干掉后,服务重启,做一次流氓软件,哈哈 Intent intent = new Intent(getApplicationContext(), UpdateService.class); getApplication().startService(intent); super.onDestroy(); } /** * 判断手机网络是否可用 * * @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("网络连接未打开") .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(); } }
三:这是桌面组件主要布局文件,如果大家有什么好的建议,欢迎提,本人对布局不是特别擅长。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/bg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/ww_bg" android:orientation="vertical" > <LinearLayout android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/wd_bg" android:orientation="horizontal" > <ImageView android:id="@+id/h1" android:layout_width="wrap_content" android:layout_height="fill_parent" android:paddingLeft="10dip" android:src="@drawable/n0" /> <ImageView android:id="@+id/h2" android:layout_width="wrap_content" android:layout_height="fill_parent" android:src="@drawable/n0" /> </LinearLayout> <TextView android:layout_width="30dip" android:layout_height="wrap_content" /> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/wd_bg" android:orientation="horizontal" > <ImageView android:id="@+id/m1" android:layout_width="wrap_content" android:layout_height="match_parent" android:paddingLeft="10dip" android:src="@drawable/n0" /> <ImageView android:id="@+id/m2" android:layout_width="wrap_content" android:layout_height="match_parent" android:src="@drawable/n0" /> </LinearLayout> </LinearLayout> <LinearLayout android:id="@+id/linearLayout3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center" android:orientation="horizontal" > <LinearLayout android:layout_width="100dip" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="10dip" > <TextView android:id="@+id/city" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="20sp" /> <TextView android:id="@+id/condition" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/neterror" android:textColor="#000000" android:textSize="18sp" /> </LinearLayout> <ImageView android:id="@+id/weather" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|top" android:src="@drawable/sunny" /> <LinearLayout android:layout_width="100dip" android:layout_height="wrap_content" android:gravity="right" android:orientation="vertical" android:paddingRight="10dip" > <TextView android:id="@+id/date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="18sp" /> <TextView android:id="@+id/tem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/neterror" android:textColor="#000000" android:textSize="18sp" /> </LinearLayout> </LinearLayout> </LinearLayout>
四:谷歌天气不给力,所以我就简单的给出获取天气的核心代码。这里简单的做了一下乱码处理。
* @author way * */ public class GetWeather { /** * * @return 天气对象 */ public Weather googleWeather() { String str = "http://www.google.com/ig/api?hl=zh_cn&weather=shenzhen"; try { URL url = new URL(str); InputStream in = url.openStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len = -1; while ((len = in.read()) != -1) { bos.write(len); } InputStream is = new ByteArrayInputStream(bos.toString("GBK") .getBytes("utf-8")); // 从流中获取文档到本地内存 Document doc = DocumentBuilderFactory.newInstance() .newDocumentBuilder().parse(is); // 从文档中得到名字为current_conditions的第一个节点下的所有子节点(一个集合) NodeList nodeList = doc.getElementsByTagName("current_conditions") .item(0).getChildNodes(); // 得到nodeList下第一个节点的第一个元素内容(即当前天气) String condition = nodeList.item(0).getAttributes().item(0) .getNodeValue(); // 当前温度 String temp_c = nodeList.item(2).getAttributes().item(0) .getNodeValue(); // 当前湿度 // String humidity = nodeList.item(3).getAttributes().item(0) // .getNodeValue(); // 当前图片路径 String iconPath = nodeList.item(4).getAttributes().item(0) .getNodeValue(); // 当前风向 // String wind_condition = nodeList.item(5).getAttributes().item(0) // .getNodeValue(); Weather w = new Weather(); w.setWeather(condition); w.setTemp_c(temp_c); // 从图片路径中获取图片的名字 String icon = iconPath.substring(iconPath.lastIndexOf("/") + 1, iconPath.indexOf(".")); w.setIcon(icon); return w; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } return null; } }
今天再修改解析中国天气网的json代码:
public class MyWeather { public static String city; public static String temp1; public static String weather1; public static String img1; public static void getWeather() { try { URL url = new URL("http://m.weather.com.cn/data/101250101.html"); InputStream is = url.openStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len = -1; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) != -1) { bos.write(buffer, 0, len); } String info = bos.toString("utf-8"); JSONObject dataJson = new JSONObject(info); JSONObject json = dataJson.getJSONObject("weatherinfo"); city = json.getString("city"); temp1 = json.getString("temp1"); weather1 = json.getString("weather1"); img1 = json.getString("img1"); System.out.println(city);is.close();bos.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } } }
好了,大功告成,这是发表第三遍了,前两次都是悲剧,哎,不知道是网络问题还是我电脑问题,提交没有反应。总之很蛋疼!
相关推荐
Android 桌面组件,又称为 App Widget,是 Android 系统中一种独特且实用的功能。它允许开发者创建小型的应用程序界面,用户可以直接在主屏幕上进行交互,无需打开完整的应用程序。App Widget 提供了一种快捷的方式...
这个最简实例展示了创建一个基本Android桌面小组件的基本流程,包括定义`AppWidgetProvider`、配置`app_widget_info`、实现`onUpdate()`方法以及设计小组件布局。尽管简单,但足以理解AppWidget的工作原理和核心组件...
在Android开发中,桌面组件(Widget)是一种可以让用户在手机或平板电脑的主屏幕上直接查看和操作的应用小部件。本文将深入探讨如何在Android中创建和使用Widget,以及相关的框架类和方法。 首先,我们来看App...
下面,我们将深入探讨“Android桌面小组件”的概念、实现方式以及与之相关的知识点。 1. **AppWidget的基本概念** - **定义**:AppWidget是Android系统提供的一种服务,允许开发者创建可以在用户主屏幕上展示和...
在Android系统中,桌面...通过这个入门项目,你可以学习到如何创建一个基础的桌面小部件,并逐步深入理解Android系统的广播机制和组件交互。继续探索和实践,你将能够创建更复杂、功能丰富的桌面小部件,提升用户体验。
Android桌面Widget是Android应用组件之一,它类似于一个小应用程序,可以被放置在用户的主屏幕上,为用户提供快捷的操作或信息展示。它们通过AppWidgetProvider类与系统交互,并使用RemoteViews来更新其内容。 创建...
在Android平台上,AppWidgetProvider是实现桌面小工具(AppWidget)功能的核心类。AppWidget是可以在用户主屏幕上显示的小部件,通常提供快捷方式或者简单的信息展示功能,无需打开应用程序即可与之交互。本教程将...
Android桌面小组件是Android操作系统中一个独特且实用的功能,它允许用户将小应用程序或功能直接放在手机或平板电脑的主屏幕上,以便快速访问和交互。这些小组件通常不具有独立的用户界面,而是从主应用中提取出来的...
在Android平台上,AppWidgetProvider是实现桌面小部件(App Widget)功能的核心类。App Widgets是Android系统中一种轻量级的应用程序组件,用户可以将它们添加到主屏幕上,以提供快速访问或控制应用的功能,无需打开...
在Android平台上,开发者可以利用Android桌面小部件(Widget)功能为用户提供便捷的桌面服务,比如“Android桌面小部件widget日签Or日历可作为桌面日历”就是一个很好的例子。这个应用设计巧妙,不仅展示了日期,还...
接着,我们需要创建一个自定义的AppWidgetProvider,这个组件会在Android主屏幕上显示我们的桌面宠物。 在AppWidgetProvider的配置文件(XML)中,定义桌面宠物的布局,包括UnityPlayer视图。在代码中,我们需要...
本资料包"Android应用源码之android Widget小组件开发.zip"提供了一套完整的源代码示例,帮助开发者深入理解和实践Android Widget的创建过程。以下是对这个主题的详细讲解: 1. **Android Widget概述** Android ...
1. **Android插件**:Android插件系统允许应用程序扩展其功能,比如天气小部件就是一个桌面插件,它能够在主屏幕上显示天气信息,用户无需打开应用即可查看。Android插件通过AppWidgetProvider类实现,这是一个...
本文实例讲述了Android编程之桌面小部件AppWidgetProvider用法。分享给大家供大家参考,具体如下: /** * 桌面小部件 AppWidget配置 * * @description: * @author ldm * @date 2016-5-16 下午1:57:16 */ ...
在Android系统中,桌面小组件(AppWidget)是一种允许用户在主屏幕上与应用程序进行交互的UI元素。它们提供了一种快捷的方式,使用户无需打开应用就能获取信息或执行某些操作。"Android桌面小组件(AppWidget)"这个...
Android widget 桌面组件开发 Widget是Android1.5版所引进的特性之一.Widget,可让用户在主屏幕界面及时了解程序显示的重要信息.标准的Android系统已包含几个Widget的示例,如模拟时钟,音乐播放器等. 一、AppWidget ...
在Android开发中,`AppWidgetProvider`是一种特殊的BroadcastReceiver,用于与桌面小部件(AppWidget)交互。这个组件可以响应用户的交互,例如点击事件,并更新小部件显示的内容。在这个特定的场景中,开发者使用`...
1. **Widget组件**:Widget是Android系统提供的一种轻量级UI元素,如时钟、天气预报等,可以直接在主屏幕上展示,无需打开应用即可使用。 2. **AppWidgetProvider**:这是Widget的核心组件,继承自...
在Android平台上,Widget小组件是应用在主屏幕上提供快速访问或简单交互的一种方式。它们可以显示实时信息,比如天气、时钟或者新闻摘要,而无需用户打开完整的应用程序。本资料包"androidWidget小组件开发.zip"包含...