Android 开发:当 phone 应用程序 activity 获得焦点时在键盘上挂钩快捷方式
Android development: hook shortcut on keyboard when phone app activity is focused
这是一个具体的问题,我之前做过一些 Android 开发,但对系统管理没有那么深入。
所以我需要创建一个在后台 运行 的应用程序(这部分没问题),并在使用特殊快捷方式时自动启动应用程序的 activity(比如 #123*6 ) 是从 phone 上的 phone 应用软件键盘输入的。
能否请您指出是否可行,如果可以,我应该使用什么API/Component?在网上找不到相关内容。
好的,我终于可以在 HTC 设备上使用它了。事实上,三星手机似乎对密码没有反应......
我只有 这个清单 :
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MenuBrowser"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".ShortcodeService"
android:exported="false">
</service>
<receiver
android:name=".ShortcodeReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code" android:host="1992" />
</intent-filter>
</receiver>
</application>
我的服务只是检查 Android M (6.0) 的正确权限,因为安全性发生了一些变化。我们现在必须在应用程序运行时即时声明权限,而不是在安装时。
我的activity:
public class MenuBrowser extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("USSDBrowser", "Start app");
//if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE, Manifest.permission.PROCESS_OUTGOING_CALLS}, 10);
Intent serviceIntent = new Intent(this.getApplicationContext(), ShortcodeService.class);
startService(serviceIntent);
//setContentView(R.layout.activity_menu_browser);
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_menu_browser, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我的接收器是这样的:
public class ShortcodeReceiver extends BroadcastReceiver {
private static String defaultCode = "1992";
@Override
public void onReceive(Context context, Intent intent) {
Log.d("USSDBrowser", "Intent received");
if(intent.getAction().equals("android.provider.Telephony.SECRET_CODE")) {
String code = intent.getDataString();
if(code.equals("android_secret_code://" + defaultCode)) {
Log.d("USSDBrowser", "Code received !!! ");
}
//Intent in = new Intent(context, MenuBrowser.class);
//context.startActivity(in);
Toast.makeText(context, "You typed a shortcode, hype !", Toast.LENGTH_LONG).show();
}
}
}
但现在我测试了它,它适用于 HTC One S (Android 4.1.1) 和 Aquaris E4 (Android 4.4.2)。
经测试未捕获密码意图的手机有:Samsung Galaxy S4 和 Galaxy S6 (Android 6.0)。
这是一个具体的问题,我之前做过一些 Android 开发,但对系统管理没有那么深入。
所以我需要创建一个在后台 运行 的应用程序(这部分没问题),并在使用特殊快捷方式时自动启动应用程序的 activity(比如 #123*6 ) 是从 phone 上的 phone 应用软件键盘输入的。
能否请您指出是否可行,如果可以,我应该使用什么API/Component?在网上找不到相关内容。
好的,我终于可以在 HTC 设备上使用它了。事实上,三星手机似乎对密码没有反应......
我只有 这个清单 :
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MenuBrowser"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".ShortcodeService"
android:exported="false">
</service>
<receiver
android:name=".ShortcodeReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code" android:host="1992" />
</intent-filter>
</receiver>
</application>
我的服务只是检查 Android M (6.0) 的正确权限,因为安全性发生了一些变化。我们现在必须在应用程序运行时即时声明权限,而不是在安装时。
我的activity:
public class MenuBrowser extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("USSDBrowser", "Start app");
//if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE, Manifest.permission.PROCESS_OUTGOING_CALLS}, 10);
Intent serviceIntent = new Intent(this.getApplicationContext(), ShortcodeService.class);
startService(serviceIntent);
//setContentView(R.layout.activity_menu_browser);
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_menu_browser, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我的接收器是这样的:
public class ShortcodeReceiver extends BroadcastReceiver {
private static String defaultCode = "1992";
@Override
public void onReceive(Context context, Intent intent) {
Log.d("USSDBrowser", "Intent received");
if(intent.getAction().equals("android.provider.Telephony.SECRET_CODE")) {
String code = intent.getDataString();
if(code.equals("android_secret_code://" + defaultCode)) {
Log.d("USSDBrowser", "Code received !!! ");
}
//Intent in = new Intent(context, MenuBrowser.class);
//context.startActivity(in);
Toast.makeText(context, "You typed a shortcode, hype !", Toast.LENGTH_LONG).show();
}
}
}
但现在我测试了它,它适用于 HTC One S (Android 4.1.1) 和 Aquaris E4 (Android 4.4.2)。
经测试未捕获密码意图的手机有:Samsung Galaxy S4 和 Galaxy S6 (Android 6.0)。