zoukankan      html  css  js  c++  java
  • android中的appwidget理解巩固 java程序员

    public class MyAppWidget extends AppWidgetProvider{


    private static final String CLICK_ACTION="cxd.appwidget.BUTTON_ONCLICK";
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
    // TODO Auto-generated method stub
    System.out.println("-------------onDeleted----------------");
    super.onDeleted(context, appWidgetIds);
    }


    //当最后一个appwidget删除后
    @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

    String actionString = intent.getAction();
    System.out.println("-------------onReceive----------------"+actionString);
    if(CLICK_ACTION.equals(actionString)){
    System.out.println("===============");
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidget01);
    remoteViews.setTextViewText(R.id.text01, context.getResources().getString(R.string.hello_world));
    remoteViews.setViewVisibility(R.id.button01, View.INVISIBLE);
    Toast.makeText(context, "success", Toast.LENGTH_SHORT).show();
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    ComponentName componentName = new ComponentName(context, MyAppWidget.class);
    appWidgetManager.updateAppWidget(componentName, remoteViews);
    }else{
    super.onReceive(context, intent);
    }

    }


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

    Intent intent = new Intent();
    intent.setAction(CLICK_ACTION);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, -1, intent, 0);
    RemoteViews rViews = new RemoteViews(context.getPackageName(), R.layout.appwidget01);
    rViews.setOnClickPendingIntent(R.id.button01, pendingIntent);
    appWidgetManager.updateAppWidget(appWidgetIds, rViews);

    }



    }

    在manifest文件里面

     <receiver android:name="MyAppWidget" android:exported="false">
                <!-- 所需要满足的过滤器 -->
                <intent-filter>
                    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                </intent-filter>
                <intent-filter>
                    <action android:name="cxd.appwidget.BUTTON_ONCLICK" />
                </intent-filter>

                 <meta-data
                    android:name="android.appwidget.provider"
                    android:resource="@xml/appwidget01_info" />
            </receiver>

    学了这么久,总结点appwidget的自己理解的东西记录下来!

    执行顺序:

    首先-->在桌面创建appwidget桌面小程序,

    重点是  <intent-filter>
                    <action android:name="cxd.appwidget.BUTTON_ONCLICK" />
                </intent-filter>

    和代码里的Intent intent = new Intent();
    intent.setAction(CLICK_ACTION);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, -1, intent, 0);和String actionString = intent.getAction();

    intent.setAction(CLICK_ACTION);是将private static final String CLICK_ACTION="cxd.appwidget.BUTTON_ONCLICK";设置action来执行

    然后和manifest里的 <intent-filter>
                    <action android:name="cxd.appwidget.BUTTON_ONCLICK" />
                </intent-filter>比对,如果相匹配就为pendingIntent对象的广播事件设置这个action,所以在onReceive里面就用String actionString = intent.getAction();得到

    在manifest匹配的action对象



    还有种比较简单的使用getActivity(Context context, int requestCode, Intent intent, int flags);  创建PendingIntent对象

    其中的intent对象是通过Intent  intent = new Intent(context,XXX.class);来创建的,可以开启一个activity



  • 相关阅读:
    android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput
    android 8种对话框(Dialog)使用方法汇总
    Gradle环境变量的配置
    Activity标题(title)的显示和隐藏
    Android Studio配置Android Annotations框架详解--说说那些坑
    Android如何防止apk程序被反编译
    Android APK反编译 apktool使用教程
    关于Installation error: INSTALL_FAILED_NO_MATCHING_ABIS的解决方法
    Android解决NDK not configured问题
    Android SDK在线更新镜像服务器
  • 原文地址:https://www.cnblogs.com/java20130725/p/3215769.html
Copyright © 2011-2022 走看看