这回通过RemoteView来制作一个能够接受点击事件的桌面控件。
1. 在res/layout 新建widget文件,这是设计桌面控件的界面
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/sample"/>
RelativeLayout>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget"
android:minHeight="84dp"
android:minWidth="84dp"
android:updatePeriodMillis="86400000">
appwidget-provider>
public class MyAppWidgetProvider extends AppWidgetProvider {
public static final String TAG = "MyAppWidgetProvider";
public static final String CLICK_ACTION = "com.example.action.CLICK";
private static RemoteViews mRemoteViews;
@Override
public void onDeleted(Context context,int[] appWidgetIds){
super.onDeleted(context, appWidgetIds);
Log.i(TAG, "onDeleted");
}
@Override
public void onDisabled(Context context){
super.onDisabled(context);
Log.i(TAG,"onDisabled");
}
@Override
public void onEnabled(Context context){
super.onEnabled(context);
Log.i(TAG,"onEnabled");
}
@Override
public void onReceive(final Context context,Intent intent){
super.onReceive(context, intent);
Log.i(TAG,"onReceive : action = "+intent.getAction());
if (intent.getAction().equals(CLICK_ACTION)){
Toast.makeText(context,"clicked it",Toast.LENGTH_SHORT).show();
new Thread(new Runnable() {
@Override
public void run() {
Bitmap srcbBitmap = BitmapFactory.decodeResource(context.getResources()
,R.drawable.sample);
for(int i=0;i<20;i++){
float degree = (i*90)%360;
mRemoteViews.setImageViewBitmap(R.id.imageView1, rotateBitmap(
context, srcbBitmap, degree));
Intent intentClick = new Intent();
intentClick.setAction(CLICK_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0,
intentClick,0);
mRemoteViews.setOnClickPendingIntent(R.id.imageView1, pendingIntent);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.updateAppWidget(new ComponentName(context,MyAppWidgetProvider.class),mRemoteViews);
try{
Thread.sleep(100);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
}).start();
}
}
@Override
public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds){
super.onUpdate(context, appWidgetManager, appWidgetIds);
Log.i(TAG,"onUpdate");
final int counter = appWidgetIds.length;
Log.i(TAG,"counter = "+counter);
for(int i=0;iint appWidgetId = appWidgetIds[i];
onWidgetUpdate(context,appWidgetManager,appWidgetId);
}
}
private Bitmap rotateBitmap(Context context,Bitmap srcbBitmap,float degree){
Matrix matrix = new Matrix();
matrix.reset();
matrix.setRotate(degree);
Bitmap tmpBitmap = Bitmap.createBitmap(srcbBitmap,0,0,srcbBitmap.getWidth(),
srcbBitmap.getHeight(),matrix,true);
return tmpBitmap;
}
private void onWidgetUpdate(Context context,AppWidgetManager appWidgetManager,
int appWidgetId){
Log.i(TAG,"appWidgetId = "+appWidgetId);
mRemoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget);
Intent intentClick = new Intent();
intentClick.setAction(CLICK_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0,
intentClick,0);
mRemoteViews.setOnClickPendingIntent(R.id.imageView1,pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId,mRemoteViews);
}
}
<receiver
android:name=".MyAppWidgetProvider"
android:icon="@mipmap/ic_launcher" >
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget_provider_info" >
meta-data>
<intent-filter>
<action android:name="com.example.action.CLICK" />
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
intent-filter>
receiver>
再见