我正在尝试使用 Broadcastreceiver 编写应用程序。不幸的是,它不能正常工作(它什么也不做)。有人可以帮助我吗?这是我的代码:
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;
public class MyApp extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerReceiver(keyboardReceiver, new IntentFilter(Intent.EXTRA_KEY_EVENT));
}
private BroadcastReceiver keyboardReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent i) {
KeyEvent keyEvent = (KeyEvent)i.getExtras().get(Intent.EXTRA_KEY_EVENT);
int test = keyEvent.getKeyCode();
Toast.makeText(getApplicationContext(), String.valueOf(test), Toast.LENGTH_SHORT);
}
};
}