• Android笔记之AppWidget


    1、AndroidMainfeast配置文件

    <receiver android:name="MyAppWidgetProvider" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
                   android:resource="@xml/example_appwidget_info" />
    </receiver>

    xml/example_appwidget_info是widget的属性文件

    MyAppWidgetProvider是继承AppWidgetProvider的类名

    2、xml配置文件

    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
        android:minWidth="40dp"
        android:minHeight="40dp"
        android:updatePeriodMillis="86400000"
        android:previewImage="@drawable/preview"
        android:initialLayout="@layout/example_appwidget"
        android:configure="com.example.android.ExampleAppWidgetConfigure" 
        android:resizeMode="horizontal|vertical"
        android:widgetCategory="home_screen|keyguard"
        android:initialKeyguardLayout="@layout/example_keyguard">
    </appwidget-provider>

     3、layout/example_appwidget设计插件的布局

    4、MyAppWidgetProvider

    @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                int[] appWidgetIds) {
            final int N = appWidgetIds.length;
    
            // Perform this loop procedure for each App Widget that belongs to this
            // provider
            for (int i = 0; i < N; i++) {
                int appWidgetId = appWidgetIds[i];
    
                // Create an Intent to launch ExampleActivity
                Intent intent = new Intent();
                intent.setAction(action_button);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
                        intent, 0);
    
                // Get the layout for the App Widget and attach an on-click listener
                // to the button
                RemoteViews views = new RemoteViews(context.getPackageName(),
                        R.layout.appwidgetlayout);
                views.setOnClickPendingIntent(R.id.button1, pendingIntent);
    
                // Tell the AppWidgetManager to perform an update on the current app
                // widget
                appWidgetManager.updateAppWidget(appWidgetId, views);
    
            }
        }
    
    @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            String action = intent.getAction();
            if (action.equals(action_button)) {
                Log.i("onReceive", "-->"+action);
    
            }
            super.onReceive(context, intent);
        }

     5、设置监听:

    1. 监听点击显示toast:
    Intent toastIntent = new Intent(context, MyAppWidgetProvider.class);
                // Set the action for the intent.
                // When the user touches a particular view, it will have the effect of
                // broadcasting TOAST_ACTION.
    toastIntent.setAction(MyAppWidgetProvider.TOAST_ACTION);
    PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent,0);
    rv.setPendingIntent(R.id.btn, toastPendingIntent);

    在由intent生成PendingIntent时,如果intent带数据(putExtra),则第四个参数是PendingIntent.FLAG_UPDATE_CURRENT

  • 相关阅读:
    ajax网站
    iTextSharp和jQuery和.Net教程
    搜索引擎学习资料
    tooltip 效果
    ccnet+web deploy远程发布
    创建报表时出现“缺少DataSetName属性的值”
    virtualBox 虚拟机相关设置
    数据库更新数据表时被禁止
    ACL
    windows server 添加IIS
  • 原文地址:https://www.cnblogs.com/xingyyy/p/3343799.html
Copyright © 2020-2023  润新知