android 考研appwidget的实现
16lz
2021-12-04
实现一个简单的appwidget
主要的功能有 显示时间 日期,显示考研倒计时间,显示天气,显示目标,以及一些励志话语等。
1.首先定义appwidget 的描述xml文件
在res文件下新建xml文件夹 新建kaoyan.xml文件
- <?xmlversion="1.0"encoding="utf-8"?>
- <appwidget-providerxmlns:android="http://schemas.android.com/apk/res/android"
- android:minWidth="286dp"
- android:minHeight="142dp"
- android:initialLayout="@layout/kaoyan_widget"
- android:updatePeriodMillis="180000"
- android:configure="com.kaoyan.KaoyanActivity"
- >
- </appwidget-provider>
- android:minWidth="286dp" //宽度
- android:minHeight="142dp" //高度
- android:initialLayout="@layout/kaoyan_widget" //布局文件
- android:updatePeriodMillis="180000"//刷新时间
- android:configure="com.kaoyan.KaoyanActivity" // 配置文件
2.建立相应的layout文件
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal"
- android:background="@drawable/beijing">
- <LinearLayout
- android:layout_width="200dp"
- android:layout_height="fill_parent"
- android:paddingLeft="35dp"
- android:paddingTop="35dp"
- android:orientation="vertical"
- >
- <TextView
- android:id="@+id/daojishi"
- android:layout_height="wrap_content"
- android:textColor="#ffffff"
- android:paddingLeft="100dp"
- android:layout_width="wrap_content"
- android:gravity="center"
- android:textSize="15dp"
- android:text="300天"
- />
- <TextView
- android:id="@+id/time"
- android:layout_height="wrap_content"
- android:textColor="#ffffff"
- android:layout_marginTop="5dp"
- android:layout_width="150dp"
- android:gravity="center"
- android:textSize="10dp"
- android:text=""
- />
- <TextView
- android:id="@+id/mymubiao"
- android:layout_width="150dp"
- android:layout_height="wrap_content"
- android:paddingTop="5dp"
- android:textColor="#ffffff"
- android:gravity="center"
- android:text=""
- />
- <TextView
- android:id="@+id/yanyu"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingTop="5dp"
- android:textColor="#ffffff"
- android:text=""
- />
- <TextView
- android:id="@+id/tishi"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingTop="5dp"
- android:textColor="#ffffff"
- android:text=""
- />
- </LinearLayout>
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:paddingTop="35dp"
- android:orientation="vertical"
- >
- <ImageView
- android:id="@+id/tupian"
- android:layout_width="45dp"
- android:layout_height="45dp"
- android:layout_marginLeft="20dp"
- android:src="@drawable/qingtian"
- />
- <TextView
- android:id="@+id/tianqi"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#ffffff"
- android:textSize="8dp"
- android:text=""/>
- <TextView
- android:id="@+id/mingtian"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#ffffff"
- android:textSize="8dp"
- android:text=""/>
- </LinearLayout>
- </LinearLayout>
4,建立相应的AppWidgetProvider类
- packagecom.kaoyan;
- importjava.io.IOException;
- importjava.io.InputStream;
- importjava.net.MalformedURLException;
- importjava.net.URL;
- importjava.net.URLConnection;
- importjava.text.SimpleDateFormat;
- importjava.util.Date;
- importorg.xmlpull.v1.XmlPullParserException;
- importandroid.app.Service;
- importandroid.appwidget.AppWidgetManager;
- importandroid.appwidget.AppWidgetProvider;
- importandroid.content.ComponentName;
- importandroid.content.Context;
- importandroid.content.Intent;
- importandroid.os.Handler;
- importandroid.os.IBinder;
- importandroid.util.Log;
- importandroid.widget.RemoteViews;
- publicclasskaoyanWidgetextendsAppWidgetProvider{
- @Override
- publicvoidonDisabled(Contextcontext){
- //TODOAuto-generatedmethodstub
- super.onDisabled(context);
- Log.i("kaoyanWidget","onDisabled");
- }
- @Override
- publicvoidonEnabled(Contextcontext){
- //TODOAuto-generatedmethodstub
- super.onEnabled(context);
- Log.i("kaoyanWidget","onEnabled");
- }
- @Override
- publicvoidonReceive(Contextcontext,Intentintent){
- //TODOAuto-generatedmethodstub
- super.onReceive(context,intent);
- Log.i("onReceive",intent.getAction());
- }
- @Override
- publicvoidonUpdate(Contextcontext,AppWidgetManagerappWidgetManager,
- int[]appWidgetIds){
- //TODOAuto-generatedmethodstub
- Log.i("update","开始更新");
- Log.i("update",appWidgetManager.getInstalledProviders().size()+"");
- Intentintent=newIntent(context,UpdateService.class);
- context.startService(intent);
- URLaURL;
- try{
- aURL=newURL("http://www.google.com/ig/api?hl=zh_cn&weather=beijing");
- URLConnectioncon=aURL.openConnection();
- InputStreamis=con.getInputStream();
- xmlParsermXml=newxmlParser();
- weathermweather=mXml.parser(is);
- RemoteViewsviews=newRemoteViews("com.kaoyan",R.layout.kaoyan_widget);
- views.setTextViewText(R.id.tianqi,"天气:"+mweather.getCondition()+"气温:"+mweather.getTemp_c());
- views.setTextViewText(R.id.mingtian,"明天:"+mweather.getmCondition()+""+mweather.getLow()+"~"+mweather.getHigh());
- Log.i("update","天气:"+mweather.getCondition()+"气温:"+mweather.getTemp_c());
- Log.i("update","明天:"+mweather.getmCondition()+""+mweather.getLow()+"~"+mweather.getHigh());
- appWidgetManager.updateAppWidget(appWidgetIds,views);
- for(inti=0;i<appWidgetIds.length;i++)
- Log.i("appWidgetIds",appWidgetIds[i]+"");
- }catch(MalformedURLExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }catch(XmlPullParserExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- super.onUpdate(context,appWidgetManager,appWidgetIds);
- Log.i("update","更新结束");
- }
- @Override
- publicvoidonDeleted(Contextcontext,int[]appWidgetIds){
- //TODOAuto-generatedmethodstub
- Intentintent=newIntent(context,UpdateService.class);
- context.stopService(intent);
- super.onDeleted(context,appWidgetIds);
- Log.i("delete",appWidgetIds.toString());
- }
- publicstaticclassUpdateServiceextendsService{
- privateHandlerobjHandler=newHandler();
- privateRunnablemTasks=newRunnable(){
- publicvoidrun(){
- //TODOAuto-generatedmethodstub
- RemoteViewsviews=newRemoteViews(UpdateService.this.getPackageName(),R.layout.kaoyan_widget);
- SimpleDateFormatsdf=newSimpleDateFormat("yyyy年MM月dd日hh:mm:ss");
- views.setTextViewText(R.id.time,""+sdf.format(newDate()));
- ComponentNamethisWidget=newComponentName(UpdateService.this,kaoyanWidget.class);
- AppWidgetManagermanager=AppWidgetManager.getInstance(UpdateService.this);
- manager.updateAppWidget(thisWidget,views);
- objHandler.postDelayed(mTasks,1000);
- }};
- @Override
- publicIBinderonBind(Intentarg0){
- //TODOAuto-generatedmethodstub
- returnnull;
- }
- @Override
- publicvoidonStart(Intentintent,intstartId){
- //TODOAuto-generatedmethodstub
- Log.i("Service","onStart");
- super.onStart(intent,startId);
- objHandler.postDelayed(mTasks,1000);
- }
- @Override
- publicvoidonDestroy(){
- //TODOAuto-generatedmethodstub
- super.onDestroy();
- Log.i("Service","onDestroy");
- }
- }
- }
更多相关文章
- NPM 和webpack 的基础使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- Android之使用XMLPull解析xml(二)
- Mac电脑开发Android(安卓)app如何连接真机
- 通过xml布局文件实现按钮改变焦点设置背景图片
- Android实现简单字符驱动程序及测试
- Android(安卓)中文 API (36) ―― Toast
- Android(安卓)数据存储(二) 文件的使用
- android nfc中MifareClassic格式的读写