widget
1:在清单注册:
<receiver
android:name="com.youku.widget2.LenovoLeosWidgetDemoWidgetProvider"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android: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 {
private static final String TAG = "LenovoLeosWidgetDemoWidgetProvider";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.e(TAG, "----onUpdate-----");
// RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
// Intent intent = new Intent(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
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.e(TAG, "action ================================== " + action);
super.onReceive(context, intent);
}
}
5:横竖屏监听 :
在application:
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.e("yy", "当前屏幕为横屏");
} else {
Log.e("yy", "当前屏幕为竖屏");
}
super.onConfigurationChanged(newConfig);
}
清单中:
application加上这条语句
android:configChanges="orientation|keyboardHidden"