`

android培训文档提纲(三)

阅读更多
1、Activity和Service组件是Context的子类,BroadcastReceiver和ContentProvider不是

2、用adb shell命令,可以看到底层linux的目录结构
data/anr 记录ANR信息
data/app/*.apk 应用的安装源文件
data/data/package_name 装好的应用会出现在这里,package_name取决于Manifest.xml文件中的定义
data/data/package_name/shared_prefs/name.xml SharedPreferences文件会保存在这里
data/data/package_name/files/file.name 用openFileOnput()创建的文件在这里
data/data/package_name/databases/dbname.db 内置的SQLite数据库文件在这里,如果没有这个文件,就会触发SQLiteOpenHelper类的onCreate()方法

3、利用SharedPreferences来存储键值对
   Context.MODE_PRIVATE 0
   Context.MODE_WORLD_READABLE 1
   Context.MODE_WORLD_WRITEABLE 2
创建的文件在这个路径:shared_prefs/name.xml

4、用Context的createPackageContext()方法,可以得到另外一个应用的Context,不过前提是你得知道另外一个应用的package_name,但是别人如果不告诉你,你一般是不会知道的
Context.createPackageContext(String packageName,Context.MODE_WORLD_READABLE)

5、SharedPreferences are ultimately backed by XML files on the Android filesystem

6、Context.openFileOutput(String fileName,Context.MODE_PRIVATE);
创建的文件保存在files/file.name

7、Context.openFileInput(String fileName,Contet.MODE_PRIVATE);

8、Occasionally, setting the user ID of your application can be extremely useful. For instance, if you have multiple applications that need to share data with one another, but you also don’t want that data to be accessible outside that group of applications, you might want to make the permissions private and share the UID to allow access. You can allow a shared UID by using the sharedUserId attribute in your manifest: android:sharedUserId="YourID".

9、When you place a file in the res/raw location, it’s not compiled by the platform, but is available as a raw resource

10、Files in res/xml are different from raw files in that you don’t use a stream to access them because they’re compiled into an efficient binary form when deployed.

11、The SD card is removable, and SD card support on most devices (including Android-powered devices) supports the File Allocation Table (FAT) ) filesystem. The SD card doesn’t have the access modes and permissions that come from the Linux filesystem.

12、要用好Android内置的SQLite,首先需要熟练掌握下面4个类的用法
SQLiteOpenHelper
SQLiteDatabase
ContentValues
Cursor

13、Unlike the SharedPreferences you saw earlier, you can’t make a database WORLD_READABLE. Each database is accessible only by the package in which it was created. If you need to pass data across processes, you can use AIDL/Binder or create a ContentProvider, but you can’t use a database directly across the process/package boundary.

14、内置的SQLite数据库,可以用
adb shell
cd data/data/package_name/databases
sqlite3 name.db
来访问,在这个命令行界面,可以使用很多命令,有一个比较方便的是.header ON,可以在用select语句的时候,打印出列明。不然什么都看不到,因为默认的是.header OFF

15、ContentProvider可以跨应用访问数据库

16、用完Cursor之前,不能调用SQLiteDatabase.close();否则Cursor就失效了,为了这个低级错误,浪费了很多时间调试代码。。。不过最终写了一个还算不错的通用数据库访问层,可以支持简单的ORM和跨表事务。代码在公司,周一再专门写一篇博客发出来

17、SQLiteDatabase.insert()中用到的ContentValues不包含autoincrement的_id,这个也是低级错误。。也研究了好长时间

18、单元测试继承自AndroidTestCase,里面有一个getContext()

19、MainActivity.this,这个一般常出现在OnClickListener的onClick()方法体里,因为OnClickListener在这个场景里常常是作为一个内部类出现,所以MainActivity.this,指的就是所在的MainActivity对象的引用

20、Cursor用完要关闭哦,亲,不是只关DB就可以的

21、如果一个Receiver要同时侦听好多事件,可以在onReceive()方法里用intent.getAction()来判断触发的是哪个事件

22、侦听网络变化用ConnectivityManager,而不是TelephonyManager,因为可能会捕获到2次OFF_LINE事件
public class NetworkChangeReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {

		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		NetworkInfo status = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if (status.isConnected()) {
			Log.i("tag", "网络可用");
		} else {
			Log.i("tag", "网络不可用");
		}

	}

}

<application android:icon="@drawable/icon" android:label="@string/app_name">
    
        <receiver android:name=".receiver.NetworkChangeReceiver">
			<intent-filter>
				<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
			</intent-filter>
		</receiver>

    </application>
    
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics