单击小部件按钮并检测小部件Android的ID

时间:2012-11-26 18:22:42

标签: android android-widget

我的小部件和操作存在问题

在configurator类中,我为整个小部件和按钮设置了监听器:

 //the first one when we click on the button
 Intent firstIntent = new Intent(c.getApplicationContext(), ShowDetailedCityWeather.class);
 // Intent firstIntent = new Intent(WeatherWidget.ACTION_WIDGET_RECEIVER);
 PendingIntent myPI = PendingIntent.getActivity(c, 0, firstIntent, 0);
 firstIntent.setAction("Привет мир");
 remoteViews.setOnClickPendingIntent(R.id.imageButton1, myPI);
 firstIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);

 //the second one when we click on widget
 Intent secondIntent = new Intent(ChoiseCityList.this, MainTelephoneActivity.class);
 PendingIntent myPI2 = PendingIntent.getActivity(c, 0, secondIntent, 0);
 remoteViews.setOnClickPendingIntent(R.id.newLinearLayout, myPI2);


 manager.updateAppWidget(awID, remoteViews);

当我点击按钮时,新的Activity ShowDetailedCityWeather启动runnig。当我有许多小部件时,如何在此活动中检测小部件的ID?而且我也希望从小部件中获取数据。我试试:

Intent prevIntent = getIntent();
if(prevIntent != null)
 Toast.makeText(this, "Hello:" + prevIntent.getAction(), Toast.LENGTH_SHORT).show();
else
 Toast.makeText(this, "Help!", Toast.LENGTH_SHORT).show();

在Toast中我有:你好:null

然后我尝试了:

int awID = -90;
        Context c = ShowDetailedCityWeather.this;
        Intent intentWidget = getIntent();
        Toast.makeText(c, "Action : " + intentWidget.getAction(), Toast.LENGTH_SHORT).show();

        Bundle extras = intentWidget.getExtras();
        if(extras!=null)
           awID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        Toast.makeText(c, "ID : " + awID, Toast.LENGTH_SHORT).show();

我总是有-90。你能帮帮我吗...

enter code here

0 个答案:

没有答案