【Android】开发中的小技术(持续更新)

【特】Android源码下载

 

1.WebView加载各种类型的网页

//打开本包内asset目录下的index.html文件
wView.loadUrl(" file:///android_asset/index.html ");  

//打开本地sd卡内的index.html文件
wView.loadUrl("content://com.android.htmlfileprovider/sdcard/index.html");

//打开指定URL的html文件
wView.loadUrl(" http://m.oschina.net");

 

2.在一个应用程序中启动另外一个应用程序

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);            
ComponentName cn = new ComponentName("com.android.androidweb", "com.android.androidweb.AndroidWeb"); //前面是包名,后面是主类名         
intent.setComponent(cn);
startActivity(intent);

下面记录一个很特殊的,是系统自带的相册程序:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);            
ComponentName cn = new ComponentName("com.android.gallery", "com.android.camera.GalleryPicker");            
intent.setComponent(cn);
startActivity(intent);

知道包名获取启动类的方法:

PackageManager pm = getPackageManager();
PackageInfo pi;
try {
	pi = pm.getPackageInfo("com.android.androidweb", 0);//此处加入包名~
	Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
	resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
	resolveIntent.setPackage(pi.packageName);

	List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0);

	ResolveInfo ri = apps.iterator().next();
	if (ri != null ) {
	  String packageName = ri.activityInfo.packageName;
	  String className = ri.activityInfo.name;

	  Intent intent = new Intent(Intent.ACTION_MAIN);
	  intent.addCategory(Intent.CATEGORY_LAUNCHER);

	  ComponentName cn = new ComponentName(packageName, className);

	  intent.setComponent(cn);
	  startActivity(intent);
	  }
	} catch (NameNotFoundException e) {
	  // TODO Auto-generated catch block
	  e.printStackTrace();
	}

 

6.SQLite数据库中实现ID自增

将字段声明为:INTEGER PRIMARY KEY AUTOINCREMENT

 

7.读取返回的Cursor的内容

 1 Cursor cursor = ThumbnailsImages.queryPicDirectory(getContentResolver());
 2 String display = "";
 3 cursor.moveToFirst();
 4 while(!cursor.isAfterLast()){
 5     display = display + cursor.getString(0) + "/";
 6     display = display + cursor.getString(1) + "/";
 7     cursor.moveToNext();
 8 }
 9 view.setText(display);
10 cursor.close();

 

10.文件过滤器

 1 mFiles = mDirectory.listFiles(new FilenameFilter() {
 2                     public boolean accept(File file, String name) {
 3                         if (name.toLowerCase().endsWith(".pdf"))
 4                             return true;
 5                         if (name.toLowerCase().endsWith(".xps"))
 6                             return true;
 7                         if (name.toLowerCase().endsWith(".cbz"))
 8                             return true;
 9                         return false;
10                     }
11                 });
12 
13                 Arrays.sort(mFiles, new Comparator<File>() {
14                     public int compare(File arg0, File arg1) {
15                         return arg0.getName().compareToIgnoreCase(arg1.getName());
16                     }
17                 });

 

11.操作软键盘(出现和隐藏)

 1   void showKeyboard() {
 2         InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
 3         if (imm != null)
 4                         //mSearchText是一个搜索输入框(EditText)
 5             imm.showSoftInput(mSearchText, 0);
 6     }
 7 
 8     void hideKeyboard() {
 9         InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
10         if (imm != null)
11             imm.hideSoftInputFromWindow(mSearchText.getWindowToken(), 0);
12     } 

 

 12.移动Dialog的位置

 1 Dialog dialog = new Dialog(this);
 2         
 3         // setContentView可以设置为一个View也可以简单地指定资源ID
 4         // LayoutInflater
 5         // li=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
 6         // View v=li.inflate(R.layout.dialog_layout, null);
 7         // dialog.setContentView(v);
 8         dialog.setContentView(R.layout.dialog_layout);
 9 
