gpt4 book ai didi

android - 我的 onHandleIntent() 没有被调用?

转载 作者:行者123 更新时间:2023-11-30 04:08:41 24 4
gpt4 key购买 nike

我有两个文件。

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。怎么了?我该怎么做才能让它发挥作用?

谢谢!

最佳答案

我通过添加 onStartCommand 但没有覆盖它来解决我的问题。

 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);
}

关于android - 我的 onHandleIntent() 没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11202101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com