android 中 startService(intent) 为什么不执行service的onStartCommand(),我是新手,请帮忙!谢谢
启动serviceIntentintent=newIntent("com.Sevices.mqtt.ReceivingMessageService");intent.pu...
启动service
Intent intent = new Intent("com.Sevices.mqtt.ReceivingMessageService");
intent.putExtra("message", s);
startService(intent);
注册
<service android:name="com.Sevices.mqtt.ReceivingMessageService"/>
service的onStartCommand方法
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i("serv1", "onStartCommand");
String s = intent.getStringExtra("message");
if (s != null && s.length() > 0)
{
Log.i("serv1", "message:"+s);
}
return super.onStartCommand(intent, flags, startId);
} 展开
Intent intent = new Intent("com.Sevices.mqtt.ReceivingMessageService");
intent.putExtra("message", s);
startService(intent);
注册
<service android:name="com.Sevices.mqtt.ReceivingMessageService"/>
service的onStartCommand方法
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i("serv1", "onStartCommand");
String s = intent.getStringExtra("message");
if (s != null && s.length() > 0)
{
Log.i("serv1", "message:"+s);
}
return super.onStartCommand(intent, flags, startId);
} 展开
2个回答
展开全部
你只是简单的注册了<service android:name="com.Sevices.mqtt.ReceivingMessageService"/>
并没有给这个service起个名字,所以Intent intent = new Intent("com.Sevices.mqtt.ReceivingMessageService");
这样调用是启动不了你的service的,请改为类似如下:
Intent intent = new Intent(MainActivity.this,com.Sevices.mqtt.ReceivingMessageService.class);
或者在manifest中加个别名:
<service android:name="com.Sevices.mqtt.ReceivingMessageService">
<intent-filter>
<action android:name="com.Sevices.mqtt.ReceivingMessageService" />
</intent-filter>
</service>
并没有给这个service起个名字,所以Intent intent = new Intent("com.Sevices.mqtt.ReceivingMessageService");
这样调用是启动不了你的service的,请改为类似如下:
Intent intent = new Intent(MainActivity.this,com.Sevices.mqtt.ReceivingMessageService.class);
或者在manifest中加个别名:
<service android:name="com.Sevices.mqtt.ReceivingMessageService">
<intent-filter>
<action android:name="com.Sevices.mqtt.ReceivingMessageService" />
</intent-filter>
</service>
2013-11-07
展开全部
If someone calls Context.startService() then the system will retrieve the service (creating it and calling its onCreate() method if needed) and then call itsonStartCommand(Intent, int, int) method with the arguments supplied by the client.
请贴出Service的完整代码
请贴出Service的完整代码
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询