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.1Android2.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自动同步,
省去自己手动管理。