java - 将数据从服务发送到 Activity

标签 java android

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

但仍然没有运气。

其他人是否也遇到过同样的问题?

提前致谢。

最佳答案

您还必须修改 Manifest 文件。

这是有效的例子:

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

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);
    }

这是主要 Activity 中的方法:

你必须在 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>

关于java - 将数据从服务发送到 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1464853/

相关文章:

java - 使用 java.nio 时抛出 IllegalArgumentException

java - Elasticsearch 分页 - 最后一页返回太多结果

android - Facebook Android 生成 key 哈希

android - Android:如何使gradle离线获得特定的依赖关系,而其他在线?

java - 如何正确使用setLocaleScale方法?我在确定 AR 模型的大小时遇到​​问题

Android Studio 无法识别我的小米红米 Note 3

运行新项目时的 Android 错误消息

java - Querydsl列表包含某个类的对象

java - 用 POI 命名一个单元格

java - Preon 中的有符号和无符号整数