안드로이드 유용한 코드 세그먼트

24987 단어
1: 메모리 카드 삽입 여부 보기
1
2
3
4
5
String status=Environment.getExternalStorageState();
if(status.equals(Enviroment.MEDIA_MOUNTED))
{
      SD   
}

2: Activity 투명성
1
2
OnCreate   Layout
this.setTheme(R.style.Theme_Transparent);

다음은 ThemeTransparent의 정의 (transparent bg는 투명한 그림입니다)
3: 화면 요소의 핸들을 Activity로 설정합니다.findViewById를 사용하여 화면에 있는 요소의 핸들을 가져옵니다.이 핸들을 사용하면 대상이 노출된 값을 설정하거나 얻을 수 있습니다.
1
2
TextView msgTextView = (TextView)findViewById(R.id.msg);
   msgTextView.setText(R.string.push_me);

4:문자 보내기
1
2
3
4
5
6
7
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:채신 발송
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
StringBuilder sb = new StringBuilder();
 
            sb.append(”file://”);
 
            sb.append(fd.getAbsoluteFile());
 
            Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(”mmsto”, number, null));
            // Below extra datas are all optional.
            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);

7: Mail 전송
1
2
3
4
5
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);

8: BroadcastReceiver 등록
1
2
3
4
5
6
7
8
9
10
11
12
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();
            }
        }
 
    };

9: ContentObserver 정의, 데이터 테이블 수신
1
2
3
4
5
6
7
8
9
10
11
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) {}

10:휴대폰 UA 획득
1
2
3
4
5
public String getUserAgent()
    {
           String user_agent = ProductProperties.get(ProductProperties.USER_AGENT_KEY, null);
            return user_agent;
    }

11:휴대폰에서 쿠키 비우기
1
2
CookieSyncManager.createInstance(getApplicationContext());
        CookieManager.getInstance().removeAllCookie();

12: GPRS 연결 설정
1
2
3
4
5
6
7
8
9
10
11
12
//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”, “”, “”);
            }
 
    }

13: Preference Activity 사용법
1
2
3
4
5
6
7
8
9
10
public class Setting extends PreferenceActivity
 
{
 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings)}
 
}
1
2
3
4
5
6
7
8
9
10
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”/>

14: HttpClient를 통해 지정된 서버에서 데이터 가져오기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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();
                }
            }

15: toast 표시
1
2
 
Toast.makeText(this._getApplicationContext(), R.string._item, Toast.LENGTH_SHORT).show();

텍스트 링크:http://jinguo.javaeye.com/blog/690825

좋은 웹페이지 즐겨찾기