有时候 eclipse 和 虚拟机 两次跑一个程序 出不一样的结果 。 崩溃。。。。
建个 xml/hello_appwidget_info.xml
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="294dp" android:minHeight="272dp" android:updatePeriodMillis="86400000" android:initialLayout="@layout/hello_appwidget" > <!-- android:configure="demo.appwidget.AppWidgetConfig" --> </appwidget-provider>
布局 hello_appwidget
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/widgetText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="textfirst" /> <Button android:id="@+id/widgetButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击普通监听" /> <Button android:id="@+id/widgetButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="通过广播监听 " /> <Button android:id="@+id/widgetButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试 " /> </LinearLayout>
maifest
<application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".HelloAppWidget" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="HelloAppWidgetProvider"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <intent-filter> <action android:name="demo.appwidget.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/hello_appwidget_info" /> </receiver> <activity android:name=".AppWidgetConfig"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" /> </intent-filter> </activity> </application>
public class HelloAppWidgetProvider extends AppWidgetProvider { private static final String ACTION_UPATE="demo.appwidget.APPWIDGET_UPDATE"; @Override public void onDeleted(Context context, int[] appWidgetIds) { // TODO Auto-generated method stub System.out.println("onDeleted"); super.onDeleted(context, appWidgetIds); } @Override public void onDisabled(Context context) { // TODO Auto-generated method stub System.out.println("onDisabled"); super.onDisabled(context); } //只有在第一次添加一个widget才调用此方法 @Override public void onEnabled(Context context) { // TODO Auto-generated method stub System.out.println("onEnabled"); super.onEnabled(context); } //每次都调用update @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub System.out.println("onReceive"); String action = intent.getAction(); if (ACTION_UPATE.equals(action)) { System.out.println(action); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.hello_appwidget); remoteViews.setTextViewText(R.id.widgetText, "接受广播之后的值"); AppWidgetManager appWidgetManager = AppWidgetManager .getInstance(context); ComponentName componentName = new ComponentName(context, HelloAppWidgetProvider.class); // 更新 appWidgetManager.updateAppWidget(componentName, remoteViews); } else { super.onReceive(context, intent); } } //接着调用update @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // TODO Auto-generated method stub System.out.println("onUpdate"); //给所有的appWidgets 添加监听事件 for (int i = 0; i < appWidgetIds.length; i++) { System.out.println("update"+appWidgetIds[i]); Intent intent=new Intent(context,AppWidgetConfig.class); PendingIntent pendingIntent=PendingIntent.getActivity(context, 0, intent, 0); //得到appwidget控件 RemoteViews remoteView= new RemoteViews(context.getPackageName(), R.layout.hello_appwidget); //在appWidget中的控件 remoteView.setOnClickPendingIntent(R.id.widgetButton, pendingIntent); //更新指定id控件 appWidgetManager.updateAppWidget(appWidgetIds[i], remoteView); } //使用广播 Intent intent=new Intent(); intent.setAction(ACTION_UPATE); PendingIntent pendingIntent=PendingIntent.getBroadcast(context, 0, intent, 0); RemoteViews remoteViews=new RemoteViews(context.getPackageName(), R.layout.hello_appwidget); remoteViews.setOnClickPendingIntent(R.id.widgetButton2, pendingIntent); appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); super.onUpdate(context, appWidgetManager, appWidgetIds); } }
我在同一个provider 中即使用了 getActivity() 和 getBroadcast() 测试的时候发现 有时候行 有时候不行 , 有知道怎么回事的不 ? 请教?