桌面组件AppWidget

可以添加的窗口小部件,代码

public class MyAppWidget extends AppWidgetProvider {

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onDeleted(context, appWidgetIds);
    }

    @Override
    public void onDisabled(Context context) {
        // TODO Auto-generated method stub
        super.onDisabled(context);
    }

    @Override
    public void onEnabled(Context context) {
        // TODO Auto-generated method stub
        super.onEnabled(context);
    }
//处理广播
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        super.onReceive(context, intent);
    }
//更新时触发
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }
         
}

在AndroidManifest.xml中设置


           
               
           

                            android:resource="@xml/my_appwidget"/>
       

//桌面组件大小设置的xml

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="300px"
    android:minHeight="80px"
    android:updatePeriodMillis="3600"
    android:initialLayout="@layout/app_provider">

//在桌面上要显示的组件

        android:layout_width="fill_parent"
        android:layout_height="match_parent"
         >
          android:id="@+id/app_city"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerHorizontal="true"
       
      />
          android:id="@+id/app_weather"
       android:layout_below="@+id/app_city"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
          android:id="@+id/app_area"
       android:layout_below="@+id/app_weather"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="area"
       />
   

//要实现在桌面点击的效果,这样可以跳转到指定的Activity,这个是官方的文档

public class ExampleAppWidgetProvider extends AppWidgetProvider {

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            // Create an Intent to launch ExampleActivity
            Intent intent = new Intent(context, ExampleActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            // Get the layout for the App Widget and attach an on-click listener
            // to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
            views.setOnClickPendingIntent(R.id.button, pendingIntent);

            // Tell the AppWidgetManager to perform an update on the current app widget
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
}
//如果是要刷新Appwidget界面的组件或者信息,看以下代码

public class MyAppWidget extends AppWidgetProvider {

      @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

     if("org.first.demo".equals(intent.getAction){

      RemoteView remote=new RemoteView(context.PackageName(),R.layout.app_provider)

   remote.setImageViewViewResource(R.id.img,R.drawable.mldn_man)

remote.setTextView(R.id.textView,"要替换成这些文字");

AppwidgetManager appwidgetManager=AppWidgetManager.getInstance(context);

ComponentName conponenName=new ComponenName(context,MainActivity.class);

appwidgetManager.updateAppWidget(conponenName,remote)

}else{

       super.onReceive(context, intent);

}
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
       Intent  it=new Intent();

it.setAction("org.first.demo");

//注意这个PendingIntent与上面的区别上面的是getActivity指定跳转的Activity,这个是getBroadCast是更新Appwidget

PendingIntent pending=PendingIntent.getBroadcast(context,0,it,PendingIntent.FLAG_UPDATE_CURRENT);

RemoteView remote=new RemoteView(context.PackName(),R.layout.app_widget);

remote.setOnCllickPendingIntent(R.id.app_area,pending)

 appWidgetManager.updateAppWidget(appWidgetId, remote);

    }
    }     
}

//同时在androidManifest.xml中修改文件

 
           
               
           

           
                           

                            android:resource="@xml/my_appwidget"/>
       


你可能感兴趣的:(android)