-1
我正在嘗試使用Broadcastreceiver編寫應用程序。不幸的是,它不能正常工作(它什麼都不做)。有人能幫助我嗎? 這裏是我的代碼: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);
}
};
}
是什麼你期望從你的廣播接收器?你在聽什麼事件 –