资料来源:网络整理
时间:2023/2/14 0:55:52 共计:3611 浏览
/// <summary>
/// 重写按键事件
/// </summary>
/// <param name="keyCode"></param>
/// <param name="e"></param>
/// <returns></returns>
public override bool OnKeyDown([GeneratedEnum] Keycode keyCode, KeyEvent e)
{
// 根据某种情形禁用返回键
if (keyCode == Keycode.Back && disableBack)
{
PackageManager pm = PackageManager;
ResolveInfo homeInfo = pm.ResolveActivity(new Intent(Intent.ActionMain).AddCategory(Intent.CategoryHome), 0);
ActivityInfo ai = homeInfo.ActivityInfo;
Intent startIntent = new Intent(Intent.ActionMain);
startIntent.AddCategory(Intent.CategoryLauncher);
startIntent.SetComponent(new ComponentName(ai.PackageName, ai.Name));
StartActivitySafely(startIntent);
return false;
}
return base.OnKeyDown(keyCode, e);
}
private void StartActivitySafely(Intent intent)
{
intent.AddFlags(ActivityFlags.NewTask);
try
{
StartActivity(intent);
}
catch (ActivityNotFoundException ex)
{
Toast.MakeText(this, "StartActivitySafely()异常:" + ex.Message, ToastLength.Short).Show();
}
catch (SecurityException ex)
{
Toast.MakeText(this, "StartActivitySafely()异常:" + ex.Message, ToastLength.Short).Show();
}
}
版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。