如何在 Android 应用中打开 Google Play 商店?


简介

在 Android 应用中,我们经常会看到用户身份验证收到的 OTP 会自动检测并自动添加到应用中。用户无需手动添加 OTP。这可以通过广播接收器在 Android 应用中实现。本文将介绍如何在 Android 应用中监听传入的短信。

实现

我们将创建一个简单的应用,其中我们将显示两个 TextView。第一个 TextView 用于显示应用标题。第二个 TextView 将用于显示收到的短信。现在让我们转到 Android Studio 创建一个新项目。

步骤 1:在 Android Studio 中创建一个新项目

导航到 Android Studio,如下面的屏幕所示。在下面的屏幕中,点击“新建项目”以创建一个新的 Android Studio 项目。

点击“新建项目”后,您将看到下面的屏幕。

在这个屏幕中,我们只需选择“空活动”并点击“下一步”。点击“下一步”后,您将看到下面的屏幕。

在这个屏幕中,我们只需指定项目名称。然后包名将自动生成。

注意:请确保选择 Java 作为语言。

指定所有详细信息后,点击“完成”以创建一个新的 Android Studio 项目。

项目创建完成后,我们将看到两个打开的文件,即 activity_main.xml 和 MainActivity.java 文件。

步骤 2:使用 activity_main.xml

导航到 activity_main.xml。如果此文件不可见,要打开此文件,请在左侧窗格中导航到 app>res>layout>activity_main.xml 以打开此文件。打开此文件后,将以下代码添加到其中。代码中添加了注释以详细了解。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">
   <!--creating a text view on below line-->
   <TextView
       android:id="@+id/idTVHeading"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_centerInParent="true"
       android:layout_marginStart="20dp"
       android:layout_marginTop="20dp"
       android:layout_marginEnd="20dp"
       android:layout_marginBottom="20dp"
       android:padding="4dp"
       android:text="Listen Incoming SMS Message in Android"
       android:textAlignment="center"
       android:textColor="@color/black"
       android:textSize="20sp"
       android:textStyle="bold" />
   <!--creating a text view on below line-->
   <TextView
       android:id="@+id/idTVMessage"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/idTVHeading"
       android:layout_centerInParent="true"
       android:layout_marginStart="20dp"
       android:layout_marginTop="20dp"
       android:layout_marginEnd="20dp"
       android:layout_marginBottom="20dp"
       android:padding="4dp"
       android:text="Message will appear here"
       android:textAlignment="center"
       android:textColor="@color/black"
       android:textSize="20sp"
       android:textStyle="bold" />
</RelativeLayout>

说明:在上面的代码中,我们创建了一个根布局作为相对布局。在此布局中,我们创建一个 TextView 用于显示应用的标题。然后,我们创建一个按钮,点击该按钮将打开 Google Play 商店应用。现在让我们转到 MainActivity.java 文件为该按钮添加功能。

步骤 3:使用 MainActivity.java 文件

导航到 MainActivity.java。如果此文件不可见,要打开此文件,请在左侧窗格中导航到 app>res>layout>MainActivity.java 以打开此文件。打开此文件后,将以下代码添加到其中。代码中添加了注释以详细了解。

package com.example.java_test_application;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
   // creating variable for button on below line.
   private Button openPlayBtn;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       // initializing variables for button on below line.
       openPlayBtn = findViewById(R.id.idBtnOpenGooglePlay);
       // adding click listener for button on below line.
       openPlayBtn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               // on below line we are creating the uri to open google play store to open google maps application
               Uri uri = Uri.parse("https://play.google.com/store/apps/details?id=com.google.android.apps.maps");
               // initializing intent with action view.
               Intent i = new Intent(Intent.ACTION_VIEW, uri);
               // set flags on below line.
               i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               // on below line we are starting the activity.
               startActivity(i);
           }
       });
   }
}

说明:在上面的代码中,首先我们为按钮创建变量。现在我们将看到 onCreate 方法。这是每个 Android 应用的默认方法。当应用视图创建时,将调用此方法。在此方法中,我们设置内容视图,即名为 activity_main.xml 的布局文件,以从该文件中设置 UI。在 onCreate 方法中,我们使用我们在 activity_main.xml 文件中给出的 id 初始化按钮和 EditText 的变量。然后,我们为按钮添加一个点击监听器。在点击监听器中,我们创建要打开的 URI。我们传递 Google Play 商店的网页 URL。然后,我们使用 action 作为 action_view 创建一个 Intent。然后,我们为该 Intent 设置标志。最后,我们调用 startActivity 以打开 Google Play 商店。

添加上述代码后,我们只需点击顶部的绿色图标即可在移动设备上运行我们的应用。

注意:请确保已连接到您的真实设备或模拟器。

输出

结论

在本文中,我们介绍了如何在 Android 应用中打开 Google Play 商店。

更新于: 2023 年 5 月 9 日

653 次浏览

开启您的 职业生涯

通过完成课程获得认证

开始
广告

© . All rights reserved.