Problem In Key Capture
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.
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 开发者_Go百科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;
}
}
I used broadcast reciever..
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);
}
}
Can anyone correct me..
You cannot capture key events outside of your application.
精彩评论