728x90
λ€μ λμμ νλ κΈ°λ₯μ ꡬνν΄ λ³΄κ² λ€.
(0. μΉ΄μΉ΄μ€ν‘μ ν¨ν€μ§λͺ
μ μκΈ° μν΄ APKPURE μμ ν¨ν€μ§λͺ
μ νλνλ€)
https://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
Intent κ΄λ ¨ : https://developer.android.com/guide/components/intents-filters
Intent Flag κ΄λ ¨ : https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_NEW_TASK
λκΈ