2012-06-26 39 views
0

我有两个文件。我的onHandleIntent()没有被调用?

IntentServiceTest1Activity.java

public class IntentServiceTest1Activity extends Activity { 
    /** Called when the activity is first created. */ 

    public class ResponseReceiver extends BroadcastReceiver { 

     public static final String ACTION_RESP = "com.mamlambo.intent.action.MESSAGE_PROCESSED";@Override 
     public void onReceive(Context context, Intent intent) { 
      // TODO Auto-generated method stub 
      TextView result = (TextView) findViewById(R.id.txt_result); 
      String text = intent.getStringExtra(SimpleIntentService.PARAM_OUT_MSG); 
      result.setText(text); 
     } 
    } 
    private ResponseReceiver receiver; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP); 
     filter.addCategory(Intent.CATEGORY_DEFAULT); 
     receiver = new ResponseReceiver(); 
     registerReceiver(receiver, filter); 

     Button button = (Button) findViewById(R.id.button); 
     button.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       EditText input = (EditText) findViewById(R.id.txt_input); 
       String strInputMsg = input.getText().toString(); 
       Intent msgIntent = new Intent(IntentServiceTest1Activity.this, SimpleIntentService.class); 
       msgIntent.putExtra(SimpleIntentService.PARAM_IN_MSG, strInputMsg); 
       startService(msgIntent); 
      } 
     }); 
    } 
} 

SimpleIntentService.java

public class SimpleIntentService extends IntentService { 

    public static final String PARAM_IN_MSG = "imsg"; 
    public static final String PARAM_OUT_MSG = "omsg"; 
    public SimpleIntentService() 
    { 
     super("SimpleIntentService"); 
    } 
    @Override 
    protected void onHandleIntent(Intent intent) { 
     // TODO Auto-generated method stub 
     String msg = intent.getStringExtra(PARAM_IN_MSG); 
     SystemClock.sleep(30000); // 30 seconds   
     String resultTxt = msg + " " + DateFormat.format("MM/dd/yy h:mmaa", System.currentTimeMillis()); 

     Intent broadcastIntent = new Intent(); 
     broadcastIntent.setAction(ResponseReceiver.ACTION_RESP); 
     broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT); 
     broadcastIntent.putExtra(PARAM_OUT_MSG, resultTxt); 
     sendBroadcast(broadcastIntent); 
    } 
} 

通过Log.d(),我可以看到,即使在服务启动的onHandleIntent从未被调用。我的Log.d路径追踪startService(msgIntent);并在那里停下来。没有显示onHandleIntent内的Log.d。哪里不对?我能做些什么来使它工作?

谢谢!

+0

尝试(能布尔){ Log.d(标签, “setIntentRedelivery()”); super.setIntentRedelivery(enabled); } IntentService –

+0

你确定你已经在manifest中注册了SimpleIntentService吗?除了你的代码看起来不错,它应该可以工作。 –

+0

@VipulShah:嗨,是的,他正在注册清单中的服务 –

回答

1

我通过添加onStartCommand解决了我的问题,但不覆盖它。添加@覆盖 公共无效setIntentRedelivery后

public int onStartCommand(Intent intent, int flags, int startId) //sometimes overriding onStartCommand will not call onHandleIntent 
{ 

    Logger_.logtoFile(tag, "here..!", 1); 
    return super.onStartCommand(intent,flags,startId); 
} 
+1

我很困惑......这不是重写'onStartCommand'?你使用相同的参数调用'super.onStartCommand()'...所以这意味着你重写了它? –

+0

@AlexLockwood在这种方法中只从父级调用'onStartCommand()',如果你想覆盖你必须使用@ @ Override' annontation的方法。 – Trinity

+3

无论您是否添加“@Override”注释,Java中的方法仍被重写。添加注释不会影响任何Java程序的运行时行为。 –