动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); }
2013-11-07 14:43
提问者采纳
你只是简单的注册了<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>
提问者评价
-
谢谢!