5

我尝试编写我的第一个小部件应用程序,但遇到了问题。所以,我有 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);

在服务中,我执行以下操作:


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

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

}

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

4

0 回答 0