Android:如何隐藏或关闭最高活动?

发布于 2025-01-27 07:03:30 字数 2486 浏览 4 评论 0 原文

在我的应用中,我需要使用动作。此操作的原因是,在这种情况下,我可以使用 Pro摄像机模式,例如手动ISO,曝光时间等,同时使用动作

​Intent Filter action_new_picture 我也能够捕捉照片的照片拍摄。

我需要的是一旦拍摄照片,就可以将内置的相机活动隐藏在广播接收器的 OnReceive 方法中。到目前为止,我尝试的是,我通过指定意图动作标志flag_activitivity_reorder_to_front和flag_activitivitive_single_top以这种方式(一个C#语言代码段,但几乎与Java相同),从OnReceive中调用了我的活动:

public override void OnReceive(Context context, Intent intent) {
    Toast.MakeText(context, $"Data1: {intent.DataString}", ToastLength.Short).Show();

    var path = Path.Combine(context.CacheDir.AbsolutePath, $"Photo_{DateTime.Now.ToString("yyyyMMMdd_HHmmss")}.jpg");
    try {
        // copy the photo to app cache dir
        using (var outs = File.Create(path)) {
            using (var ins = context.ContentResolver.OpenInputStream(intent.Data)) {
                ins.Seek(0, SeekOrigin.Begin);
                ins.CopyTo(outs);
            }
        }
        
        var a = Xamarin.Essentials.Platform.CurrentActivity;
        var tvInfo = a.FindViewById<AppCompatTextView>(Resource.Id.tvInfo);
        tvInfo.Append("Picture was taken and saved to:");
        tvInfo.Append($"{path}{SysEnv.NewLine}{SysEnv.NewLine}");

        // hide the builtin camera's Activity by brining my Activity to top
        var i = new Intent(context, typeof(MainActivity)).SetFlags(ActivityFlags.ReorderToFront | ActivityFlags.SingleTop);
        a.StartActivity(i);
    }
    catch (Exception ex) {
        Toast.MakeText(context, $"Error: {ex.Message}", ToastLength.Short).Show();
    }
}

这是我如何启动相机的

var btnTake = FindViewById<AppCompatButton>(Resource.Id.btnTake);
btnTake.Click += (s, e) => {
    var i = new Intent(MediaStore.IntentActionStillImageCamera);
    var r = i.ResolveActivity(this.PackageManager);
    if (r != null) {
        StartActivity(i);
    }
};

问题 :是它仅在第一次尝试中起作用,这意味着我单击应用程序的按钮,内置相机启动。当我拍照时,将图片保存并复制到我的应用程序的Cache Dir中,内置相机的活动消失了,并且我的活动出现了。 但是在第二次尝试中,我的活动没有出现,而Bulti-In相机的活动则停留在顶部。 有什么想法,如何隐藏或关闭内置相机的活动?

In my app, I need to start the built-in camera application using the action INTENT_ACTION_STILL_IMAGE_CAMERA. The reason of this action is that in this case, I can use the Pro camera mode, like manual ISO, exposure time, etc., while using the action ACTION_IMAGE_CAPTURE this is not possible (at least for the builtin camera app for my Samsung Galaxy S20+ device)

Using the broadcast receiver intent filter ACTION_NEW_PICTURE I am also able to catch the URI of the photo taken.

What I need is to hide the built-in camera Activity from the broadcast receiver's OnReceive method, once the photo was taken. What I tried so far is that I called my Activity from OnReceive by specifying the intent action flags FLAG_ACTIVITY_REORDER_TO_FRONT and FLAG_ACTIVITY_SINGLE_TOP this way (a C# language snippet, but almost identical to Java):

public override void OnReceive(Context context, Intent intent) {
    Toast.MakeText(context, 
quot;Data1: {intent.DataString}", ToastLength.Short).Show();

    var path = Path.Combine(context.CacheDir.AbsolutePath, 
quot;Photo_{DateTime.Now.ToString("yyyyMMMdd_HHmmss")}.jpg");
    try {
        // copy the photo to app cache dir
        using (var outs = File.Create(path)) {
            using (var ins = context.ContentResolver.OpenInputStream(intent.Data)) {
                ins.Seek(0, SeekOrigin.Begin);
                ins.CopyTo(outs);
            }
        }
        
        var a = Xamarin.Essentials.Platform.CurrentActivity;
        var tvInfo = a.FindViewById<AppCompatTextView>(Resource.Id.tvInfo);
        tvInfo.Append("Picture was taken and saved to:");
        tvInfo.Append(
quot;{path}{SysEnv.NewLine}{SysEnv.NewLine}");

        // hide the builtin camera's Activity by brining my Activity to top
        var i = new Intent(context, typeof(MainActivity)).SetFlags(ActivityFlags.ReorderToFront | ActivityFlags.SingleTop);
        a.StartActivity(i);
    }
    catch (Exception ex) {
        Toast.MakeText(context, 
quot;Error: {ex.Message}", ToastLength.Short).Show();
    }
}

Here is how I start the camera:

var btnTake = FindViewById<AppCompatButton>(Resource.Id.btnTake);
btnTake.Click += (s, e) => {
    var i = new Intent(MediaStore.IntentActionStillImageCamera);
    var r = i.ResolveActivity(this.PackageManager);
    if (r != null) {
        StartActivity(i);
    }
};

The problem with the above is that it works only on the first attempt, which means I click on my app's button, the built-in camera starts. When I take a picture, the picture is saved and copied to my app's cache dir, the built-in camera's Activity disappears and my Activity appears.
But on the second attempt, my activity does not appear and the bulti-in camera's Activity stays on top.
Any idea, how to hide or close the built-in camera's Activity?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人问我粥可暖 2025-02-03 07:03:30

感谢@davidwasser终于有效!!!

解决方案是从其 oncreate()中添加新的虚拟 castive finish(),并最终从广播接收者的 onreceive( )

var i = new Intent(context, typeof(Dummy));
context.StartActivity(i);

Thanks to @DavidWasser finally it works!!!

Solution is to add a new dummy activity calling Finish() from its OnCreate() and finally call following from broadcast receiver's OnReceive():

var i = new Intent(context, typeof(Dummy));
context.StartActivity(i);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文