タス デザイン グループ

Read Article

アプリを指定してマーケットを起動する

AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar" >
        <activity
            android:name="com.example.fxos.androidtips54.MainActivity"
            android:label="@string/app_name"
            android:launchMode="singleTask" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/explain_message"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/buttonLaunchOrMarket"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="@string/button_launch_or_market" />

</LinearLayout>

MainActivity.java

package com.example.fxos.androidtips54;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {
    private static final String FIREFOX_PACKAGE = "org.mozilla.firefox";

    private static final Uri MARKET_URI = Uri.parse("market://details?id=" + FIREFOX_PACKAGE);

    PackageManager mPackageManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mPackageManager = getPackageManager();
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.buttonLaunchOrMarket:
                // set launch intent.
                Intent intent = mPackageManager.getLaunchIntentForPackage(FIREFOX_PACKAGE);

                // set start market intent if application is not installed.
                if (intent == null) {
                    intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(MARKET_URI);
                }
                startActivity(intent);
                break;
        }
    }

}

manifest.webapp

{
  "name": "start_market",
  "description": "54_start_market",
  "launch_path": "/index.html",
  "type": "certified",
  "icons": {
    "64": "/img/icon/app-icon64.png",
    "128": "/img/icon/app-icon128.png"
  },
  "developer": {
    "name": "TAS Design Group",
    "url": "http://tasdg.co.jp/"
  },
  "permissions": {
    "webapps-manage": {}
  },
  "default_locale": "ja"
}

CSS (style/style.css)

@charset "UTF-8";

html {
  height: 100%;
  font-size: 62.5%;
}

body {
  margin: 0;
  height: 100%;
  display: block;
  overflow: hidden;
  font-family: sans-serif;
}

index.html

<!DOCTYPE html>
<html lang="ja" dir="ltr">
<head>
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1">
  <meta charset="utf-8">
  <title>start market</title>
  <link rel="stylesheet" href="style/style.css">
  <script src="js/main.js"></script>
</head>
<body>
  <section>
    <header>
      <h1>start market</h1>
    </header>
    <article>To launch apps that you specify, if it is installed. Start the Marketplace if it is not already installed.</article>
    <button id='btn'>Launch/Market</button>
  </section>
</body>
</html>

main.js

window.addEventListener('load', function(){

  document.getElementById('btn').onclick = function(){

    var manager = navigator.mozApps.mgmt;
    var applist = manager.getAll();
    var installed = false;

    applist.onsuccess = function(){

      for(var i=0;i<applist.result.length;i++){
        var app = applist.result[i];
        if(app.manifestURL === 'https://marketplace.firefox.com/app/fa85e65f-14ac-4736-89da-7fc27ac8862c/manifest.webapp'){

          var a = app.launch();
          a.onsuccess = function(){
            console.log('Exec success');
          };
          a.onerror = function(){
            console.log('Exec error');
          };
          installed = true;
          break;
        }
      }

      if (!installed) {
        new MozActivity({
          name: 'view',
          data: {
            type: 'url',
            url: 'https://marketplace.firefox.com/app/notesplus'
          }
        });
      }
    };

    applist.onerror = function(){
      console.log('error');
    };

  };

});
Return Top