【问题标题】:Sending data from service to activity将数据从服务发送到活动
【发布时间】:2009-09-23 09:05:36
【问题描述】:

我在通过 Notification 将数据从 Service 发送到 Activity 时遇到问题,我单击 Notification 一个 Activity 被调用,但是当我尝试通过 bundle 添加一些参数时,我无法获取调用意图中的参数,我有通过链接 How to send parameters from a notification-click to an activity?

但还是没有运气。

其他人也有同样的问题吗?

提前致谢。

【问题讨论】:

  • 如何将数据从服务发送到活动?

标签: java android


【解决方案1】:

您还必须修改 Manifest 文件。

这是一个有效的例子:

这些变量和方法是Service类的成员:

public static final String MOVEMENT_UPDATE = "com.client.gaitlink.AccelerationService.action.MOVEMENT_UPDATE";
    public static final String ACCELERATION_X = "com.client.gaitlink.AccelerationService.ACCELERATION_X";
    public static final String ACCELERATION_Y = "com.client.gaitlink.AccelerationService.ACCELERATION_Y";
    public static final String ACCELERATION_Z = "com.client.gaitlink.AccelerationService.ACCELERATION_Z";

private void announceAccelerationChanges()//this method sends broadcast messages
    {
        Intent intent = new Intent(MOVEMENT_UPDATE);
        intent.putExtra(ACCELERATION_X, accelerationX);
        intent.putExtra(ACCELERATION_Y, accelerationY);
        intent.putExtra(ACCELERATION_Z, accelerationZ);

        sendBroadcast(intent);
    }

这是来自主要活动的方法:

你必须在 onResume 方法中注册接收者:

    @Override
    public void onResume()
    {   

        IntentFilter movementFilter;
        movementFilter = new IntentFilter(AccelerationService.MOVEMENT_UPDATE);
        accelerationReceiver = new AccelerationServiceReceiver();
        registerReceiver(accelerationReceiver, movementFilter);


        startAccelerationService();

        super.onResume();
    }

    private void startAccelerationService()
    {
        startService(new Intent(this, AccelerationService.class));
    }

    public class AccelerationServiceReceiver extends BroadcastReceiver
    {
      @Override
        public void onReceive(Context context, Intent intent)//this method receives broadcast messages. Be sure to modify AndroidManifest.xml file in order to enable message receiving
        {
            accelerationX = intent.getDoubleExtra(AccelerationService.ACCELERATION_X, 0);
            accelerationY = intent.getDoubleExtra(AccelerationService.ACCELERATION_Y, 0);
            accelerationZ = intent.getDoubleExtra(AccelerationService.ACCELERATION_Z, 0);

            announceSession();

            updateGUI();
        }
    }

这是为了接收广播消息而必须设置的 AndroidManifest.xml 文件的一部分:

<activity android:name=".GaitLink"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

                <action android:name="com.client.gaitlink.CommunicationService.action.ACTIVITY_STATUS_UPDATE" />

            </intent-filter>
        </activity>

【讨论】:

  • 我已经尝试过了,但它没有按预期工作..我已经编写了日志语句,但没有在 onRecieve 方法中显示。我可能做错了什么,我正在做和说的一样!
  • 服务是否广播消息? BroadcastReceiver 是否已在活动中启动并运行?你在 AndroidManifest.xml 中添加了意图过滤器吗?
  • 我已经在 AndroidManifest.xml 文件中添加了 BroadcastReceiver,是的,该服务进行了广播,我如何确认 BroadcastReceiver 已经启动。
  • Niko,您的示例运行良好。这对我帮助很大。非常感谢。
  • 您的代码不能 100% 工作,但使用广播和 BroadcastReceivers 正是我想要使用的
猜你喜欢
  • 2012-12-30
  • 1970-01-01
  • 2013-07-28
  • 2021-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多