RemoteViews和setOnClickPendingIntent

any*_*guy 5 android android-widget android-intent android-pendingintent

我尝试编写我的第一个小部件应用程序,我遇到了问题.所以,我有2个箭头:切换到左边并切换到右边.对于他们两个我想使用单个服务,它根据箭头的方向更改我的主屏幕小部件的视图.为了区分箭头的方向,我将额外的数据添加到我使用的每个意图中.这是我的示例代码:

    
RemoteViews remoteViews =
     new RemoteViews(context.getPackageName(),
      R.layout.presentation_layout);

 Intent updateServiceIntent1 = new Intent(context, UpdateService.class);
 updateServiceIntent1.putExtra("com.mycompany.direction", 1);
 PendingIntent updateServicePendingIntent1 =
     PendingIntent.getService(context, 0, updateServiceIntent1,
      PendingIntent.FLAG_UPDATE_CURRENT);
 //Action when we touch left arrow
 remoteViews.setOnClickPendingIntent(R.id.left,
  updateServicePendingIntent1);

 Intent updateServiceIntent2 = new Intent(context, UpdateService.class);
 updateServiceIntent2.putExtra("com.mycompany.direction", 0);
 PendingIntent updateServicePendingIntent2 =
     PendingIntent.getService(context, 0, updateServiceIntent2,
      PendingIntent.FLAG_UPDATE_CURRENT);
 //Action when we touch right arrow
 remoteViews.setOnClickPendingIntent(R.id.right,
  updateServicePendingIntent2);

 ComponentName thisWidget =
     new ComponentName(context, HelperWidget.class);
 AppWidgetManager manager = AppWidgetManager.getInstance(context);
 manager.updateAppWidget(thisWidget, remoteViews);
Run Code Online (Sandbox Code Playgroud)

在服务中我做下一个:


@Override
    public void onStart(Intent intent, int startId) {

   Log.i(TAG, "direction " + intent.getIntExtra("com.mycompany.direction", -1) + "");

}
Run Code Online (Sandbox Code Playgroud)

所以,在调试中我总是得到方向0.但是当我触摸向右箭头时,我想要触摸左箭头和方向0时获得方向1.你能帮我解决这个问题吗?谢谢.