桌面小部件旋转屏后失效问题

ppWidgetProvider

@Override
public void onReceive(Context context, Intent intent)    
{
    super.onReceive(context, intent);

    if(intent.getAction().equals("test.CLICK"))
    {
        CallTestMethod(context);
    }
}
@Override
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];

        RemoteViews views=new RemoteViews(context.getPackageName(), R.layout.widget);            
        Intent clickintent=new Intent("test.CLICK");
        PendingIntent pendingIntentClick=PendingIntent.getBroadcast(context, 0, clickintent, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.change_mode, pendingIntentClick);
        SetInitialLayout(context);
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

 上述代码在旋转屏后失效,解决办法是

@Override
public void onReceive(Context context, Intent intent)    
{
    super.onReceive(context, intent);
    if(intent.getAction().equals("test.CLICK"))
    {
        getIntent().putExtra("Just received click", true);
        CallTestMethod(context);
    }
}

或者呢 如下使用

@Override
 public void onUpdate(Context context, AppWidgetManager appWidgetManager,
   int[] appWidgetIds) {
  context.startService(new Intent(context, MyUpdateService.class));
 }

public static class MyUpdateService extends Service 
 { 
  @Override
  public void onCreate() {
   super.onCreate();
  }

  @Override
  public void onDestroy() {
   super.onDestroy();
  }

  @Override 
  public void onStart(Intent intent, int startId) 
  { 
   super.onStart(intent, startId); 
   // Update the widget 
   RemoteViews remoteView = buildRemoteView(this); 

   // Push update to homescreen 
   pushUpdate(remoteView); 

             } 

  public RemoteViews buildRemoteView(Context context) 
  { 
   int layoutID = R.layout.widget;
   if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
    layoutID = R.layout.widget_landscape;
   }
                                    //Here is where you set your onclick listeners again since the remote views need to be refreshed/recreated
   RemoteViews updateView = new RemoteViews(context.getPackageName(),layoutID); 
   // Create an Intent to launch ExampleActivity
   Intent intent = new Intent(this, yourAndroidActivity.class);
   PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
     intent, 0);

   updateView.setOnClickPendingIntent(R.id.yourClickableViewHere, pendingIntent);

   return updateView; 
  } 

  @Override 
  public void onConfigurationChanged(Configuration newConfig) 
  { 
   RemoteViews remoteView = buildRemoteView(this); 
    // Push update to home screen 
   pushUpdate(remoteView); 
  } 

  private void pushUpdate(RemoteViews updateViews) 
  { 
   ComponentName myWidget = new ComponentName(this, YourAppWidget
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值