標籤: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<ResolveInfo> availableSoft = (List<ResolveInfo>) 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<ResolveInfo> availableSoft = (List<ResolveInfo>) 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);}}}