zoukankan      html  css  js  c++  java
  • AppWidget的范例

    首先在res文件夹下新建一目录,命名为xml。然后在这个目录下新建一个xml文件(如下图):image

    example_appwidget.xml的内容如下:

    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
        android:initialLayout="@layout/appwidget_layout"
        android:minHeight="72dp"
        android:minWidth="294dp"
        android:updatePeriodMillis="86400000" />
    然后创建一个类:ExampleAppWidget。ExampleAppWidget.java如下:
    package com.kevin.appwidget.test1;
     
    import android.app.PendingIntent;
    import android.appwidget.AppWidgetManager;
    import android.appwidget.AppWidgetProvider;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    import android.widget.RemoteViews;
     
    public class ExampleAppWidget extends AppWidgetProvider {
     
        @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                int[] appWidgetIds) {
            super.onUpdate(context, appWidgetManager, appWidgetIds);
            Log.v("tag", "------------------------>onUpdate()");
     
            Intent intent = new Intent(context, AppWidget01Activity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                    intent, 0);
            RemoteViews view = new RemoteViews(context.getPackageName(),
                    R.layout.appwidget_layout);
            view.setOnClickPendingIntent(R.id.btnStartActivity, pendingIntent);
            appWidgetManager.updateAppWidget(appWidgetIds, view);
        }
     
        @Override
        public void onDeleted(Context context, int[] appWidgetIds) {
            super.onDeleted(context, appWidgetIds);
            Log.v("tag", "------------------------>onDeleted()");
        }
     
        @Override
        public void onEnabled(Context context) {
            super.onEnabled(context);
            Log.v("tag", "------------------------>onEnabled()");
        }
     
        @Override
        public void onDisabled(Context context) {
            super.onDisabled(context);
            Log.v("tag", "------------------------>onDisabled()");
        }
     
    }
     
    最后配置Manifest.xml文件。如下:
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.kevin.appwidget.test1"
        android:versionCode="1"
        android:versionName="1.0" >
     
        <uses-sdk android:minSdkVersion="8" />
     
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:label="@string/app_name"
                android:name=".AppWidget01Activity" >
                <intent-filter >
                    <action android:name="android.intent.action.MAIN" />
     
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
     
            <receiver android:name=".ExampleAppWidget" >
                <intent-filter >
                    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                </intent-filter>
     
                <meta-data
                    android:name="android.appwidget.provider"
                    android:resource="@xml/example_appwidget" />
            </receiver>
        </application>
     
    </manifest>

    这样就可以看到appwidget的效果了。

  • 相关阅读:
    kbmMW 5.07.00试用笔记
    在 Andriod/IOS 程序中使用自定义字体
    【转】Delphi 10.3关于相机该注意的细节
    Delphi 10.3终于来了
    REST easy with kbmMW #17 – Database 6 – Existing databases
    【go】1环境搭建go语言介绍
    【ESSD技术解读02】企业级利器,阿里云 NVMe 盘和共享存储
    项目实战总结以及接入UAPM
    RocketMQ 5.0 POP 消费模式探秘
    Cube 技术解读 | 详解「支付宝」全新的卡片技术栈
  • 原文地址:https://www.cnblogs.com/yangzhenyu/p/2271281.html
Copyright © 2011-2022 走看看