Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android一些系統相關的東西

android一些系統相關的東西

編輯:關於Android編程

增加快捷方式和刪除快捷方式:

private void addShortcut() {
        Intent shortcut = new Intent(
                "com.android.launcher.action.INSTALL_SHORTCUT");

        // 快捷方式的名稱
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
                getString(R.string.app_name));
        shortcut.putExtra("duplicate", false); // 不允許重復創建

        // 指定當前的Activity為快捷方式啟動的對象
        ComponentName comp = new ComponentName(this.getPackageName(),
                getClass().getName());
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
                Intent.ACTION_MAIN).setComponent(comp));

        // 快捷方式的圖標
        ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
                this, R.drawable.icon);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

        sendBroadcast(shortcut);
    }
    
    /** 
     * 刪除程序的快捷方式。
     */ 
    private void deleteShortcuts() {
        Intent shortcut = new Intent(
                "com.android.launcher.action.UNINSTALL_SHORTCUT");

        // 快捷方式的名稱
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
                getString(R.string.app_name));

        // 指定當前的Activity為快捷方式啟動的對象
        ComponentName comp = new ComponentName(this.getPackageName(),
                getClass().getName());
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
                Intent.ACTION_MAIN).setComponent(comp));

        sendBroadcast(shortcut);
    }

發郵件:

	public boolean sendEmail(String to[], String subject, String body,
			String attachementFilePath) {
		final Intent emailIntent = new Intent(
				android.content.Intent.ACTION_SEND);
		emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, to);
		emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
		emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
		if (attachementFilePath != null) {
			Uri attachmentUri = null;
			try {
				File file = new File(attachementFilePath);
				if (file == null) {
					Log.d("[RC] Mail", "File error: " + attachementFilePath);
				} else if (!file.exists()) {
					Log.d("[RC] Mail", "File does not exist: "
							+ attachementFilePath);
				} else if (!file.canRead()) {
					Log.d("[RC] Mail", "File can't be read: "
							+ attachementFilePath);
				} else if (!file.isFile()) {
					Log.d("[RC] Mail", "Invalid file: " + attachementFilePath);
				} else {
					attachmentUri = Uri.fromFile(file);
					Log.d("[RC] Mail", "Attachement path[size=" + file.length()
							+ "]: " + attachementFilePath);
					Log.d("[RC] Mail",
							"Attachement URI: " + attachmentUri.toString());
				}
			} catch (java.lang.Throwable ex) {
				Log.e("[RC] Mail", "Error: " + ex.toString());
			}

			if (attachmentUri != null) {
				emailIntent.putExtra(Intent.EXTRA_STREAM, attachmentUri);
			}
		}
		emailIntent.setType(PLAIN_TEXT);
		List availableSoft = (List) mContext
				.getPackageManager().queryIntentActivities(emailIntent,
						PackageManager.MATCH_DEFAULT_ONLY);
		if (availableSoft.size() <= 0) {
			return false;
		}
		mContext.startActivity(Intent.createChooser(emailIntent, mContext
				.getResources().getString(R.string.menu_sendEmail)));

		return true;
	}

默認使用Google chrome打開WebView:

//new Intent(Intent.ACTION_VIEW, uri)
	public void startActiviyByChromeIfExists(Context context,
			Intent intent) {

		try {
			Log.d("startActiviyByChromeIfExists",
					"Intent Scheme: " + intent.getScheme());
		} catch (Exception e) {
		}

		if (context != null && intent != null) {
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			List availableSoft = (List) context
					.getPackageManager().queryIntentActivities(intent,
							PackageManager.MATCH_DEFAULT_ONLY);
			for (ResolveInfo info : availableSoft) {
				if ("com.android.chrome".equals(info.activityInfo.packageName)) {
					intent.setComponent(new ComponentName(
							info.activityInfo.packageName,
							info.activityInfo.name));
					context.startActivity(intent);
					return;
				}
			}
			if (availableSoft.size() == 0) {
				try {
					Toast.makeText(mContext, R.string.setting_no_browser_installed, Toast.LENGTH_LONG).show();
				} catch (Exception e) {
					Log.e("startActiviyByChromeIfExists", e.getMessage());
				}

			} else {
				context.startActivity(intent);
			}
		}
	}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved