1.从SMS获取联系人信息
ContactItem getContactByAddr(Context context, final SMSItem sms) { Uri personUri = Uri.withAppendedPath( ContactsContract.PhoneLookup.CONTENT_FILTER_URI, sms.mAddress); Cursor cur = context.getContentResolver().query(personUri, new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null ); if( cur.moveToFirst() ) { int nameIdx = cur.getColumnIndex(PhoneLookup.DISPLAY_NAME); ContactItem item = new ContactItem(); item.mName = cur.getString(nameIdx); cur.close(); return item; } return null; }
2.从gallery获取图片并显示
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch(requestCode) { case REQ_CODE_PICK_IMAGE: if(resultCode == RESULT_OK){ Uri selectedImage = imageReturnedIntent.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String filePath = cursor.getString(columnIndex); cursor.close(); Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath); } } }
3.service 里面使用toast
public class ConnectionService extends Service { private Handler handler = new Handler(); public void restartConnection(){ int sleepTime = reconnectCounter * MIN_RECON_WAIT; if (sleepTime > MAX_RECON_WAIT){ sleepTime = MAX_RECON_WAIT; } String msg = "The connection has been lost. Restart attempt will start in: " + sleepTime/1000 + " seconds"; (new Timer()).schedule( new TimerTask() { public void run() { handler.post(new Runnable() { public void run() { Toast.makeText(getApplicationContext(), "msg", Toast.LENGTH_LONG).show(); reconnectCounter++; this.startConnectionThread() } }); } }, sleepTime); }//end restartConnection }//end ConnectionService
4.禁用webView 放大缩小
<meta content='width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;' name='viewport' /> <meta name="viewport" content="width=device-width" />
5.margin代码实现View周围空出距离
extView tv = (TextView)findViewById(R.id.my_text_view); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)tv.getLayoutParams(); params.setMargins(0, 0, 10, 0); tv.setLayoutParams(params);
6.内部类的使用
<view class="com.android.notepad.NoteEditor$MyEditText" id="@+id/note" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:drawable/empty" android:padding="10dip" android:scrollbars="vertical" android:fadingEdge="vertical" />
7.使用本地tomcat自己搭建服务器注意点:
在模拟器上可以用10.0.2.2代替127.0.0.1和localhost 在局域网环境可以用 192.168.0.x或者192.168.1.x(根据具体配置)连接本机
8.反编译apk中的文件或者生成apk所用到的:
事先声明必须使用相关工具 反编译xml为我们可以看懂的格式 java -jar AXMLPrinter2.jar ***.xml>***.txt 发编译classes.dex到当前目录的classout中 java -jar baksmali.jar -o classout/ classes.dex 编译classout下的文件为classes.dex java -jar smali.jar classout/ -o classes.dex