2012-06-02 94 views
-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); 
     } 
    }; 
} 
+0

是什麼你期望從你的廣播接收器?你在聽什麼事件 –

回答

0

你應該使用一個行動,而不是額外的,註冊您的BroadcastReceiver時:

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

嘗試使用類似的東西:

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