-1

我正在尝试使用 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);
        }
    };
}
4

1 回答 1

0

注册时,您应该使用一个动作,而不是额外的BroadcastReceiver

registerReceiver(keyboardReceiver, new IntentFilter(Intent.EXTRA_KEY_EVENT));

尝试使用类似的东西:

registerReceiver(keyboardReceiver, new IntentFilter(Intent.ACTION_MEDIA_BUTTON));
于 2012-06-02T20:12:48.690 回答