android一些系統相關的東西

來源:互聯網
上載者:User

標籤: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);}}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.