widget

1:在清单注册:    

   <receiver

android:name="com.youku.widget2.LenovoLeosWidgetDemoWidgetProvider"

android:label="@string/app_name"

>

<intent-filter>

<actionandroid:name="android.appwidget.action.APPWIDGET_UPDATE"/>

</intent-filter>

<meta-dataandroid:name="android.appwidget.provider"android:resource="@xml/demo_info"/>

      </receiver> 

2:xml 文件夹下demo_info.xml

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

<appwidget-provider

xmlns:android="http://schemas.android.com/apk/res/android"

android:updatePeriodMillis="1800000"

android:initialLayout="@layout/widget"

android:minheight="288px"

android:minwidth="512px"

>

</appwidget-provider>

3.layout文件夹下widget.xml

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

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="512px"

android:layout_height="294px"

android:orientation="vertical"

    >

    <LinearLayout

android:id="@+id/linearLayout1"

android:layout_width="match_parent"

        android:layout_height="wrap_content" >

        <Button

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

            android:text="电视剧" />

        <Button

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

            android:text="电视台" />

        <Button

android:id="@+id/button3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

            android:text="电影" />

    </LinearLayout>

</LinearLayout>

4 在src 下:

public class LenovoLeosWidgetDemoWidgetProvider extends AppWidgetProvider {

privatestaticfinalStringTAG="LenovoLeosWidgetDemoWidgetProvider";

@Override

publicvoidonUpdate(Contextcontext,AppWidgetManagerappWidgetManager,int[]appWidgetIds){

Log.e(TAG,"----onUpdate-----");

//RemoteViewsviews=newRemoteViews(context.getPackageName(),R.layout.widget);

//Intentintent=newIntent(context,TargetActivity.class);

//        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

        for (int appWidgetId : appWidgetIds) {

//views.setOnClickPendingIntent(R.id.button,pendingIntent);

//appWidgetManager.updateAppWidget(appWidgetId,views);

}

super.onUpdate(context,appWidgetManager,appWidgetIds);

    }

    @Override

publicvoidonReceive(Contextcontext,Intentintent){

Stringaction=intent.getAction();

Log.e(TAG,"action=================================="+action);

super.onReceive(context,intent);

}

}

5:横竖屏监听 :

在application:

    @Override

publicvoidonConfigurationChanged(ConfigurationnewConfig){

//TODOAuto-generatedmethodstub

if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){

Log.e("yy","当前屏幕为横屏");

}else{

Log.e("yy","当前屏幕为竖屏");

}

super.onConfigurationChanged(newConfig);

    }

清单中:

application加上这条语句

android:configChanges="orientation|keyboardHidden"

相关推荐