栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将数据从服务发送到活动

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将数据从服务发送到活动

您还必须修改清单文件。

这是有效的示例:

这些变量和方法是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);    }

这是Main活动中的方法:

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/495762.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号