android 调用系统设置界面

在编写android应用程序时,如果需要调用系统原生的管理应用程序界面呢?本人在一个项目中遇到过,本人没有发现这方面现成的intent,不过通过看源代码实现了。

android源代码application_settings.xml

  1. <PreferenceScreen  

  2.           android:title="@string/manageapplications_settings_title"  

  3.           android:summary="@string/manageapplications_settings_summary">  

  4.       <intent android:action="android.intent.action.MAIN"  

  5.               android:targetPackage="com.android.settings"  

  6.               android:targetClass="com.android.settings.ManageApplications" />  

  7.   </PreferenceScreen>  


熟悉PreferenceScreen的朋友应该知道如何做了,如果不熟悉的朋友,文章最后贴出一篇文章,大家参考一下就知道了,根据xml的描述,我们可以用如下代码调用系统原生的管理应用程序界面

  1. Intent intent =  new Intent();  

  2. intent.setAction("android.intent.action.MAIN");  

  3. intent.setClassName("com.android.settings", "com.android.settings.ManageApplications");  

  4. startActivity(intent);  

如果要进入某个软件详细设置界面 可以如下操作:  

“Android系统设置->应用程序->管理应用程序”列表下,列出了系统已安装的应用程序。选择其中一个程序,则进入“应用程序信息(Application Info)”界面。这个界面显示了程序名称、版本、存储、权限等信息,并有卸载、停止、清除缓存等按钮,可谓功能不少。如果在编写相关程序时(比如任务管理器)可以调用这个面板,自然提供了很大的方便。那么如何实现呢?

在最新的Android SDK 2.3(API Level 9)中,提供了这样的接口。在文档路径

docs/reference/android/provider/Settings.html#ACTION_APPLICATION_DETAILS_SETTINGS

下,有这样的描述:

public static final String ACTION_APPLICATION_DETAILS_SETTINGS    Since: API Level 9

Activity Action: Show screen of details about a particular application.
In some cases, a matching Activity may not exist, so ensure you safeguard against this.
Input: The Intent's data URI specifies the application package name to be shown, with the "package" scheme. That is "package:com.my.app".
Output: Nothing.
Constant Value: "android.settings.APPLICATION_DETAILS_SETTINGS"

就是说,我们只要以android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS作为Action;“package:应用程序的包名”作为URI,就可以用startActivity启动应用程序信息界面了。代码如下:

  1. Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);  

  2. Uri uri = Uri.fromParts(SCHEME, packageName, null);  

  3. intent.setData(uri);  

  4. startActivity(intent);  

但是,在Android 2.3之前的版本,并没有公开相关的接口。
通过查看系统设置platform/packages/apps/Settings.git程序的源码,可以发现应用程序信息界面为InstalledAppDetails。
在这里(2.1)还有这里(2.2),我们可以分别看到Android2.1和Android2.2的应用管理程序(ManageApplications.java)是如何启动InstalledAppDetails的。
  1. // utility method used to start sub activity  

  2. private void startApplicationDetailsActivity() {  

  3.    // Create intent to start new activity  

  4.    Intent intent = new Intent(Intent.ACTION_VIEW);  

  5.    intent.setClass(this, InstalledAppDetails.class);  

  6.    intent.putExtra(APP_PKG_NAME, mCurrentPkgName);  

  7.    // start new activity to display extended information  

  8.    startActivityForResult(intent, INSTALLED_APP_DETAILS);  

  9. }  

但是常量APP_PKG_NAME的定义并不相同。
2.2中定义为"pkg",2.1中定义为"com.android.settings.ApplicationPkgName"
那么,对于2.1及以下版本,我们可以这样调用InstalledAppDetails:
  1. Intent i = new Intent(Intent.ACTION_VIEW);                  

  2. i.setClassName("com.android.settings","com.android.settings.InstalledAppDetails");  

  3. i.putExtra("com.android.settings.ApplicationPkgName", packageName);  

  4. startActivity(i);  

