/** * 删除程序的快捷方式 * * @param activity Activity */ public static void delShortcut(Activity activity) { Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT"); // 快捷方式的名称 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, activity.getString(R.string.app_name)); String appClass = activity.getPackageName() + "." + activity.getLocalClassName(); ComponentName comp = new ComponentName(activity.getPackageName(), appClass); shortcut.putExtra( Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp)); activity.sendBroadcast(shortcut); }
public int getIdFromName(String instanceName) { String res = mActivity.getPackageName() + ":id/" + instanceName; int id = mActivity.getResources().getIdentifier(res, "id", mActivity.getPackageName()); if (id == 0) { for (ViewGroup viewgrp : mViewGroup) { id = viewgrp.getResources().getIdentifier(res, "id", mActivity.getPackageName()); if (id != 0) break; } } return id; }
/** * @param activity The context (normally the UI context) * @return boolean True if successfully connected */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) private final boolean servicesConnected(Activity activity) { int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity); if (resultCode != ConnectionResult.SUCCESS) { Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog( resultCode, activity, CONNECTION_FAILURE_RESOLUTION_REQUEST); // Can Google Play service provide an error dialog if (errorDialog != null) { PackageInfo pInfo; try { pInfo = activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0); // only version 11 and above support ErrorDialogFragment if (pInfo.versionCode >= Build.VERSION_CODES.HONEYCOMB) { ErrorDialogFragment errorFragment = new ErrorDialogFragment(); // Set the dialog in the DialogFragment errorFragment.setDialog(errorDialog); // Show the error dialog in the DialogFragment errorFragment.show(activity.getFragmentManager(), serviceDescription_); } } catch (NameNotFoundException e) { Log.w(serviceDescription_, "Unable to determine version", e); } } else { Log.e(serviceDescription_, "Failed to get Map Service" + resultCode); } return false; } else { Log.d(serviceDescription_, "Google Play services is available."); return true; } }
@Override public void onActivityStarted(Activity activity) { if (activitiesOnRotation.contains(activity.getPackageName() + activity.getLocalClassName())) { activitiesOnRotation.remove(activity.getPackageName() + activity.getLocalClassName()); } else { if (activityStack.empty() && !activity.isChangingConfigurations()) { appWillReturnfromBackground(); } } this.activityStack.push(new ActivityLifecyleWrapper(activity, true, false)); }
public static String getInfosAboutDevice(Activity a) { String s = ""; try { PackageInfo pInfo = a.getPackageManager().getPackageInfo(a.getPackageName(), PackageManager.GET_META_DATA); s += "\n APP Package Name: " + a.getPackageName(); s += "\n APP Version Name: " + pInfo.versionName; s += "\n APP Version Code: " + pInfo.versionCode; s += "\n"; } catch (NameNotFoundException e) { } s += "\n OS Version: " + System.getProperty("os.version") + " (" + android.os.Build.VERSION.INCREMENTAL + ")"; s += "\n OS API Level: " + android.os.Build.VERSION.SDK; s += "\n Device: " + android.os.Build.DEVICE; s += "\n Model (and Product): " + android.os.Build.MODEL + " (" + android.os.Build.PRODUCT + ")"; // TODO add application version! // more from // http://developer.android.com/reference/android/os/Build.html : s += "\n Manufacturer: " + android.os.Build.MANUFACTURER; s += "\n Other TAGS: " + android.os.Build.TAGS; s += "\n screenWidth: " + a.getWindow().getWindowManager().getDefaultDisplay().getWidth(); s += "\n screenHeigth: " + a.getWindow().getWindowManager().getDefaultDisplay().getHeight(); s += "\n Keyboard available: " + (a.getResources().getConfiguration().keyboard != Configuration.KEYBOARD_NOKEYS); s += "\n Trackball available: " + (a.getResources().getConfiguration().navigation == Configuration.NAVIGATION_TRACKBALL); s += "\n SD Card state: " + Environment.getExternalStorageState(); Properties p = System.getProperties(); Enumeration keys = p.keys(); String key = ""; while (keys.hasMoreElements()) { key = (String) keys.nextElement(); s += "\n > " + key + " = " + (String) p.get(key); } return s; }
@Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (mActivity == null) { return false; } final Uri uri = Uri.parse(url); if (Utils.divertMailtoUri(mActivity, uri, mAccount)) { return true; } final Intent intent; if (mAccount != null && !Utils.isEmpty(mAccount.viewIntentProxyUri)) { intent = generateProxyIntent(uri); } else { intent = new Intent(Intent.ACTION_VIEW, uri); intent.putExtra(Browser.EXTRA_APPLICATION_ID, mActivity.getPackageName()); intent.putExtra(Browser.EXTRA_CREATE_NEW_TAB, true); } boolean result = false; try { intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET | Intent.FLAG_ACTIVITY_NO_ANIMATION); mActivity.startActivity(intent); result = true; } catch (ActivityNotFoundException ex) { // If no application can handle the URL, assume that the // caller can handle it. } return result; }
/** * Provides the key hash to solve the openSSL issue with Amazon * * @return key hash */ @TargetApi(Build.VERSION_CODES.FROYO) public static String getKeyHash() { try { // In some cases the unity activity may not exist. This can happen when we are // completing a login and unity activity was killed in the background. In this // situation it's not necessary to send back the keyhash since the app will overwrite // the value with the value they get during the init call and the unity activity // wil be created by the time init is called. Activity activity = getUnityActivity(); if (activity == null) { return ""; } PackageInfo info = activity .getPackageManager() .getPackageInfo(activity.getPackageName(), PackageManager.GET_SIGNATURES); for (Signature signature : info.signatures) { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(signature.toByteArray()); String keyHash = Base64.encodeToString(md.digest(), Base64.DEFAULT); Log.d(TAG, "KeyHash: " + keyHash); return keyHash; } } catch (NameNotFoundException e) { } catch (NoSuchAlgorithmException e) { } return ""; }
public void testInvalidLoginActivity() throws Exception { // Unlock the lock screen. solo.unlockScreen(); // Assert that LoginActivity is opened. Activity currentAcctivity = solo.getCurrentActivity(); solo.assertCurrentActivity("Expected LoginActivity", "LoginActivity"); solo.sleep(3000); // if user session isLogin = solo.waitForActivity("LoginActivity"); if (!isLogin) { solo.clickOnMenuItem("Logout", true); } // Enter a valid Email and password solo.enterText((EditText) solo.getView("email_address"), "*****@*****.**"); solo.enterText((EditText) solo.getView("password"), "wrongPassword"); // Click on login button int id = currentAcctivity .getResources() .getIdentifier("btn_login", "id", currentAcctivity.getPackageName()); Button login_btn = (Button) solo.getView(id); solo.clickOnView(login_btn); // Check if login solo.assertCurrentActivity("Invalid user can not log in, test pass!", LoginActivity.class); solo.sleep(2000); isLogin = solo.waitForActivity("LoginActivity"); if (!isLogin) { solo.clickOnMenuItem("Logout", true); } }
public void testValidLoginActivity() throws Exception { // Unlock the lock screen. solo.unlockScreen(); // Assert that LoginActivity is opened. solo.assertCurrentActivity("Expected LoginActivity", "LoginActivity"); // Enter a valid Email and password solo.enterText((EditText) solo.getView("email_address"), "*****@*****.**"); solo.enterText((EditText) solo.getView("password"), "abc123456"); // Click on login button Activity currentAcctivity = solo.getCurrentActivity(); int id = currentAcctivity .getResources() .getIdentifier("btn_login", "id", currentAcctivity.getPackageName()); Button login_btn = (Button) solo.getView(id); solo.clickOnView(login_btn); System.out.print(solo.getCurrentActivity()); // Activity activity = solo.getCurrentActivity(); // boolean authSuceess = !activity.equals(LoginActivity.class); // assertTrue("Sucessfully log in", authSuceess); solo.sleep(1000); System.out.print(solo.getCurrentActivity()); solo.clickOnMenuItem("Logout", true); ; solo.sleep(300); }
/** * Clears the distribution pref to return distribution state to STATE_UNKNOWN, and wipes the * in-memory referrer pigeonhole. */ private void clearDistributionPref() { mAsserter.dumpLog("Clearing distribution pref."); SharedPreferences settings = mActivity.getSharedPreferences("GoannaApp", Activity.MODE_PRIVATE); String keyName = mActivity.getPackageName() + ".distribution_state"; settings.edit().remove(keyName).commit(); TestableDistribution.clearReferrerDescriptorForTesting(); }
/* * Get the resource from Fragment arguments and return it. * * If no resource can be found, return the resource id of the default preference screen. */ private int getResource() { int resid = 0; final String resourceName = getArguments().getString("resource"); final Activity activity = getActivity(); if (resourceName != null) { // Fetch resource id by resource name. final Resources resources = activity.getResources(); final String packageName = activity.getPackageName(); resid = resources.getIdentifier(resourceName, "xml", packageName); } if (resid == 0) { // The resource was invalid. Use the default resource. Log.e(LOGTAG, "Failed to find resource: " + resourceName + ". Displaying default settings."); boolean isMultiPane = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) && ((PreferenceActivity) activity).isMultiPane(); resid = isMultiPane ? R.xml.preferences_customize_tablet : R.xml.preferences; } return resid; }
@Override public void setUp() throws Exception { super.setUp(); activity = mock(Activity.class); when(activity.getPackageName()).thenReturn("com.example"); builder = Crop.of(Uri.parse("image:input"), Uri.parse("image:output")); }
private void reflashData() { SessionContext sessionContext = new SessionContext( mActivity.getResources().getIdentifier("icon", "drawable", mActivity.getPackageName()), "Mr King", "不一样的烟火"); datas.add(0, sessionContext); }
private String getVersion() { try { PackageInfo p = mActivity.getPackageManager().getPackageInfo(mActivity.getPackageName(), 0); return p.versionName; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return "1.0"; } }
public static String getApplicationVersion(Activity context) { String res = ""; try { PackageManager manager = context.getPackageManager(); PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0); res = info.versionName; } catch (Exception e) { } return res; }
/** @return Application's version code from the {@code PackageManager}. */ private static int getAppVersion(Activity context) { try { PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); return packageInfo.versionCode; } catch (NameNotFoundException e) { // should never happen throw new RuntimeException("Could not get package name: " + e); } }
private Drawable getIconDrawableResourceId(Activity activity) { try { PackageManager pm = activity.getPackageManager(); PackageInfo pi = pm.getPackageInfo(activity.getPackageName(), 0); return activity.getResources().getDrawable(pi.applicationInfo.icon); } catch (Exception e) { Log.e("Error loading app icon.", e); } return null; }
@Override public boolean willChromeHandleIntent(Intent intent) { try { ResolveInfo info = mActivity.getPackageManager().resolveActivity(intent, 0); return info != null && info.activityInfo.packageName.equals(mActivity.getPackageName()); } catch (RuntimeException e) { logTransactionTooLargeOrRethrow(e, intent); return false; } }
// Get the current app version private String GetAppVersion() { try { PackageInfo _info = fActivity.getPackageManager().getPackageInfo(fActivity.getPackageName(), 0); return _info.versionName; } catch (NameNotFoundException e) { e.printStackTrace(); return ""; } }
private int getVersionCode(Context context) { int versionCode = 0; try { versionCode = context.getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionCode; } catch (NameNotFoundException e) { e.printStackTrace(); } return versionCode; }
private String getAppMetaData(String name) { try { PackageManager packageManager = mActivity.getPackageManager(); ApplicationInfo appInfo = packageManager.getApplicationInfo( mActivity.getPackageName(), PackageManager.GET_META_DATA); return appInfo.metaData.getString(name); } catch (NameNotFoundException | NullPointerException e) { } return null; }
public void loadFaceImageView(Activity activity, int imageViewId) { String currentPictureName = String.format(RES_IMAGE_FORMAT, index); Log.i(TAG, "Loading image with name: " + currentPictureName); int id = activity .getResources() .getIdentifier(currentPictureName, "drawable", activity.getPackageName()); ImageView image = (ImageView) activity.findViewById(imageViewId); image.setImageResource(id); }
private void getIcon(ImageView imageView, String icon) { int resId = activity.getResources().getIdentifier("i" + icon, "drawable", activity.getPackageName()); if (resId != 0) { imageView.setBackgroundResource(resId); } else { Log.d(TAG, "Icon not exist " + icon); new IconForecast(imageView, icon); } }
/** * Returns the app's name. * * @return The app's name as a String. */ public String getAppName() { Activity activity = getActivity(); try { PackageManager pm = activity.getPackageManager(); ApplicationInfo applicationInfo = pm.getApplicationInfo(activity.getPackageName(), 0); return pm.getApplicationLabel(applicationInfo).toString(); } catch (NameNotFoundException exception) { return ""; } }
@Override public void onAttach(final Activity activity) { super.onAttach(activity); this.activity = (AbstractWalletActivity) activity; this.application = (WalletApplication) activity.getApplication(); this.config = application.getConfiguration(); this.wallet = application.getWallet(); this.contentUri = ExchangeRatesProvider.contentUri(activity.getPackageName(), false); this.loaderManager = getLoaderManager(); }
/** �˳�Ӧ�ó��� */ public void AppExit(Activity activity) { try { finishAllActivity(); activity.finish(); android.os.Process.killProcess(android.os.Process.myPid()); ActivityManager manager = (ActivityManager) activity.getSystemService(Activity.ACTIVITY_SERVICE); manager.killBackgroundProcesses(activity.getPackageName()); System.exit(0); // 退出程序 } catch (Exception e) { } }
private PackageInfo getPackageInfo() { PackageInfo info = null; try { info = mContext .getPackageManager() .getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return info; }
private void gotoStore() { final String appPackageName = hostActivity.getPackageName(); // from Context or Activity object try { hostActivity.startActivity( new Intent( Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName))); } catch (android.content.ActivityNotFoundException anfe) { hostActivity.startActivity( new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName))); } }
private PackageInfo getPackageInfo() { PackageInfo pi = null; try { pi = mActivity .getPackageManager() .getPackageInfo(mActivity.getPackageName(), PackageManager.GET_ACTIVITIES); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return pi; }
static String appSignature() { Activity act = $.act(); try { return act.getPackageManager() .getPackageInfo(act.getPackageName(), PackageManager.GET_SIGNATURES) .signatures[0] .toCharsString(); } catch (NameNotFoundException e) { return null; } }