Android RemoteView的应用 一 桌面控件

这回通过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>
  1. 在res/xml 新建appwidget_provider_info文件,为了定义桌面控件配置信息,比如设置控件的大小和自动更新时间

<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>
  1. 新建控件的实现类
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);

    }
}

  1. 在AndroidManifest声明控件,这个声明将实现类和widget联系在一起,并声明这是一个能够更新和反馈点击事件的桌面小部件,
<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>

再见

你可能感兴趣的:(Android相关)