对于2.2,只需替换上面putExtra的第一个参数为"pkg"
综上,通用的调用“应用程序信息”的代码如下:
  1. private static final String SCHEME = "package";  

  2. /**

  3. * 调用系统InstalledAppDetails界面所需的Extra名称(用于Android 2.1及之前版本)

  4. */  

  5. private static final String APP_PKG_NAME_21 = "com.android.settings.ApplicationPkgName";  

  6. /**

  7. * 调用系统InstalledAppDetails界面所需的Extra名称(用于Android 2.2)

  8. */  

  9. private static final String APP_PKG_NAME_22 = "pkg";  

  10. /**

  11. * InstalledAppDetails所在包名

  12. */  

  13. private static final String APP_DETAILS_PACKAGE_NAME = "com.android.settings";  

  14. /**

  15. * InstalledAppDetails类名

  16. */  

  17. private static final String APP_DETAILS_CLASS_NAME = "com.android.settings.InstalledAppDetails";  

  18. /**

  19. * 调用系统InstalledAppDetails界面显示已安装应用程序的详细信息。 对于Android 2.3(Api Level

  20. * 9)以上,使用SDK提供的接口; 2.3以下,使用非公开的接口(查看InstalledAppDetails源码)。

  21. *

  22. * @param context

  23. *

  24. * @param packageName

  25. *            应用程序的包名

  26. */  

  27. public static void showInstalledAppDetails(Context context, String packageName) {  

  28.    Intent intent = new Intent();  

  29.    final int apiLevel = Build.VERSION.SDK_INT;  

  30.    if (apiLevel >= 9) { // 2.3(ApiLevel 9)以上,使用SDK提供的接口  

  31.        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);  

  32.        Uri uri = Uri.fromParts(SCHEME, packageName, null);  

  33.        intent.setData(uri);  

  34.    } else { // 2.3以下,使用非公开的接口(查看InstalledAppDetails源码)  

  35.        // 2.2和2.1中,InstalledAppDetails使用的APP_PKG_NAME不同。  

  36.        final String appPkgName = (apiLevel == 8 ? APP_PKG_NAME_22  

  37.                : APP_PKG_NAME_21);  

  38.        intent.setAction(Intent.ACTION_VIEW);  

  39.        intent.setClassName(APP_DETAILS_PACKAGE_NAME,  

  40.                APP_DETAILS_CLASS_NAME);  

  41.        intent.putExtra(appPkgName, packageName);  

  42.    }  

  43.    context.startActivity(intent);  

  44. }  

android中 TextView设置滚动条

TextView实现滚动的三种方式:

1、嵌套在ScrollView或者HorizontalScrollView中

垂直滚动:
<scrollview android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:scrollbars="vertical">
   <textview android:text="http://orgcent.com …"/>
</scrollview>

水平滚动:使用标签<horizontalscrollview></horizontalscrollview>

一定要注意的是 ScrollView要放到 textView外边  才能生效地 。  

2、设置ScrollingMovementMethod
代码中添加:

TextView.setMovementMethod(new ScrollingMovementMethod());

XML中配置:

android:scrollbars="vertical"

特别要注意 2条件同时满足才能效。    

3、使用Scroller来自定义TextView
点击查看:android自定义View-垂直滚动的TextView



如果需要删除TextView的 滚动条 可以 设置ScrollView 的xml的属性 android:scrollbars="none"



android 返回布局View

话说xml布局就是view?不理解:

三种方式可以生成LayoutInflater:

LayoutInflater inflater=LayoutInflater.from(this);
LayoutInflater inflater=getLayoutInflater();
LayoutInflater inflater=(LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);

然后调用inflate方法将xml布局文件转成View   

public View inflate(intresource,ViewGrouproot,booleanattachToRoot)
//在View类中,也有inflate方法
public static View inflate(Contextcontext,intresource,ViewGrouproot)

 

android控制软键盘弹出或者隐藏

