๋ค์ ๋์์ ํ๋ ๊ธฐ๋ฅ์ ๊ตฌํํด ๋ณด๊ฒ ๋ค.
(0. ์นด์นด์คํก์ ํจํค์ง๋ช
์ ์๊ธฐ ์ํด APKPURE ์์ ํจํค์ง๋ช
์ ํ๋ํ๋ค)
https://apkpure.com
Download APK on Android with Free Online APK Downloader - APKPure
V2-X: Fast & Secure VPN Enjoy the ultimate privacy , security and internet filter breaker V2-X VPN
apkpure.com
1. MainActivity.java
package com.devshin93.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Context ctx = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ctx = getApplicationContext();
Button btn = (Button) findViewById(R.id.test);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startApp();
}
});
}
private void startApp(){
// apkName : app ํจํค์ง๋ช
String apkName = "com.kakao.talk";
// PackageManager : ํ์ฌ ์ฅ์น์ ์ค์น๋ ์์ฉํ๋ก๊ทธ๋จ ํจํค์ง์ ๊ด๋ จ๋ ๋ค์ํ ์ข
๋ฅ์ ์ ๋ณด๋ฅผ ๊ฒ์ํ๋ ํด๋์ค
PackageManager pm = ctx.getPackageManager();
// ์ฑ์ด ์ค์น๋์ด ์๋์ง ํ์ธ
boolean appInstalled = false;
PackageInfo info = null;
try {
// getPackageInfo(String packageName, int flags) : ์์คํ
์ ์ค์น ๋ ์ ํ๋ฆฌ์บ์ด์
ํจํค์ง์ ๋ํ ์ ์ฒด ์ ๋ณด๋ฅผ ๊ฒ์
// PackageManager.GET_ACTIVITIES : PackageInfo ํ๋๊ทธ: ํจํค์ง ํ๋์ ๋ํ ์ ๋ณด๋ฅผ ๋ฐํ
// ํด๋น ์ฑ์ด ์ค์น๋์ด ์๋์ง ํ์ธ
info = pm.getPackageInfo(apkName, PackageManager.GET_ACTIVITIES);
// ์ฑ ์ค์น ํ๋๊ทธ ๊ฐ์ true ๋ก ์ค์
appInstalled = true;
} catch(PackageManager.NameNotFoundException e) {
// PackageManager.NameNotFoundException : ์ง์ ๋ ํจํค์ง๋ฅผ ์์คํ
์์ ์ฐพ์ ์ ์๊ฑฐ๋ ํธ์ถ์๊ฐ ์์ค ๋๋ ๋์ ํจํค์ง์ ๋ํ ์ธ๋ถ ์ ๋ณด๋ฅผ ์ฟผ๋ฆฌํ ์ ์๋ ๊ฒฝ์ฐ ๋ฐ์ํ๋ exception ์ฒ๋ฆฌ
appInstalled = false;
}
// ์ฑ์ด ์ค์น๋์ด ์์ผ๋ฉด
if(appInstalled) {
// getLaunchIntentForPackage(String packageName)
// : front-door ์กํฐ๋นํฐ์์ ์คํํ ํจํค์ง์ intent๋ฅผ ๋ฐํ. ํจํค์ง๋ฅผ ์ฐพ์ง ๋ชป ํ๋ฉด null์ ๋ฐํํ๋ค.
Intent intent = pm.getLaunchIntentForPackage(apkName);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (ctx != null)
// Activity ๋ ์ฑ ์์ ๋จ์ผ ํ๋ฉด์ ๋ํ๋. Activity ์ ์ ์ธ์คํด์ค๋ฅผ ์์ํ๋ ค๋ฉด Intent ๋ฅผ startActivity() ๋ก ์ ๋ฌํ๋ฉด ๋๋ค.
ctx.startActivity(intent);
}
// ์ฑ์ด ์ค์น๋์ด ์์ง ์์ผ๋ฉด
else {
// Uri.parse(String uriString) : ์ฃผ์ด์ง ์ธ์ฝ๋ฉ๋ URI ๋ฌธ์์ด์ ๊ตฌ๋ฌธ ๋ถ์ํ๋ Uri๋ฅผ ๋ง๋ฆ
Uri marektUri = Uri.parse("market://details?id=" + apkName);
// Intent.ACTION_VIEW : ์กํฐ๋นํฐ๊ฐ ์ฌ์ฉ์์๊ฒ ํ์ํ ์ ์๋ ์ด๋ค ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์์ ๋ startActivity() ๊ฐ ์๋ ์ธํ
ํธ์์ ์ฌ์ฉ
// ์๋ฅผ ๋ค์ด ๊ฐค๋ฌ๋ฆฌ ์ฑ์์ ๋ณผ ์ฌ์ง์ด๋ ์ง๋ ์ฑ์์ ๋ณผ ์ฃผ์ ๋ฑ์ด ์ด์ ํด๋น
Intent intent = new Intent(Intent.ACTION_VIEW, marektUri);
// Intent.FLAG_ACTIVITY_NEW_TASK : ์ค์ ๋ ๊ฒฝ์ฐ, ์ด ํ๋์ ์ด ๊ธฐ๋ก ์คํ์์ ์ ์์
์ ์์์ด ๋จ
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (ctx != null)
ctx.startActivity(intent);
}
}
}
2. activity_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start App"
/>
</RelativeLayout>
ํ
์คํธํ ๋ฒํผ ์ถ๊ฐ!
3. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.devshin93.myapplication">
<!-- ์ฌ๊ธฐ์ฌ๊ธฐ -->
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
<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.StartApp">
<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>
์์ ๊ถํ(permission) ์ถ๊ฐ!
์ฐธ๊ณ :
PackageManager ๊ด๋ จ : https://developer.android.com/reference/android/content/pm/PackageManager
PackageManager | Android Developers
developer.android.com
Intent ๊ด๋ จ : https://developer.android.com/guide/components/intents-filters
์ธํ ํธ ๋ฐ ์ธํ ํธ ํํฐ | Android ๊ฐ๋ฐ์ | Android Developers
An Intent is a messaging object you can use to request an action from another app component . Although intents facilitate communication between components in several ways, there are three fundamental use cases: An Activity represents a single screen inโฆ
developer.android.com
Intent Flag ๊ด๋ จ : https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_NEW_TASK
Intent | Android Developers
developer.android.com
๋๊ธ