我正在尝试使用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);
}
};
}
答案 0 :(得分:0)
注册BroadcastReceiver
时,您应该使用操作,而不是额外的操作:
registerReceiver(keyboardReceiver, new IntentFilter(Intent.EXTRA_KEY_EVENT));
尝试使用类似的东西:
registerReceiver(keyboardReceiver, new IntentFilter(Intent.ACTION_MEDIA_BUTTON));