• App Widget Provider 应用


    /*

     * 实现AppWidgetProvider  *

      * onUpdate 在到达指定的更新时间之后或当用户添加app widget时调用该方法 *

     * onEnabled 当App Widget 第一个实例被创建时  *

      * onDeleted  当App Widget 实例被删除时  *

     * onDisabled  当App Widget 最后一个实例被删除时  *

     * onReceive  接受广播  *  * */

     注册信息

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package
    ="my.com.appwidget"
    android:versionCode
    ="1"
    android:versionName
    ="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
    android:icon="@drawable/ic_launcher"
    android:label
    ="@string/app_name" >
    <activity
    android:label="@string/app_name"
    android:name
    =".Activity01" >
    <intent-filter >
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
    <receiver
    android:name=".appwidgetprovider">
    <intent-filter>
    <action
    android:name="android.appwidget.action.APPWIDGET_UPDATE">
    </action>
    </intent-filter>
    <meta-data
    android:resource="@xml/appwidget_info"
    android:name
    ="android.appwidget.provider">
    </meta-data>
    </receiver>>
    </application>

    </manifest>

    appwidget_info.xml   在res/xml中    定义AppWidgetProviderInfo 

    <?xml version="1.0" encoding="utf-8"?>
    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth
    ="72dip"
    android:minHeight
    ="72dip"
    android:updatePeriodMillis
    ="86400000"
    android:initialLayout
    ="@layout/example_appwidget"

    />

    桌面上Widget布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="fill_parent"
    android:orientation
    ="vertical" >

    <Button
    android:layout_width="fill_parent"
    android:layout_height
    ="wrap_content"
    android:text
    ="测试按钮"
            android:id="@+id/buton01"/>
    </LinearLayout>
    <!-- 桌面上显示布局 -->
    
    

     appwidgetprovider.xml

    package my.com.appwidget;

    import android.app.PendingIntent;
    import android.appwidget.AppWidgetManager;
    import android.appwidget.AppWidgetProvider;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.RemoteViews;

    /*
    * 实现AppWidgetProvider
    *
    * onUpdate 在到达指定的更新时间之后或当用户添加app widget时调用该方法
    *
    * onEnabled 当App Widget 第一个实例被创建时
    *
    * onDeleted 当App Widget 实例被删除时
    *
    * onDisabled 当App Widget 最后一个实例被删除时
    *
    * onReceive 接受广播
    *
    *
    */
    public class appwidgetprovider extends AppWidgetProvider {

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
    // TODO Auto-generated method stub
    System.out.println("-------onDeleted");
    super.onDeleted(context, appWidgetIds);
    }

    @Override
    public void onDisabled(Context context) {
    // TODO Auto-generated method stub
    System.out.println("-------onDisabled");
    super.onDisabled(context);
    }

    @Override
    public void onEnabled(Context context) {
    // TODO Auto-generated method stub
    System.out.println("-------onEnabled");
    super.onEnabled(context);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    System.out.println("-------onReceive");
    super.onReceive(context, intent);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
    int[] appWidgetIds) {
    // TODO Auto-generated method stub
    System.out.println("-------onUpdate");

    super.onUpdate(context, appWidgetManager, appWidgetIds);

    final int N = appWidgetIds.length;
    for (int i = 0; i < N; i++)
    {
    int appWidgetId = appWidgetIds[i];
    //创建一个Intent
    Intent configIntent = new Intent(context, Activity01.class);
    //创建一个PendingIntent对象
    PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0,
    configIntent, 0);
    // 获取远程桌面的布局文件
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
    R.layout.example_appwidget);
    //为按钮绑定事件处理器
    //第一个参数用来指定被绑定处理器的空间的ID
    remoteViews.setOnClickPendingIntent(R.id.buton01, configPendingIntent);

    appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }
    }

    }

    Activity01.java

    package my.com.appwidget;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.Button;

    public class Activity01 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.example_appwidget);
    Button m_button=(Button)findViewById(R.id.buton01);
    }
    }






  • 相关阅读:
    Codeforce 1335C
    CodeForces
    Codeforce:208A. Dubstep (字符串处理,正则表达式)
    HDU
    CodeForces -337A:Puzzles(水)
    Codeforce 318A
    从零教你打造免费、稳定、高效的图床
    docker-compose mysql和node连接认证mongo问题
    awardRotate转盘插件文字模糊问题和图片加载问题
    webpack从单页面到多页面
  • 原文地址:https://www.cnblogs.com/shanshan520/p/2389125.html
Copyright © 2020-2023  润新知