实现一个简单的appwidget

主要的功能有 显示时间 日期,显示考研倒计时间,显示天气,显示目标,以及一些励志话语等。

1.首先定义appwidget 的描述xml文件

在res文件下新建xml文件夹 新建kaoyan.xml文件

        
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <appwidget-providerxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:minWidth="286dp"
  4. android:minHeight="142dp"
  5. android:initialLayout="@layout/kaoyan_widget"
  6. android:updatePeriodMillis="180000"
  7. android:configure="com.kaoyan.KaoyanActivity"
  8. >
  9. </appwidget-provider>

  1. android:minWidth="286dp" //宽度
  2. android:minHeight="142dp" //高度
  3. android:initialLayout="@layout/kaoyan_widget" //布局文件
  4. android:updatePeriodMillis="180000"//刷新时间
  5. android:configure="com.kaoyan.KaoyanActivity" // 配置文件

2.建立相应的layout文件

        
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="horizontal"
  6. android:background="@drawable/beijing">
  7. <LinearLayout
  8. android:layout_width="200dp"
  9. android:layout_height="fill_parent"
  10. android:paddingLeft="35dp"
  11. android:paddingTop="35dp"
  12. android:orientation="vertical"
  13. >
  14. <TextView
  15. android:id="@+id/daojishi"
  16. android:layout_height="wrap_content"
  17. android:textColor="#ffffff"
  18. android:paddingLeft="100dp"
  19. android:layout_width="wrap_content"
  20. android:gravity="center"
  21. android:textSize="15dp"
  22. android:text="300天"
  23. />
  24. <TextView
  25. android:id="@+id/time"
  26. android:layout_height="wrap_content"
  27. android:textColor="#ffffff"
  28. android:layout_marginTop="5dp"
  29. android:layout_width="150dp"
  30. android:gravity="center"
  31. android:textSize="10dp"
  32. android:text=""
  33. />
  34. <TextView
  35. android:id="@+id/mymubiao"
  36. android:layout_width="150dp"
  37. android:layout_height="wrap_content"
  38. android:paddingTop="5dp"
  39. android:textColor="#ffffff"
  40. android:gravity="center"
  41. android:text=""
  42. />
  43. <TextView
  44. android:id="@+id/yanyu"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:paddingTop="5dp"
  48. android:textColor="#ffffff"
  49. android:text=""
  50. />
  51. <TextView
  52. android:id="@+id/tishi"
  53. android:layout_width="wrap_content"
  54. android:layout_height="wrap_content"
  55. android:paddingTop="5dp"
  56. android:textColor="#ffffff"
  57. android:text=""
  58. />
  59. </LinearLayout>
  60. <LinearLayout
  61. android:layout_width="fill_parent"
  62. android:layout_height="fill_parent"
  63. android:paddingTop="35dp"
  64. android:orientation="vertical"
  65. >
  66. <ImageView
  67. android:id="@+id/tupian"
  68. android:layout_width="45dp"
  69. android:layout_height="45dp"
  70. android:layout_marginLeft="20dp"
  71. android:src="@drawable/qingtian"
  72. />
  73. <TextView
  74. android:id="@+id/tianqi"
  75. android:layout_width="wrap_content"
  76. android:layout_height="wrap_content"
  77. android:textColor="#ffffff"
  78. android:textSize="8dp"
  79. android:text=""/>
  80. <TextView
  81. android:id="@+id/mingtian"
  82. android:layout_width="wrap_content"
  83. android:layout_height="wrap_content"
  84. android:textColor="#ffffff"
  85. android:textSize="8dp"
  86. android:text=""/>
  87. </LinearLayout>
  88. </LinearLayout>