在程序中加入以下代码时,软键盘会出现:

InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.RESULT_SHOWN);

如果要让软键盘消失,则为以下代码:

InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

Android 关于数据库访问

Android 用的 数据库是Sqlite3,。

android 操作数据库和我们用其他语言操作数据库差不多。

首先取得数据库对象

SQLiteDatabase

db= SQLiteDatabase.openDatabase(MyApp.MAINACITVITY.getDatabasePath("数据库文件名").toString(),null,SQLiteDatabase.OPEN_READONLY);

然后通过db取得cursor对象 这个cursor相当于数据集 dataset

Cursor cursor=db.rawQuery("select id,name,x,y from locationInfo ", null);
MainActivity.ccsuMapAPP.startManagingCursor(cursor);//这句话要调用一次使得cursor生命周期和activity同步 但是最后要记得调用stopManagingCursor()
while(cursor.moveToNext()){
LocationTag tag=new LocationTag(MainActivity.ccsuMapAPP,this);
tag.setBaseInfo(cursor.getInt(0), cursor.getString(1), cursor.getInt(2), cursor.getInt(3));
tags.add(tag);
}

总结一下Activity.startManagingCursor方法: 转
我们将获得的Cursor对象交与Activity 来管理,这样Cursor对象的生命周期便能与当前的Activity自动同步,省去了自己管理Cursor。

1.这个方法使用的前提是:游标结果集里有很多的数据记录。
所以,在使用之前,先对Cursor是否为null进行判断,如果Cursor != null,再使用此方法

2.如果使用这个方法,最后也要用stopManagingCursor()来把它停止掉,以免出现错误。

3.使用这个方法的目的是把获取的Cursor对象交给Activity管理,这样Cursor的生命周期便能和Activity自动同步,
省去自己手动管理。

android中几个基础知识

1: 获取asset下面的文件 获得文件IO流。

(context).getResources().getAssets().open("文件名")

2: 通过流创建Bitmap对象:

BitmapFactory.decodeStream(is) 返回Bitma对象,获取后要记得关闭

3:通过流直接获取Drawab

Bitmap b=new BitmapDrawable(is)

Bitmap Config 参数

Bitmap.Config ALPHA_8
Bitmap.Config ARGB_4444
Bitmap.Config ARGB_8888
Bitmap.Config RGB_565
A  R  G  B
透明度 红色 绿色 蓝色
Bitmap.Config ARGB_4444 16 每个像素 占四位
Bitmap.Config ARGB_8888 32 每个像素 占八位
Bitmap.Config RGB_565 16 R占5位 G占6位 B占5位 没有透明度(A)
一般情况下我们都是用argb888 但是无可厚非 它也相对的很占内存
因为一个像素32位 8位一个字节 如果是800*480的图片的话自己算 估计有1M多了

4: 计算字体宽度 在canvas中

Paint p=new Paint();
p.setTextSize(24); 
Rect bounds = new Rect(); 
p.getTextBounds(str,0, str.length(), bounds); 
int width = bounds.width();

5.  什么是density

引用

1) density
density表示每英寸有多少个显示点(逻辑值),它的单位是dpi:dot per inch,通常屏幕大时,density就大,屏幕小时,density就小,通常
屏幕实际分辨率为240px*400px时,density=120
屏幕实际分辨率为320px*533px,density=160
屏幕实际分辨率为480px*800px,density=240

2)  分辨率
是整个屏是多少点,比如800×480,它是软件的显示单位

3) 从Android1.6版本开始支持density(对应API Level 4)
用于解决应用在屏幕大小不同的硬件上正常显示的问题

6.相关代码及设置

引用

1)         AndroidManifest.xml

这个参数在API Level 4也就是SDK 1.6以后才启用的,而且1.6版本的API Level 4的应用默认值就是True

