Problem In Key Capture
<p>I am developing an application that has to capture the key events both in foreground and in background. I used the following code. But am unable to capture the keys in background.</p>

<pre><code>package com.sri.KeyCap;
import android.app.Activity;
import android.content.Intent;
import android.os.IBinder;
import android.view.KeyEvent;
import android.widget.Toast;
import java.io.OutputStreamWriter;

public class KeyCap extends Activity {
 OutputStreamWriter out;
 int flag = 0;

 /** Called when the activity is first created. */

 public IBinder onBind(Intent intent)
 {
  return null;


  //super.onCreate(savedInstanceState);
  //setContentView(R.layout.main);

 }

 public boolean onKeyDown(int keyCode, KeyEvent event) {
  try {

   if (flag == 0) {
    out = new OutputStreamWriter(
      openFileOutput("myfilename.txt", 0));
    flag = 1;
   }

   out.write("" + keyCode);
   out.flush();
   Toast.makeText(getApplicationContext(), "" + keyCode,
     Toast.LENGTH_LONG).show();

  } catch (java.io.IOException e) {

   // do something if an IOException occurs.

  }

  return true;

 }
}
</code></pre>

<p>I used broadcast reciever..</p>

<pre><code>package com.sri.KeyCap;

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 

public class _service extends BroadcastReceiver {

 public void onReceive(Context aContext, Intent aIntent) { 


  aIntent.setClass(aContext,KeyCap.class);
  aContext.startActivity(aIntent);
 }
}
</code></pre>

<p>Can anyone correct me..</p>