4,建立相应的AppWidgetProvider类

        
  1. packagecom.kaoyan;
  2. importjava.io.IOException;
  3. importjava.io.InputStream;
  4. importjava.net.MalformedURLException;
  5. importjava.net.URL;
  6. importjava.net.URLConnection;
  7. importjava.text.SimpleDateFormat;
  8. importjava.util.Date;
  9. importorg.xmlpull.v1.XmlPullParserException;
  10. importandroid.app.Service;
  11. importandroid.appwidget.AppWidgetManager;
  12. importandroid.appwidget.AppWidgetProvider;
  13. importandroid.content.ComponentName;
  14. importandroid.content.Context;
  15. importandroid.content.Intent;
  16. importandroid.os.Handler;
  17. importandroid.os.IBinder;
  18. importandroid.util.Log;
  19. importandroid.widget.RemoteViews;
  20. publicclasskaoyanWidgetextendsAppWidgetProvider{
  21. @Override
  22. publicvoidonDisabled(Contextcontext){
  23. //TODOAuto-generatedmethodstub
  24. super.onDisabled(context);
  25. Log.i("kaoyanWidget","onDisabled");
  26. }
  27. @Override
  28. publicvoidonEnabled(Contextcontext){
  29. //TODOAuto-generatedmethodstub
  30. super.onEnabled(context);
  31. Log.i("kaoyanWidget","onEnabled");
  32. }
  33. @Override
  34. publicvoidonReceive(Contextcontext,Intentintent){
  35. //TODOAuto-generatedmethodstub
  36. super.onReceive(context,intent);
  37. Log.i("onReceive",intent.getAction());
  38. }
  39. @Override
  40. publicvoidonUpdate(Contextcontext,AppWidgetManagerappWidgetManager,
  41. int[]appWidgetIds){
  42. //TODOAuto-generatedmethodstub
  43. Log.i("update","开始更新");
  44. Log.i("update",appWidgetManager.getInstalledProviders().size()+"");
  45. Intentintent=newIntent(context,UpdateService.class);
  46. context.startService(intent);
  47. URLaURL;
  48. try{
  49. aURL=newURL("http://www.google.com/ig/api?hl=zh_cn&weather=beijing");
  50. URLConnectioncon=aURL.openConnection();
  51. InputStreamis=con.getInputStream();
  52. xmlParsermXml=newxmlParser();
  53. weathermweather=mXml.parser(is);
  54. RemoteViewsviews=newRemoteViews("com.kaoyan",R.layout.kaoyan_widget);
  55. views.setTextViewText(R.id.tianqi,"天气:"+mweather.getCondition()+"气温:"+mweather.getTemp_c());
  56. views.setTextViewText(R.id.mingtian,"明天:"+mweather.getmCondition()+""+mweather.getLow()+"~"+mweather.getHigh());
  57. Log.i("update","天气:"+mweather.getCondition()+"气温:"+mweather.getTemp_c());
  58. Log.i("update","明天:"+mweather.getmCondition()+""+mweather.getLow()+"~"+mweather.getHigh());
  59. appWidgetManager.updateAppWidget(appWidgetIds,views);
  60. for(inti=0;i<appWidgetIds.length;i++)
  61. Log.i("appWidgetIds",appWidgetIds[i]+"");
  62. }catch(MalformedURLExceptione){
  63. //TODOAuto-generatedcatchblock
  64. e.printStackTrace();
  65. }catch(XmlPullParserExceptione){
  66. //TODOAuto-generatedcatchblock
  67. e.printStackTrace();
  68. }catch(IOExceptione){
  69. //TODOAuto-generatedcatchblock
  70. e.printStackTrace();
  71. }
  72. super.onUpdate(context,appWidgetManager,appWidgetIds);
  73. Log.i("update","更新结束");
  74. }
  75. @Override
  76. publicvoidonDeleted(Contextcontext,int[]appWidgetIds){
  77. //TODOAuto-generatedmethodstub
  78. Intentintent=newIntent(context,UpdateService.class);
  79. context.stopService(intent);
  80. super.onDeleted(context,appWidgetIds);
  81. Log.i("delete",appWidgetIds.toString());
  82. }
  83. publicstaticclassUpdateServiceextendsService{
  84. privateHandlerobjHandler=newHandler();
  85. privateRunnablemTasks=newRunnable(){
  86. publicvoidrun(){
  87. //TODOAuto-generatedmethodstub
  88. RemoteViewsviews=newRemoteViews(UpdateService.this.getPackageName(),R.layout.kaoyan_widget);
  89. SimpleDateFormatsdf=newSimpleDateFormat("yyyy年MM月dd日hh:mm:ss");
  90. views.setTextViewText(R.id.time,""+sdf.format(newDate()));
  91. ComponentNamethisWidget=newComponentName(UpdateService.this,kaoyanWidget.class);
  92. AppWidgetManagermanager=AppWidgetManager.getInstance(UpdateService.this);
  93. manager.updateAppWidget(thisWidget,views);
  94. objHandler.postDelayed(mTasks,1000);
  95. }};
  96. @Override
  97. publicIBinderonBind(Intentarg0){
  98. //TODOAuto-generatedmethodstub
  99. returnnull;
  100. }
  101. @Override
  102. publicvoidonStart(Intentintent,intstartId){
  103. //TODOAuto-generatedmethodstub
  104. Log.i("Service","onStart");
  105. super.onStart(intent,startId);
  106. objHandler.postDelayed(mTasks,1000);
  107. }
  108. @Override
  109. publicvoidonDestroy(){
  110. //TODOAuto-generatedmethodstub
  111. super.onDestroy();
  112. Log.i("Service","onDestroy");
  113. }
  114. }
  115. }

更多相关文章

  1. NPM 和webpack 的基础使用
  2. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  3. Android之使用XMLPull解析xml(二)
  4. Mac电脑开发Android(安卓)app如何连接真机
  5. 通过xml布局文件实现按钮改变焦点设置背景图片
  6. Android实现简单字符驱动程序及测试
  7. Android(安卓)中文 API (36) ―― Toast
  8. Android(安卓)数据存储(二) 文件的使用
  9. android nfc中MifareClassic格式的读写

随机推荐

  1. [视频]Firefox for Android(安卓)预览版
  2. Android网络基础1——网络分层
  3. ym——Android酷炫实用的开源框架(UI框架)(
  4. 【Android】百度地图自定义弹出窗口
  5. Android检测Cursor泄漏的原理以及使用方
  6. android压力测试命令Monkey
  7. 苹果App被置病毒 网友:安卓无压力
  8. Android翻页效果原理实现之翻页的尝试
  9. ANDROID GridView 分页平滑滑动 效果的实
  10. Android应用程序介绍页面实现 (二)