Android AppWidgetProvider学习

下面介绍下AppWidgetProvider简单的实现过程(我是在AS中创建了一个空的工程,没有Activity)

1.AndroidMainfest文件如下

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >

        <receiver android:name=".MyAppWidgetProvider" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="com.skywang.widget.UPDATE_ALL"/>
            </intent-filter>

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

创建了类 MyAppWidgetProvider ,还要创建一个layout文件和xml文件

2.layout文件(文件名称为my_appwidget.xml)

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <TextView
            android:id="@+id/my_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="widget" />

        <Button
            android:id="@+id/my_btm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

</LinearLayout>

一个按钮和一个文本

3.创建xml目录及my_appwidget_info.xml

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="30dp"
    android:minHeight="30dp"
    android:previewImage="@drawable/preview"
    android:initialLayout="@layout/my_appwidget">
</appwidget-provider>

4.实现MyAppWidgetProvider类

public class MyAppWidgetProvider extends AppWidgetProvider {

    private static final String TAG = "MyAppWidgetProvider";

    private static boolean n = false;
    private  RemoteViews remoteView = null;
    private static final int BUTTON_SHOW = 1;

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        Log.e(TAG,"xtk onReceive");
        remoteView = new RemoteViews(context.getPackageName(), R.layout.my_appwidget);

        if(n) {
            remoteView.setTextViewText(R.id.my_text, "hahahha");
            n = false;
        }else{
            remoteView.setTextViewText(R.id.my_text, "henhenhen");
            n = true;
        }

        //需要实例化一个pending
        remoteView.setOnClickPendingIntent(R.id.my_btm, getPendingIntent(context, BUTTON_SHOW));
    }

    private PendingIntent getPendingIntent(Context context, int buttonId) {
        Intent intent = new Intent();
        intent.setClass(context, MyAppWidgetProvider.class);
        intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
        intent.setData(Uri.parse("custom:" + buttonId));
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0 );
        return pi;
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Log.e(TAG,"xtk onUpdate");
        super.onUpdate(context, appWidgetManager, appWidgetIds);

        remoteView = new RemoteViews(context.getPackageName(), R.layout.my_appwidget);
        appWidgetManager.updateAppWidget(appWidgetIds, remoteView);

    }
}

设置小部件信息

remoteView = new RemoteViews(context.getPackageName(), R.layout.my_appwidget);
remoteView.setTextViewText(R.id.my_text, “hahahha”);

更新小部件

appWidgetManager.updateAppWidget(appWidgetIds, remoteView);

设置按钮事件

remoteView.setOnClickPendingIntent(R.id.my_btm, getPendingIntent(context, BUTTON_SHOW));

参考学习地址:
1.https://www.cnblogs.com/playing/archive/2011/04/22/2024802.html
2.https://blog.csdn.net/liuqz2009/article/details/50922122
3.https://www.cnblogs.com/itchq/p/3817411.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值