android - 关键捕获问题

标签 android

我正在开发一个必须同时在前台和后台捕获关键事件的应用程序。我使用了以下代码。但是我无法在后台捕获 key 。

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;

 }
}

我用的是广播接收器..

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);
 }
}

谁能纠正我..

最佳答案

您无法在应用程序之外捕获关键事件。

关于android - 关键捕获问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3332573/

相关文章:

android - 用户必须购买硬币才能在我的 Android 应用程序中调用某人。对于我的场景,Google Pay 或 Google In-App Billing 哪个更好?

android - java.lang.IllegalStateException : Not connected. 调用connect()并等待onConnected()被调用

android - 创建签名包后执行任务

java - 查找 graphics2D 的 Android 等效代码

android - 将径向渐变设置为窗口背景

android - 如何使 TextView 中特定单词的出现可点击

android - 如何在 Android 中解析复杂的 soap 对象

android - 在 Android 中删除 SQLite 中的行

android - 您应该选择什么构建目标和最低 SDK 版本?

android - 动 Canvas 局的背景颜色变化