Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;

import android.net.Uri;
import android.os.Bundle;

import android.util.Log;

public class Main {
    public static boolean startActivityUsingScheme(Activity a, String scheme, Bundle args) {
        Uri uri = Uri.parse(scheme + "://");
        Intent intent = new Intent(Intent.ACTION_RUN, uri);
        boolean result = true;
        try {
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            if (args != null)
                intent.putExtras(args);
            a.startActivity(intent);
        } catch (Exception e) {
            Log.e(a.getClass().getName(), e.getMessage(), e);
            result = false;
        }
        return result;
    }

    public static boolean startActivityUsingScheme(Context a, String scheme) {
        Uri uri = Uri.parse(scheme + "://");
        Intent intent = new Intent(Intent.ACTION_RUN, uri);
        boolean result = true;
        try {
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            a.startActivity(intent);
        } catch (Exception e) {
            Log.e(a.getClass().getName(), e.getMessage(), e);
            result = false;
        }
        return result;
    }

    public static boolean startActivity(Context a, String packageName) {
        PackageManager pm = a.getPackageManager();
        boolean result = true;
        try {
            Intent intent = pm.getLaunchIntentForPackage(packageName);
            if (intent != null) {
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                //         intent.addCategory(Intent.CATEGORY_LAUNCHER);
                a.startActivity(intent);
            } else {
                result = startActivityUsingScheme(a, packageName);
            }
        } catch (Exception e) {
            Log.e(a.getClass().getName(), e.getMessage(), e);
            result = startActivityUsingScheme(a, packageName);
        }
        return result;
    }

    public static boolean startActivity(Activity a, String packageName, Bundle args) {
        PackageManager pm = a.getPackageManager();
        boolean result = true;
        try {
            Intent intent = pm.getLaunchIntentForPackage(packageName);
            if (null != intent) {
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                if (args != null)
                    intent.putExtras(args);
                a.startActivity(intent);
            } else {
                result = startActivityUsingScheme(a, packageName, args);
            }
        } catch (Exception e) {
            Log.e(a.getClass().getName(), e.getMessage(), e);
            result = startActivityUsingScheme(a, packageName, args);
        }
        return result;
    }

    public static boolean startActivity(Activity a, String packageName, Bundle args, int flags) {
        PackageManager pm = a.getPackageManager();
        boolean result = true;
        try {
            Intent intent = pm.getLaunchIntentForPackage(packageName);
            if (null != intent) {
                intent.addFlags(flags);
                if (args != null)
                    intent.putExtras(args);
                a.startActivity(intent);
            }
        } catch (Exception e) {
            Log.e(a.getClass().getName(), e.getMessage(), e);
            result = false;
        }
        return result;
    }
}