2) 资源目录名(android 2.0以后)
res/xxx-hdpi         当density为240时,使用此目录下的资源
res/xxx-mdpi        当density为160时,使用此目录下的资源
res/xxx-ldpi          当density为120时,使用此目录下的资源
res/xxx                 不常后缀,为默认设置,同xxx-mdpi

3) 资源单位(xml文件中定义大小的单位)

a)  dp=dip=dx (Density independent pixel)
基于屏幕密度的抽象单位,设备无关的点,用于说明与密度无关的尺寸和位置。这些单位是相对于一个160dpi的屏幕,所有一个dp是160dpi屏幕上的一个点。

b)  px
px指软件的单位点,设备相关的点

c)  具体使用

i.布局时尽量使用单位dip,少使用px
若使用px,如果设某控件大小400x400px,在800×480上显示正常,而在533×320的屏上就超出屏幕了
若使用dp,如果设某控件大小为160x160dp,就可以通过从系统中取density来算出真正的大小,比如在800×480屏的density设为240,而533×320屏的density设为160,借以下公式计算
pixels = dips * (density / 160)
在800×480在屏中显示240象素,而在533×320的屏中显示为160象素,控件在屏中显示的比例是一样的。

ii. 内部的处理过程分为三步:
取screen中其它元素,转为应用的值,计算应用各控件位置,然后再转成800×480以供真正显示
若res-xxx不存在,则读取res有的资源,然后对其做相应缩放

7. 实现density的关键源码

1)         BitmapFactory.java(用于缩放图片)

2)         ComptibilityInfo.java(用于位置计算)

8.取得屏幕信息(宽高等)

public static String getDisplayMetrics(Context cx) {  

 String str = "";  

 DisplayMetrics dm = new DisplayMetrics();  

 dm = cx.getApplicationContext().getResources().getDisplayMetrics();  

 int screenWidth = dm.widthPixels;  

 int screenHeight = dm.heightPixels;  

 float density = dm.density;  

 float xdpi = dm.xdpi;  

 float ydpi = dm.ydpi;  

 str += "The absolute width:" + String.valueOf(screenWidth) + "pixelsn";  

 str += "The absolute heightin:" + String.valueOf(screenHeight)  

     + "pixelsn";  

 str += "The logical density of the display.:" + String.valueOf(density)  

     + "n";  

 str += "X dimension :" + String.valueOf(xdpi) + "pixels per inchn";  

 str += "Y dimension :" + String.valueOf(ydpi) + "pixels per inchn";  

 return str;  

}  

9. 取得状态栏和标题栏高度

@Override  

protected void onCreate(Bundle savedInstanceState) {  

 super.onCreate(savedInstanceState);  

 setContentView(R.layout.layout_test2);  

 tv1 = (TextView) findViewById(R.id.TextView01);  

 tv1.post(new Runnable(){  

   public void run()  

   {  

     processLayout();  

   }  

 });  

}  

 

private void processLayout(){  

   

 Rect rect= new Rect();  

 Window window= getWindow();  

 tv1.getWindowVisibleDisplayFrame(rect);  

 //状态栏高度  

 int statusBarHeight= rect.top;  

 int contentViewTop= window.findViewById(Window.ID_ANDROID_CONTENT).getTop();  

 //标题栏高度  

 int titleBarHeight= contentViewTop - statusBarHeight;  

 //测试结果:ok之后 100多 ms 才运行了  

 Log.v("test", "=-init-= statusBarHeight="+statusBarHeight+  

 " contentViewTop="+contentViewTop+  

 " titleBarHeight="+titleBarHeight);      

 

}  

android UI布局常见问题

1.Android fill_parent、wrap_content和match_parent的区别

三个属性都用来适应视图的水平或垂直大小,一个以视图的内容或尺寸为基础的布局比精确地指定视图范围更加方便。

1)fill_parent

设置一个构件的布局为fill_parent将强制性地使构件扩展,以填充布局单元内尽可能多的空间。这跟Windows控件的dockstyle属性大体一致。设置一个顶部布局或控件为fill_parent将强制性让它布满整个屏幕。

