android編程中的瑣碎知識點匯總(5)

來源:互聯網
上載者:User

1.啟動程式無需動畫

Java代碼  
  1. myIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);//1.5的應該使用,這樣就可以沒有動畫效果了  
  2. getWindow().setWindowAnimations(1)//1.6的應該使用,這不要忘記放在activity  

 

2.SD卡建立目錄

Java代碼  
  1. File wallpaperDirectory = new File("/sdcard/Wallpaper/");  
  2. wallpaperDirectory.mkdirs();  
  3. File outputFile = new File(wallpaperDirectory, filename);  
  4. FileOutputStream fos = new FileOutputStream(outputFile);   

 注意要添加許可權哦

Xml代碼  
  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  

 

3.文字中間加橫線效果

Java代碼  
  1. priceTV.setText("價格:2.00元");     
  2. priceTV.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);    

 

4.android中使用事務操作SQLite資料庫

Java代碼  
  1. SQLiteDatabase db = ....;   
  2. db.beginTransaction();//開始事務   
  3. try {   
  4.     db.execSQL("insert into person(name, age) values(?,?)", new Object[]{"張三",   
  5.   
  6. 4});  
  7.     db.execSQL("update person set name=? where personid=?", new Object[]{"李四", 1});  
  8.     db.setTransactionSuccessful();//調用此方法會在執行到endTransaction()時提交當前事  
  9.   
  10. 務,如果不調用此方法會復原事務   
  11. } finally {   
  12.     db.endTransaction();//由事務的標誌決定是提交事務,還是復原事務   
  13. }  
  14. db.close();  

 

5. 關於簡訊類SmsMessage的疑問

Java代碼  
  1. public void onReceive(Context context, Intent intent) {  
  2.   // TODO Auto-generated method stub  
  3.   Log.d(TAG, "--->onReceive  ,SMS reach");  
  4.     
  5.   Bundle bundle = intent.getExtras();  
  6.         if (bundle != null) {  
  7.             Object[] pdus = (Object[]) bundle.get("pdus");  
  8.             SmsMessage[] messages = new SmsMessage[pdus.length];  
  9.             for (int i = 0; i < pdus.length; i++) {  
  10.                 messages = SmsMessage.createFromPdu((byte[]) pdus);  
  11.             }  
  12.            for (SmsMessage smsMessage : messages) {  
  13.                 from = smsMessage.getDisplayOriginatingAddress();  
  14.                 data = smsMessage.getDisplayMessageBody().trim();  
  15.                 Log.d(TAG, from + " " + data);  
  16.                  //處理內容  
  17.                  response(context, data);  
  18.                  }  
  19.         }  
  20. }  

 

6.android中的WebView支援多點觸控:

Java代碼  
  1. public class UsingMyWebview extends Activity {  
  2.     private WebView mWebView;  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.           
  8.         // Get Web view  
  9.         mWebView = (WebView) findViewById(R.id.MyWebview);// This is the id you gave to the WebView in the main.xml  
  10.         mWebView.getSettings().setJavaScriptEnabled(true);  
  11.         mWebView.getSettings().setSupportZoom(true);  
  12.         // Zoom Control on web (You don't need this if ROM supports Multi-Touch  
  13.         mWebView.getSettings().setBuiltInZoomControls(true);// Enable Multitouch if supported by ROM   
  14.         //Load URL  
  15.         mWebView.loadUrl("http://www.firstdroid.com/advertisement.htm");  
  16.     }  
  17. }  

 7.android程式開機自動啟動實現:

Xml代碼  
  1. <receiver android:name=".BootBroadcastReceiver">  
  2.     <intent-filter>  
  3.         <action android:name="android.intent.action.BOOT_COMPLETED" />  
  4.     </intent-filter>  
  5. </receiver>  

 在manifest檔案的application標籤中添加receiver標籤,並且android:name指定一個BroadcastReceiver,通過過濾器實現過濾開機的完成那個廣播。

Java代碼  
  1. package com.jftt.bootstart;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6.   
  7. public class BootBroadcastReceiver extends BroadcastReceiver {  
  8.     static final String ACTION = "android.intent.action.BOOT_COMPLETED";  
  9.   
  10.     @Override  
  11.     public void onReceive(Context context, Intent intent) {  
  12.         if (intent.getAction().equals(ACTION)) {  
  13.             Intent sayHelloIntent = new Intent(context, BootStart.class);//指定要開啟的activity頁面  
  14.             sayHelloIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  15.   
  16.             context.startActivity(sayHelloIntent);  
  17.         }  
  18.     }  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.