Download from market

Description

The following code shows how to download app from market.

Example

Layout xml



<?xml version="1.0" encoding="utf-8"?>
<!--
  Copyright (c) 2012 Manning
  See the file license.txt for copying permission.
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="main content" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:onClick="onLayarClick"
        android:text="main layar" />

</RelativeLayout>

Java code


//from   www  .ja  v  a 2  s . com
/*******************************************************************************
 * Copyright (c) 2012 Manning
 * See the file license.txt for copying permission.
 ******************************************************************************/

import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }

  public void onLayarClick(View v) {
    if (!ActivityHelper.isLayarAvailable(this)) {
      ActivityHelper.showDownloadDialog(this);
    } else {
      Intent intent = new Intent();
      intent.setAction(Intent.ACTION_VIEW);
      Uri uri = Uri.parse("layar://teather/?action=refresh");
      intent.setData(uri);
      startActivity(intent);
    }

  }

}
class ActivityHelper {

  public static boolean isLayarAvailable(Context ctx) {
    PackageManager pm = ctx.getPackageManager();

    try {
      pm.getApplicationInfo("com.layar", 0);
      return true;
    } catch (PackageManager.NameNotFoundException e) {
      return false;
    }

  }

  public static AlertDialog showDownloadDialog(final Context ctx) {
    AlertDialog.Builder downloadDialog = new AlertDialog.Builder(ctx);
    downloadDialog.setTitle("Layar is not available");
    downloadDialog
        .setMessage("Do you want to download it from the market?");
    downloadDialog.setPositiveButton("Yes",
        new DialogInterface.OnClickListener() {

          @Override
          public void onClick(DialogInterface dialogInterface, int i) {
            Uri uri = Uri.parse("market://details?id=com.layar");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            try {
              ctx.startActivity(intent);
            } catch (ActivityNotFoundException anfe) {
              Toast.makeText(ctx, "Market not installed",
                  Toast.LENGTH_SHORT).show();
            }
          }

        });

    downloadDialog.setNegativeButton("No",
        new DialogInterface.OnClickListener() {

          @Override
          public void onClick(DialogInterface dialogInterface, int i) {
          }

        });

    return downloadDialog.show();
  }
}
Download from market




















Home »
  Android »
    Android Basics »




Hello Android
Resources
Activity
Calendar
Camera
Contact
Content Provider
Database
Hardware
Intent
Location
Media
Network
Notification
Preference
Sensor
Service
SMS