2) wrap_content

设置一个视图的尺寸为wrap_content将强制性地使视图扩展以显示全部内容。以TextView和ImageView控件为例,设置为wrap_content将完整显示其内部的文本和图像。布局元素将根据内容更改大小。设置一个视图的尺寸为wrap_content大体等同于设置Windows控件的Autosize属性为True。

3)match_parent
Android2.2中match_parent和fill_parent是一个意思 .两个参数意思一样,match_parent更贴切,于是从2.2开始两个词都可以用。那么如果考虑低版本的使用情况你就需要用fill_parent了

2 五种布局及其重要标签

  •  框架布局FrameLayout重要标签:

android:layout_gravity

参考    http://wenku.baidu.com/view/12ca5929b4daa58da0114aea.html

android:background

android:layout_marginTop

android:layout_marginBottom

  •  线性布局LinearLayout

  android:orientation

它确定了LinearLayout的方向,其值可以为

*vertical, 表示垂直布局

*horizontal, 表示水平布局

android:layout_gravity

同上。

android:layout_weight

  •  框架布局 FrameLayout

关于设置android:layout_marginTop等属性失效

框架布局是直接覆盖在原图层上的。如果要定位我们需先设置     android:layout_gravity=”left”

然后再设置android:layout_marginTop等属性 否则无效

  • 相对布局RelativeLayout

相对布局 RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一。它灵活性大很多,当然属性也多,操作难度也大,属性之间产生冲突的的可能性也大,使用相对布局时要多做些测试。

RelativeLayout相对布局

RelativeLayout相对布局

下面是常用的一些属性

RelativeLayout用到的一些重要的属性:

第一类:属性值为true或false
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相对于父元素完全居中
android:layout_alignParentBottom 贴紧父元素的下边缘
android:layout_alignParentLeft 贴紧父元素的左边缘
android:layout_alignParentRight 贴紧父元素的右边缘
android:layout_alignParentTop 贴紧父元素的上边缘
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物

第二类:属性值必须为id的引用名“@id/id-name”
android:layout_below 在某元素的下方
android:layout_above 在某元素的的上方
android:layout_toLeftOf 在某元素的左边
android:layout_toRightOf 在某元素的右边

android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐

第三类:属性值为具体的像素值,如30dip,40px
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离

EditText的android:hint

设置EditText为空时输入框内的提示信息。

android:gravity
android:gravity属性是对该view 内容的限定.比如一个button 上面的text. 你可以设置该text 在view的靠左,靠右等位置.以button为例,android:gravity=”right”则button上面的文字靠右

android:layout_gravity
android:layout_gravity是用来设置该view相对与起父view 的位置.比如一个button 在linearlayout里,你想把该button放在靠左、靠右等位置就可以通过该属性设置.以button为例,android:layout_gravity=”right”则button靠右

android:layout_alignParentRight
使当前控件的右端和父控件的右端对齐。这里属性值只能为true或false,默认false。

android:scaleType:
android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType / android:scaleType值的意义区别:

CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示

CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)

CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽

FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示

FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置

FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置

FIT_XY / fitXY 把图片不按比例扩大/缩小到View的大小显示

MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。

** 要注意一点,Drawable文件夹里面的图片命名是不能大写的。

 

 

 

 

其他

Android中的Layout_weight详解

布局文件是:

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android“
    android:orientation=”horizontal”
    android:layout_width=”fill_parent”
    android:layout_height=”fill_parent”
    >
<Button
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:text=”Button1″
/>
<Button
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_weight=”2″
android:text=”Button2″
/>
</LinearLayout>
出现的布局是:button1占了2/3,button2占了1/3。
57e34f80d09736ec6d811907.jpg.png
但是如果将布局文件中的button的属性android:layout_width=”fill_parent”改为android:layout_width=”wrap_content”那么出现的结果为:button1占了1/3,button2占了2/3。
3.png