发送带有未决意图的数据并进入 AppWidgetProvider 中的打开活动
Posted
技术标签:
【中文标题】发送带有未决意图的数据并进入 AppWidgetProvider 中的打开活动【英文标题】:Send data with pending intent and get into open activity in AppWidgetProvider 【发布时间】:2013-11-18 21:36:42 【问题描述】:当我触发 Pending Intent 时,我正在发送数据。打开活动后,我正在获取数据。但它什么也没做。我不知道代码有什么问题。请帮我。
这是我发送数据的代码
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds)
super.onUpdate(context, appWidgetManager, appWidgetIds);
System.out.println("id len: " + appWidgetIds.length);
for (int i = 0; i < appWidgetIds.length; i++)
int appWidgetId = appWidgetIds[i];
System.out.println("id : " + appWidgetId);
Intent intent = new Intent(context, Report.class);
PendingIntent pend = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews view = new RemoteViews(context.getPackageName(),R.layout.widget_main_1);
view.setOnClickPendingIntent(R.id.repClear, pend);
appWidgetManager.updateAppWidget(appWidgetId, view);
Intent intent1 = new Intent(context, Map.class);
intent1.putExtra("widgetId", appWidgetId);
PendingIntent pend1 = PendingIntent.getActivity(context, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
view= new RemoteViews(context.getPackageName(),R.layout.widget_main_1);
view.setOnClickPendingIntent(R.id.imageButton1, pend1);
//intent1.putExtra("string", "view.getId()");
appWidgetManager.updateAppWidget(appWidgetId, view);
Intent intent2 = new Intent(context, Map.class);
PendingIntent pend2 = PendingIntent.getActivity(context, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
view= new RemoteViews(context.getPackageName(),R.layout.widget_main_1);
view.setOnClickPendingIntent(R.id.imageButton2, pend2);
//intent2.putExtra("string", "view.getId()");
appWidgetManager.updateAppWidget(appWidgetId, view);
这是我获取数据的代码
protected void onPostExecute(String addressText)
selectedLocAddress = addressText;
//String action = intent.getAction();
int action = getIntent().getIntExtra("widgetId",0);
switch (action)
case R.id.imageButton1:
gMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_bue)).title(addressText));
break;
【问题讨论】:
【参考方案1】:我终于解决了这个问题。这不是最佳解决方案,但可能会对您有所帮助
这里是发送数据的代码
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds)
super.onUpdate(context, appWidgetManager, appWidgetIds);
System.out.println("id len: " + appWidgetIds.length);
for (int i = 0; i < appWidgetIds.length; i++)
int appWidgetId = appWidgetIds[i];
System.out.println("id : " + appWidgetId);
Intent intent = new Intent(context, Report.class);
PendingIntent pend = PendingIntent.getActivity(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.widget_main_1);
view.setOnClickPendingIntent(R.id.repClear, pend);
// view.setViewVisibility(R.id.repClear, View.INVISIBLE);
// view.setViewVisibility(R.id.repClear1, View.VISIBLE);
appWidgetManager.updateAppWidget(appWidgetId, view);
Intent intent1 = new Intent(context, Map.class);
intent1.putExtra("data 1", true);
intent1.setAction("myString1"+ 1);
PendingIntent pend1 = PendingIntent.getActivity(context, 1, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
view = new RemoteViews(context.getPackageName(), R.layout.widget_main_1);
view.setOnClickPendingIntent(R.id.imageButton1, pend1);
appWidgetManager.updateAppWidget(appWidgetId, view);
Intent intent2 = new Intent(context, Map.class);
intent2.putExtra("data 2", true);
intent2.setAction("myString2"+ 2);
PendingIntent pend2 = PendingIntent.getActivity(context, 2, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
view = new RemoteViews(context.getPackageName(), R.layout.widget_main_1);
view.setOnClickPendingIntent(R.id.imageButton2, pend2);
appWidgetManager.updateAppWidget(appWidgetId, view);
Intent intent3 = new Intent(context, Map.class);
intent3.putExtra("data 3", true);
intent3.setAction("myString3"+ 3);
PendingIntent pend3 = PendingIntent.getActivity(context, 3, intent3, PendingIntent.FLAG_UPDATE_CURRENT);
view = new RemoteViews(context.getPackageName(), R.layout.widget_main_1);
view.setOnClickPendingIntent(R.id.imageButton3, pend3);
appWidgetManager.updateAppWidget(appWidgetId, view);
Intent intent4 = new Intent(context, Map.class);
intent4.putExtra("data 4", true);
intent4.setAction("myString4"+ 4);
PendingIntent pend4 = PendingIntent.getActivity(context, 4, intent4, PendingIntent.FLAG_UPDATE_CURRENT);
view = new RemoteViews(context.getPackageName(), R.layout.widget_main_1);
view.setOnClickPendingIntent(R.id.imageButton4, pend4);
appWidgetManager.updateAppWidget(appWidgetId, view);
Intent intent5 = new Intent(context, Map.class);
intent5.putExtra("data 5", true);
intent5.setAction("myString5"+ 5);
PendingIntent pend5 = PendingIntent.getActivity(context, 5, intent5, PendingIntent.FLAG_UPDATE_CURRENT);
view = new RemoteViews(context.getPackageName(), R.layout.widget_main_1);
view.setOnClickPendingIntent(R.id.imageButton5, pend5);
appWidgetManager.updateAppWidget(appWidgetId, view);
Intent intent6 = new Intent(context, Map.class);
intent6.putExtra("data 6", true);
intent6.setAction("myString6"+ 6);
PendingIntent pend6 = PendingIntent.getActivity(context, 6, intent6, PendingIntent.FLAG_UPDATE_CURRENT);
view = new RemoteViews(context.getPackageName(), R.layout.widget_main_1);
view.setOnClickPendingIntent(R.id.imageButton6, pend6);
appWidgetManager.updateAppWidget(appWidgetId, view);
这是获取数据的代码
protected void onPostExecute(String addressText)
selectedLocAddress = addressText;
Bundle extras = getIntent().getExtras();
data1 = extras.getBoolean("data 1");
data2 = extras.getBoolean("data 2");
data3 = extras.getBoolean("data 3");
data4 = extras.getBoolean("data 4");
data5 = extras.getBoolean("data 5");
data6 = extras.getBoolean("data 6");
System.out.println("d1 "+data1+" d2 "+data2);
if(data1==true)
gMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_bue)).title(selectedLocAddress));
else if(data2==true)
gMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_green)).title(selectedLocAddress));
else if(data3==true)
gMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_light)).title(selectedLocAddress));
else if(data4==true)
gMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_purple)).title(selectedLocAddress));
else if(data5==true)
gMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_red)).title(selectedLocAddress));
else if(data6==true)
gMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_yellow)).title(selectedLocAddress));
【讨论】:
以上是关于发送带有未决意图的数据并进入 AppWidgetProvider 中的打开活动的主要内容,如果未能解决你的问题,请参考以下文章