【发布时间】:2016-12-29 12:37:10
【问题描述】:
我的应用不会调用来自 wakefulbroadcastreceiver 的 intentservice 请求
清单:
<service
android:name=".MyWearableListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED"/>
<data android:scheme="wear" android:host="*"/>
</intent-filter>
</service>
<service
android:name=".CounterActivity$WearableReceiverService"
android:exported="false">
</service>
<receiver
android:name=".CounterActivity$WearableReceiver"
android:enabled="true">
</receiver>
所以我注册了所有接收器和服务。
在我的主要活动中,我将这些作为主类中的子类,因此我可以调用主类 msgReqAction() 中的方法
public class WearableReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, WearableReceiverService.class);
startWakefulService(context, service);
}
}
public class WearableReceiverService extends IntentService {
public WearableReceiverService(){
super("WearableReceiverService");
}
@Override
protected void onHandleIntent(Intent intent) {
msgReqAction(intent.getIntExtra(MyConstants.BROADCAST_DATA_REQ, 0));
WearableReceiver.completeWakefulIntent(intent);
}
}
我不认为将这些作为子类会妨碍这种情况,但它可能会。如果我必须将这些放在主类之外进行操作,请告诉我。
最后,我从主要活动之外的侦听器开始整个过程,该侦听器侦听来自可穿戴设备的消息
@Override
public void onMessageReceived(final MessageEvent messageEvent) {
nodeId = messageEvent.getSourceNodeId();
String incomingPath = messageEvent.getPath();
int incomingReq = Integer.parseInt(new String(messageEvent.getData()));
if(incomingPath.equalsIgnoreCase(MyConstants.MSG_COUNTER_REQ_PATH)) {
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(MyConstants.BROADCAST_ACTION_RESP);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra(MyConstants.BROADCAST_DATA_REQ, incomingReq);
sendBroadcast(broadcastIntent);
}else if(incomingPath.equalsIgnoreCase(MyConstants.MSG_DEFAULT_PATH)){
}
}
public static final String BROADCAST_ACTION_RESP = "com.example.johnbravado.zionwork.MESSAGE_PROCESSED";
我的项目是 com.example.johnbravado.zionwork - 顺便说一句,有没有办法在 android studio 中轻松更改和重构它,以便我可以摆脱示例或完全更改它?
当我运行调试器时,系统会一直运行到
startWakefulService(context, service);
然后它在没有进入意图服务的情况下崩溃。在所有这一切中,我是否缺少一些简单的东西来阻止它进入服务和工作。我能说的最好的是它根本没有进入服务。我添加了一些服务的介绍行
@Override
protected void onHandleIntent(Intent intent) {
int data;
data = 0;
data++;
msgReqAction(intent.getIntExtra(MyConstants.BROADCAST_DATA_REQ, 0));
WearableReceiver.completeWakefulIntent(intent);
}
并尝试在这些行上运行调试点,但没有到达那里。
【问题讨论】:
标签: android android-intent android-service wear-os android-broadcastreceiver