龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 移动开发 > Android开发 >

Android实用的代码片段 常用代码总结(2)

时间:2014-09-22 11:51来源:网络整理 作者:网络 点击:
分享到:
复制代码 代码如下: private ContentObserver mDownloadsObserver = new DownloadsChangeObserver(Downloads.CONTENT_URI); private class DownloadsChangeObserver extends ContentObserver { public Downl

复制代码 代码如下:

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:获得 手机UA

复制代码 代码如下:

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

10:清空手机上Cookie

复制代码 代码如下:

CookieSyncManager.createInstance(getApplicationContext());
        CookieManager.getInstance().removeAllCookie();11:建立GPRS连接


 //Dial the GPRS link.
    private boolean openDataConnection() {
        // Set up data connection.
        DataConnection conn = DataConnection.getInstance();    
            if (connectMode == 0) {
                ret = conn.openConnection(mContext, "cmwap", "cmwap", "cmwap");
            } else {
                ret = conn.openConnection(mContext, "cmnet", "", "");
            }
    }

12:PreferenceActivity 用法

复制代码 代码如下:

public class Setting extends PreferenceActivity

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
    }

Setting.xml:
            android:key="seting2″
            android:title="@string/seting2″
            android:summary="@string/seting2″/>
            android:key="seting1″
            android:title="@string/seting1″
            android:summaryOff="@string/seting1summaryOff"
            android:summaryOn="@stringseting1summaryOff"/>

13:通过HttpClient从指定server获取数据

复制代码 代码如下:

DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet method = new HttpGet("http://www.baidu.com/1.html");
            HttpResponse resp;
            Reader reader = null;
            try {
                // AllClientPNames.TIMEOUT
                HttpParams params = new BasicHttpParams();
                params.setIntParameter(AllClientPNames.CONNECTION_TIMEOUT, 10000);
                httpClient.setParams(params);
                resp = httpClient.execute(method);
                int status = resp.getStatusLine().getStatusCode();
                if (status != HttpStatus.SC_OK) return false;
                // HttpStatus.SC_OK;
                return true;
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (reader != null) try {
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

精彩图集

赞助商链接