appwidget 实现广播机制

package com.example.appwidget02;

import com.example.appwidget02.R.drawable;

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

 

public class appwidget02 extends AppWidgetProvider{     private static final String MY_ACTION="my.action.APPWIDGET_UPDATE";//静态全局常量    @Override  public void onReceive(Context context, Intent intent) {   String action = intent.getAction();   if(MY_ACTION.equals(action)){    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidgit);    remoteViews.setImageViewResource(R.id.my_image, R.drawable.ic_launcher);    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);    ComponentName componentName = new ComponentName(context, appwidget02.class);    appWidgetManager.updateAppWidget(componentName, remoteViews);   }else{       super.onReceive(context, intent);  }  }    @Override  public void onUpdate(Context context, AppWidgetManager appWidgetManager,    int[] appWidgetIds) {   for(int i=0;i<appWidgetIds.length;i++){    Intent intent = new Intent();    intent.setAction(MY_ACTION);    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.example_appwidgit);    remoteViews.setOnClickPendingIntent(R.id.button1, pendingIntent);    appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);       }   super.onUpdate(context, appWidgetManager, appWidgetIds);  }  @Override  public void onEnabled(Context context) {   // TODO Auto-generated method stub   super.onEnabled(context);  }  @Override  public void onDisabled(Context context) {   // TODO Auto-generated method stub   super.onDisabled(context);  }  @Override  public void onDeleted(Context context, int[] appWidgetIds) {   // TODO Auto-generated method stub   super.onDeleted(context, appWidgetIds);  } }

 

配置文件

mainfest.xml

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

    <uses-sdk         android:minSdkVersion="14"         android:targetSdkVersion="14" />

    <application         android:allowBackup="true"         android:icon="@drawable/ic_launcher"         android:label="@string/app_name"         android:theme="@style/AppTheme" >         <activity             android:name=".MainActivity"             android:label="@string/app_name" >             <intent-filter>                 <action android:name="android.intent.action.MAIN" />

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

</manifest>

 

xml/example.xml

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="294dp"
    android:minHeight="294dp"
    android:updatePeriodMillis="86400000"
    android:initialLayout="@layout/example_appwidgit"
    >
   
</appwidget-provider>

 

layout/example_appwidget.xml

 

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical" >    <TextView        android:id="@+id/textView01"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="first widgit"        android:background="#000000"        />       <Button        android:id="@+id/button1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="点击测试"        />    <ImageView         android:id="@+id/my_image"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_margin="10dip"         android:src="@drawable/yes"          />

</LinearLayout>

 

转载于:https://www.cnblogs.com/caobo/p/4120653.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值