Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 常用程式碼片段整理:SD 卡、簡訊、彩信、郵件與廣播

開發過程中有些片段實在太常寫,與其每次翻舊專案,不如集中收錄。以下依功能分類整理,都是可以直接複製去改的骨架。

1. 判斷是否插入記憶卡

String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {
    // 有 SD 卡
}

注意常數拼字是 Environment,寫成 Enviroment 是常見的手誤。

2. 讓某個 Activity 透明

在 onCreate() 中不要設定 Layout,改套用自訂主題:

this.setTheme(R.style.Theme_Transparent);

主題定義中需搭配一張全透明的背景圖作為 transparent_bg。

3. 取得畫面元件的句柄

用 Activity.findViewById() 取得元件實例,之後即可讀寫該物件對外開放的屬性:

TextView msgTextView = (TextView) findViewById(R.id.msg);
msgTextView.setText(R.string.push_me);

4. 傳送簡訊

String body = "this is mms demo";
Intent mmsintent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("smsto", number, null));
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, true);
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, true);
startActivity(mmsintent);

5. 傳送彩信

StringBuilder sb = new StringBuilder();
sb.append("file://");
sb.append(fd.getAbsoluteFile());

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mmsto", number, null));
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_SUBJECT, subject);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_CONTENT_URI, sb.toString());
intent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, composeMode);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, exitOnSent);
startActivity(intent);

6. 分享/寄送郵件

mime = "img/jpg";
shareIntent.setDataAndType(Uri.fromFile(fd), mime);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fd));
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
shareIntent.putExtra(Intent.EXTRA_TEXT, body);

7. 註冊 BroadcastReceiver

registerReceiver(mMasterResetReciever, new IntentFilter("oms.action.MASTERRESET"));

private BroadcastReceiver mMasterResetReciever = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if ("oms.action.MASTERRESET".equals(action)) {
            RecoverDefaultConfig();
        }
    }
};

8. 用 ContentObserver 監聽資料表

private ContentObserver mDownloadsObserver = new DownloadsChangeObserver(Downloads.CONTENT_URI);

private class DownloadsChangeObserver extends ContentObserver {
    public DownloadsChangeObserver(Uri uri) {
        super(new Handler());
    }
    @Override
    public void onChange(boolean selfChange) { }
}

9. 取得手機 User-Agent

public String getUserAgent() {
    String user_agent = ProductProperties.get(ProductProperties.USER_AGENT_KEY, null);
    return user_agent;
}

10. 清除 Cookie

CookieSyncManager.createInstance(getApplicationContext());
CookieManager.getInstance().removeAllCookie();

使用提醒

這些片段多為早期 API 的寫法,實際套用時請留意兩件事:一是 registerReceiver 註冊後必須在適當時機 unregister,否則會洩漏;二是傳送簡訊與彩信這類片段多半只是叫起系統的編輯界面,若要背景直接送達,需要 SMSManager 與相應權限,而高版本 Android 對背景簡訊的限制也更嚴格。

Copyright © Linux教程網 All Rights Reserved