10         dialog.setTitle("Custom Dialog");
11 
12         /* 
13          * 获取圣诞框的窗口对象及参数对象以修改对话框的布局设置,
14          * 可以直接调用getWindow(),表示获得这个Activity的Window
15          * 对象,这样这可以以同样的方式改变这个Activity的属性.
16          */
17         Window dialogWindow = dialog.getWindow();
18         WindowManager.LayoutParams lp = dialogWindow.getAttributes();
19         dialogWindow.setGravity(Gravity.LEFT | Gravity.TOP);
20 
21         /*
22          * lp.x与lp.y表示相对于原始位置的偏移.
23          * 当参数值包含Gravity.LEFT时,对话框出现在左边,所以lp.x就表示相对左边的偏移,负值忽略.
24          * 当参数值包含Gravity.RIGHT时,对话框出现在右边,所以lp.x就表示相对右边的偏移,负值忽略.
25          * 当参数值包含Gravity.TOP时,对话框出现在上边,所以lp.y就表示相对上边的偏移,负值忽略.
26          * 当参数值包含Gravity.BOTTOM时,对话框出现在下边,所以lp.y就表示相对下边的偏移,负值忽略.
27          * 当参数值包含Gravity.CENTER_HORIZONTAL时
28          * ,对话框水平居中,所以lp.x就表示在水平居中的位置移动lp.x像素,正值向右移动,负值向左移动.
29          * 当参数值包含Gravity.CENTER_VERTICAL时
30          * ,对话框垂直居中,所以lp.y就表示在垂直居中的位置移动lp.y像素,正值向右移动,负值向左移动.
31          * gravity的默认值为Gravity.CENTER,即Gravity.CENTER_HORIZONTAL |
32          * Gravity.CENTER_VERTICAL.
33          * 
34          * 本来setGravity的参数值为Gravity.LEFT | Gravity.TOP时对话框应出现在程序的左上角,但在
35          * 我手机上测试时发现距左边与上边都有一小段距离,而且垂直坐标把程序标题栏也计算在内了,
36          * Gravity.LEFT, Gravity.TOP, Gravity.BOTTOM与Gravity.RIGHT都是如此,据边界有一小段距离
37          */
38         lp.x = 100; // 新位置X坐标
39         lp.y = 100; // 新位置Y坐标
40         lp.width = 300; // 宽度
41         lp.height = 300; // 高度
42         lp.alpha = 0.7f; // 透明度
43 
44         // 当Window的Attributes改变时系统会调用此函数,可以直接调用以应用上面对窗口参数的更改,也可以用setAttributes
45         // dialog.onWindowAttributesChanged(lp);
46         dialogWindow.setAttributes(lp);
47 
48         /*
49          * 将对话框的大小按屏幕大小的百分比设置
50          */
51 //        WindowManager m = getWindowManager();
52 //        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
53 //        WindowManager.LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值
54 //        p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的0.6
55 //        p.width = (int) (d.getWidth() * 0.65); // 宽度设置为屏幕的0.95
56 //        dialogWindow.setAttributes(p);
57 
58         dialog.show();

 

13.数据库操作

Android中结合OrmLite for android组件对SQLite的CRUD(增删改查)操作实例

 

14.环形ProgressBar的实现

 1  android:fromDegrees="0"
 2     android:pivotX="50%"
 3     android:pivotY="50%"
 4     android:toDegrees="360" >
 5 
 6    
 7         android:innerRadiusRatio="3"
 8         android:shape="ring"
 9         android:thicknessRatio="30"
10         android:useLevel="false" >
11 
12        
13             android:centerColor="#FFFFFF"
14             android:centerY="0.01"
15             android:endColor="#FFFFFF"
16             android:startColor="#000000"
17             android:type="sweep"
18             android:useLevel="false" />
View Code
1 <ProgressBar
2         android:layout_width="wrap_content"
3         android:layout_height="wrap_content"
4         android:layout_gravity="center_horizontal"
5         android:indeterminateDrawable="@drawable/wel_act_loading_pro"
6         />
View Code
gradient里的属性没什么好介绍的, 这里主要是shape.
下列属性只在android:shape=”ring”时使用:
android:innerRadiux
尺寸值,它用尺寸值或尺寸资源指定圆环内部的半径(指中间的圆孔的半径)。
 
android:innerRadiusRatio
浮点值,它用圆环宽度的比率来表示内部圆环的半径。例如,如果android:innerRadiusRatio=”5”,那么内部半径就等于圆环的宽度除以5。这个值会被android:innerRadius的值覆盖。默认是9。
 
android:thickness
尺寸值,它用一个尺寸值或尺寸资源来定义圆环的厚度。
 
android:thicknessRatio
浮点值。它用圆环宽度的比率来表示圆环的厚度。例如,如果android:thicknessRatio=”2”,那么厚度就等于圆环的宽度除以2。这个值会被android:innerRadius覆盖。默认值是3。也就是说这个值设置的越大圆环就越细了。
 
android:useLevel
布尔值,如果这个形状要用于LevelListDrawable对象,那么就设置为true。通常应该设置为false或者让形状不可见。
 
15、Notification中如何打开一个已经存在的Activity?
Notification并不要求一定重新启动一个全新的Activity(如果已经存在),如果想要打开一个已经存在的Activity,则将生成PendingIntent的intent设置为:
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
即可。
posted @ 2011-12-08 22:42  大脚印  阅读(1194)  评论(0编辑  收藏  举报