Android 桌面小部件制作

public class ButtonWidget extends AppWidgetProvider {
private Context mContext;
public static final int[] BUTTON_STRING_RES_ID = { R.string.rename_action,
R.string.cancel_action, R.string.group_applications};
public static final int[] BUTTON_DRAWABLE_RES_ID = { R.drawable.ic_launcher_home,
R.drawable.ic_launcher_home,R.drawable.ic_launcher_home};
static final ComponentName ONE_APPWIDGET =
            new ComponentName("com.android.settings",
                    "com.android.settings.Settings");
static final ComponentName TWO_APPWIDGET =
            new ComponentName("com.android.deskclock",
                    "com.android.deskclock.DeskClock");
static final ComponentName THREE_APPWIDGET =
            new ComponentName("com.optima.soundrecorder",
                    "com.optima.soundrecorder.SoundRecorder");
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d("wancg", "onReceive  action= " +action);
mContext = context;
refreshButtonWidget(context);




}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
}


@Override
public void onEnabled(Context context) {
mContext = context;
super.onEnabled(context);
}


@Override
public void onDisabled(Context context) {
mContext = context;
super.onDisabled(context);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}


@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
mContext = context;
Log.d("wancg", "onUpdate  action= " );
refreshButtonWidget(mContext);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
private void refreshButtonWidget(final Context context) {
Log.d("wancg", "refreshWidget  action= " );




//璁剧疆remoteViews鐩稿叧鍐呭
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.button_appwidget);
//设置背景
views.setInt(R.id.widget_layout, "setBackgroundResource", R.drawable.shape_corner);
//设置文字
views.setTextViewText(R.id.tv_one, context.getResources().getString(BUTTON_STRING_RES_ID[0]));
views.setTextViewText(R.id.tv_two, context.getResources().getString(BUTTON_STRING_RES_ID[1]));
views.setTextViewText(R.id.tv_three, context.getResources().getString(BUTTON_STRING_RES_ID[2]));
//设置图片
views.setImageViewResource(R.id.img_one,  BUTTON_DRAWABLE_RES_ID[0]);
views.setImageViewResource(R.id.img_two,  BUTTON_DRAWABLE_RES_ID[1]);
views.setImageViewResource(R.id.img_three, BUTTON_DRAWABLE_RES_ID[2]);
//设置点击事件
Intent widgetLayout = new Intent();
widgetLayout.setComponent(ONE_APPWIDGET);
        PendingIntent widgetLayoutIntent = PendingIntent.getActivity(context, 0, widgetLayout, 0);
        views.setOnClickPendingIntent(R.id.btn_one, widgetLayoutIntent);
        
        Intent widgetLayout_two = new Intent();
        widgetLayout_two.setComponent(TWO_APPWIDGET);
        PendingIntent widgetLayoutIntent_two = PendingIntent.getActivity(context, 0, widgetLayout_two, 0);
        views.setOnClickPendingIntent(R.id.btn_two, widgetLayoutIntent_two);
        
        Intent widgetLayout_three = new Intent();
        widgetLayout_three.setComponent(THREE_APPWIDGET);
        PendingIntent widgetLayoutIntent_three = PendingIntent.getActivity(context, 0, widgetLayout_three, 0);
        views.setOnClickPendingIntent(R.id.btn_three, widgetLayoutIntent_three);




AppWidgetManager awm = AppWidgetManager.getInstance(context);
int[] awIds = awm.getAppWidgetIds(new ComponentName(context, ButtonWidget.class));
awm.updateAppWidget(awIds, views);
}

}

2.清单文件注册

     
              <receiver android:name="com.negative.screen.widget.ButtonWidget" >
            <intent-filter>          
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="com.negative.screen.widget.NEGATIVE_ENABLE" />
                <action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
                <action android:name="android.appwidget.action.APPWIDGET_DISABLED" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                       android:resource="@xml/btn_widget_info" />

        </receiver>

 3. 新建widget资源文件xml

<?xml version="1.0" encoding="utf-8"?>

<!-- Copyright (C) 2008 The Android Open Source Project


     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<!-- M: Use our own AnalogClockWidget -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
   >


    <LinearLayout
        android:id="@+id/btn_one"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:paddingTop="12dp"
        
        android:clickable="true"
        android:focusable="true"
        android:orientation="vertical">


        <ImageView
            android:id="@+id/img_one"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:scaleType="center"
           
            android:contentDescription="@null"
            />


        <TextView
            android:id="@+id/tv_one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
          android:gravity="center"
            android:contentDescription="@null"
            
            />
        </LinearLayout>






    <LinearLayout
        android:id="@+id/btn_two"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:paddingTop="12dp"
      
        android:clickable="true"
        android:focusable="true"
        android:orientation="vertical">


        <ImageView
            android:id="@+id/img_two"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:scaleType="center"
            android:contentDescription="@null"
            
            />


        <TextView
            android:id="@+id/tv_two"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:contentDescription="@null"
            
            />


    </LinearLayout>






    <LinearLayout
        android:id="@+id/btn_three"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:paddingTop="12dp"
        android:clickable="true"
        android:focusable="true"
        android:orientation="vertical">


        <ImageView
            android:id="@+id/img_three"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:scaleType="center"
            
            android:contentDescription="@null"
            />


        <TextView
            android:id="@+id/tv_three"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
          
            />
    </LinearLayout>
</LinearLayout>

猜你喜欢

转载自blog.csdn.net/qingcai_yuanzi/article/details/80191396