사악미소
현대마법의 공방
사악미소
전체 방문자
오늘
어제
  • 분류 전체보기
    • Document
    • JavaScript
    • jQuery
    • Spring Web
      • Spring Framework
      • Spring Boot
    • Android
      • Java Code
      • Setting
    • iOS
      • Swift Code
      • Setting
    • PHP
      • Code
      • Setting
    • AWS
    • LINUX
      • Rocky Linux
      • CentOS
    • Node.js
    • Developer Tool
    • GIT
    • MAC
    • Scraping Document
    • MariaDB
    • WYSIWYG
    • Scouter
    • Docker
    • Planning
    • 용어정리
반응형

인기 글

최근 댓글

Programmer 사악미소.
사악미소

사악미소의 현대마법의 공방

[Android] Application 설치 여부 및 확인
Android/Java Code

[Android] Application 설치 여부 및 확인

2022. 8. 5. 18:48
728x90

 

 

01. viewBing 설정

 

build.gradle 파일에 viewBinding 설정을 추가해준다.

plugins {
    id 'com.android.application'
}

android {

	~~ 이 하 생 략 ~~

    buildFeatures {
        viewBinding = true
    }
}

dependencies {

    ~~ 이 하 생 략 ~~
}

 

viewBinding 설정을 추가하였다면 Sync Now를 클릭하여 설정을 반영한다.

 

 

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.intentapplication">

    <uses-permission android:name="android.permission.INTERNET"/>
    <queries>
        <package android:name="com.android.chrome"/>
    </queries>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.JavaApplication">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

 

 

 

MainActivity.java

package com.example.intentapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.*;
import android.net.*;
import android.os.Bundle;
import android.util.*;
import android.view.*;

import com.example.intentapplication.databinding.*;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    public Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.btnExecution.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                String strAppPackage = "com.android.chrome";
                Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(strAppPackage);

                Log.d("설치여부", String.valueOf(intent));

                if(intent == null) {
                    try {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + strAppPackage)));
                    } catch (android.content.ActivityNotFoundException anfe) {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + strAppPackage)));
                    }
                } else {
                    // 설치 상태.
                    startActivity(intent);
                }
            }
        });
    }
}

 

 

 

 

 

 

 

 

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.intent.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    public Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        mContext = getApplicationContext();

        binding.btnBrowser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.naver.com"));
                startActivity(intent);
            }
        });


        binding.btnIntent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String strAppPackage = "com.android.chrome";
                Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(strAppPackage);

                Log.d("설치여부", String.valueOf(intent));

                if(intent == null) {
                    try {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + strAppPackage)));
                    } catch (android.content.ActivityNotFoundException anfe) {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + strAppPackage)));
                    }
                } else {
                    // 설치 상태.
                    startActivity(intent);
                }
            }
        });
    }
}
728x90

'Android > Java Code' 카테고리의 다른 글

[Android] Activity에서 Dark Theme 비활성화기  (0) 2023.01.04
[Android] 갤러리 이미지 가져오기 및 썸네일 생성  (0) 2022.11.10
[Android] 카메라 사진 가져오기 및 썸네일 생성  (0) 2022.11.10
[Android] setOnClickListener(this)를 이용한 버튼 클릭 이벤트  (0) 2022.11.09
[Android] 액션바에 검색기능 활성화  (0) 2022.07.19
    'Android/Java Code' 카테고리의 다른 글
    • [Android] 갤러리 이미지 가져오기 및 썸네일 생성
    • [Android] 카메라 사진 가져오기 및 썸네일 생성
    • [Android] setOnClickListener(this)를 이용한 버튼 클릭 이벤트
    • [Android] 액션바에 검색기능 활성화
    사악미소
    사악미소

    티스토리툴바