带有值的getStringExtra里面的Android AppWidgetProvider

时间:2015-09-11 22:38:23

标签: java android android-widget

我有小部件应用程序,我想在Textview值

中获取MainActivity.class

此MainActivity.class:

public class MainActivity extends Activity{
   protected void onCreate(Bundle savedInstanceState) {
     Intent widgetIntent = new Intent(this,widget1.class);

     //send value
     widgetIntent.putExtra("value",(TextView)findViewById(R.id.tvValue).getText());
     sendBroadcast(widgetIntent);         
   }
}

这个widget1.class:

public class widget1 extends AppWidgetProvider {
  private String value;
  private Bundle getVerseData;

    @Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    getVerseData = intent.getExtras();
    value= getVerseData.getString("value");
    super.onReceive(context, intent);

}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

    for (int i = 0; i < appWidgetIds.length; i++) {
        int currentWidgetId = appWidgetIds[i];
        Intent intent = new Intent(context, MainActivity.class);

        PendingIntent pending = PendingIntent.getActivity(context, 0,
                intent, 0);
        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.ygs_widget);

        views.setTextViewText(R.id.tvYgsGun,value);


        views.setOnClickPendingIntent(R.id.ygsRelative, pending);
        appWidgetManager.updateAppWidget(currentWidgetId, views);
    }

}
}

但是价值是空的。为什么?我在等你的答案。谢谢你

0 个答案:

没有答案