14个简单有用的android源码,适合初学者

来源:互联网 发布:国际网络安全形势 编辑:程序博客网 时间:2024/05/29 23:24

1:查看是否有存储卡插入

[java] view plain copy
 print?
  1. String status=Environment.getExternalStorageState();  
  2. if(status.equals(Enviroment.MEDIA_MOUNTED))  
  3. {  
  4.    ;//说明有SD卡插入  
  5. }  

2:让某个Activity透明

在OnCreate 中不设Layout,然后this.setTheme(R.style.Theme_Transparent);

3:获取屏幕宽高

[java] view plain copy
 print?
  1. DisplayMetrics dm = new DisplayMetrics();    
  2. //获取窗口属性  
  3. getWindowManager().getDefaultDisplay().getMetrics(dm);      
  4. int screenWidth = dm.widthPixels;    
  5. int screenHeight = dm.heightPixels;  

4:发送短信

[java] view plain copy
 print?
  1. String body=”this is mms demo”;  
  2. Intent mmsIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(”smsto”, number, null));  
  3. mmsIntent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);  
  4. mmsIntent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, true);  
  5. mmsIntent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, true);  
  6. startActivity(mmsIntent);  

   5:发送彩信

[java] view plain copy
 print?
  1. StringBuilder sb = new StringBuilder();  
  2.             sb.append(”file://”);  
  3.             sb.append(fd.getAbsoluteFile());  
  4.             Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(”mmsto”, number, null));  
  5.             // Below extra datas are all optional.  
  6.             intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_SUBJECT, subject);  
  7.             intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);  
  8.             intent.putExtra(Messaging.KEY_ACTION_SENDTO_CONTENT_URI, sb.toString());  
  9.             intent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, composeMode);  
  10.             intent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, exitOnSent);  
  11.             startActivity(intent);  

7:发送Mail

[java] view plain copy
 print?
  1. mime = “img/jpg”;  
  2.          shareIntent.setDataAndType(Uri.fromFile(fd), mime);  
  3.          shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fd));  
  4.          shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);  
  5.          shareIntent.putExtra(Intent.EXTRA_TEXT, body);  

8:注册一个 BroadcastReceiver

[java] view plain copy
 print?
  1. registerReceiver(mMasterResetReciever, new IntentFilter(”oms.action.MASTERRESET”));  
  2. private BroadcastReceiver mMasterResetReciever = new BroadcastReceiver() {  
  3.         public void onReceive(Context context, Intent intent){  
  4.             String action = intent.getAction();  
  5.             if(”oms.action.MASTERRESET”.equals(action)){  
  6.                 RecoverDefaultConfig();  
  7.             }  
  8.         }  
  9.     };  

9:定义ContentObserver,监听某个数据表

[java] view plain copy
 print?
  1. private ContentObserver mDownloadsObserver = new DownloadsChangeObserver(Downloads.CONTENT_URI);  
  2. private class DownloadsChangeObserver extends ContentObserver {  
  3.         public DownloadsChangeObserver(Uri uri) {  
  4.             super(new Handler());  
  5.         }  
  6.         @Override  
  7.         public void onChange(boolean selfChange) {}    
  8.         }  

10:获得 手机UA

[java] view plain copy
 print?
  1. public String getUserAgent() {   
  2.   
  3. String user_agent = ProductProperties.get(ProductProperties.USER_AGENT_KEY, null);  
  4.   
  5. return user_agent;   
  6.   
  7. }  

11:清空手机上Cookie

[java] view plain copy
 print?
  1. CookieSyncManager.createInstance(getApplicationContext());   
  2.   
  3. CookieManager.getInstance().removeAllCookie();  

12:建立GPRS 连接

[java] view plain copy
 print?
  1. private boolean openDataConnection() {   
  2.   
  3. DataConnection conn = DataConnection.getInstance();  
  4.   
  5.  if (connectMode == 0) {   
  6.   
  7.   ret = conn.openConnection(mContext, “cmwap”, “cmwap”, “cmwap”);  
  8.   
  9.  } else {   
  10.   
  11.   ret = conn.openConnection(mContext, “cmnet”, “”, “”);   
  12.   
  13. }  
  14.   
  15. }  

13:PreferenceActivity 用法

[java] view plain copy
 print?
  1. public class Setting extends PreferenceActivity{  
  2.   
  3.  public void onCreate(Bundle savedInstanceState) {   
  4.   
  5.  super.onCreate(savedInstanceState);  
  6.   
  7.  addPreferencesFromResource(R.xml.settings);  
  8.   
  9.  }  
  10.   
  11. }  

Setting.xml:

[html] view plain copy
 print?
  1. android:key=”seting2″   
  2.   
  3. android:title=”@string/seting2″   
  4.   
  5. android:summary=”@string/seting2″/>   
  6.   
  7. android:key=”seting1″   
  8.   
  9. android:title=”@string/seting1″   
  10.   
  11. android:summaryOff=”@string/seting1summaryOff”   
  12.   
  13. android:summaryOn=”@stringseting1summaryOff”/>  

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

[java] view plain copy
 print?
  1.  DefaultHttpClient httpClient = new DefaultHttpClient();   
  2.   
  3. HttpGet method = new HttpGet(“http://www.baidu.com/1.html”);   
  4.   
  5. HttpResponse resp;   
  6.   
  7. Reader reader = null;   
  8.   
  9. try {   
  10.   
  11. // AllClientPNames.TIMEOUT   
  12.   
  13. HttpParams params = new BasicHttpParams();   
  14.   
  15. params.setIntParameter(AllClientPNames.CONNECTION_TIMEOUT, 10000);   
  16.   
  17. httpClient.setParams(params);  
  18.   
  19. resp = httpClient.execute(method);   
  20.   
  21. int status = resp.getStatusLine().getStatusCode();   
  22.   
  23. if (status != HttpStatus.SC_OK)  
  24.   
  25.  return false;  
  26.   
  27.  // HttpStatus.SC_OK;  
  28.   
  29.  return true;  
  30.   
  31.  } catch (ClientProtocolException e) {   
  32.   
  33. // TODO Auto-generated catch block   
  34.   
  35. e.printStackTrace();   
  36.   
  37. catch (IOException e) {   
  38.   
  39. // TODO Auto-generated catch block e.printStackTrace();  
  40.   
  41.  } finally {   
  42.   
  43. if (reader != null)   
  44.   
  45. try {  
  46.   
  47.  reader.close();   
  48.   
  49. catch (IOException e) {   
  50.   
  51. // TODO Auto-generated   
  52.   
  53. catch block e.printStackTrace();  
  54.   
  55.  }   
  56.   
  57. }  
0 0
原创粉丝点击